Click here to Skip to main content
15,906,558 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to insert update delete gridview rows using c# code
Posted

this might help u:
Editable GridView in ASP.NET 2.0[^]
 
Share this answer
 
Comments
DGKumar 19-Oct-11 8:37am    
Thank you sir
member60 1-Nov-11 5:38am    
you are wel come,
Here is all you need - MSDN - GridView Examples for ASP.NET 2.0 [^]

You will find many more examples if you search the web.
 
Share this answer
 
Comments
DGKumar 19-Oct-11 8:37am    
Thank you sir
Ankur\m/ 19-Oct-11 9:43am    
You are welcome. Please do a search before you ask.
Gridview has it's own properties like edit,update,delete you just need to apply the respective events and just handle the code
 
Share this answer
 
 protected void GridName_DeleteCommand(object source, GridCommandEventArgs e)
     { your code goes here}

protected void GridName_UpdateCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
{
if (!(e.Item is GridEditFormInsertItem))
 itemID = (e.Item as GridEditFormItem).GetDataKeyValue("ID").ToString();

 if (!string.IsNullOrEmpty(itemID))       //case edit
        { }
else // case add new

your code goes here

}
 
Share this answer
 
 protected void GridName_DeleteCommand(object source, GridCommandEventArgs e)
     { your code goes here}

protected void GridName_UpdateCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
{
if (!(e.Item is GridEditFormInsertItem))
 itemID = (e.Item as GridEditFormItem).GetDataKeyValue("ID").ToString();

 if (!string.IsNullOrEmpty(itemID))       //case edit
        { }<pre lang="c#">

else // case add new

your code goes here

}

and also you have to add in the aspx page the below code in your grid
onitemcommand="GridName_ItemCommand"
ondeletecommand="GridNameitems_DeleteCommand"
onitemdatabound="GridName_ItemDataBound"
 
Share this answer
 
C#
protected void gvPerson_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvGroupName.EditIndex = e.NewEditIndex;
            gvGroupName.DataBind();
        }

        protected void gvPerson_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvGroupName.EditIndex = -1;
            gvGroupName.DataBind();
        }

        protected void gvPerson_RowUpdating(object sender, GridViewUpdateEventArgs e)
      {

int intId = (int)gvGroupName.DataKeys[e.RowIndex].Value;
        DataRow personRow = dt.Select("groupId=" + intId)[0];

      TextBox txtGname = (TextBox)gvGroupName.Rows[e.RowIndex].Cells[1].Controls[0];
      TextBox txtgroupDes = (TextBox)gvGroupName.Rows[e.RowIndex].Cells[2].Controls[0];
    
      personRow["GName"] = txtGname.Text;
     personRow["groupDes"] = txtgroupDes.Text;


    SqlCommandBuilder cb = new SqlCommandBuilder(da);

     da.Update(dt);

      gvGroupName.EditIndex = -1;

       gvGroupName.DataBind();

}

protected void gvGroup_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {


            
          
            SqlCommand cd = new SqlCommand();
            cd.Connection = con;
            cd.CommandText = "delete from groupName where groupId=@groupId";
            cd.CommandType = System.Data.CommandType.Text;
            cd.Parameters.Add("@groupId", System.Data.SqlDbType.Int).Value = gvGroupName.DataKeys[e.RowIndex].Values["groupId"].ToString();
            con.Open();
            cd.ExecuteNonQuery();
         
            gvGroup();
            DropDownGroup();
            con.Close();

        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900