Loading ...

Gridview row edit, delete, update and paging | CodeAsp.Net

Gridview row edit, delete, update and paging

 /5
0 (0votes)

Today I would like to share gridview edit, update and delete a row.

Following I have described the some gridview event and how to handle this event.

Edit:

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;

// fetch and rebind the data.
GridBind();
}

Cancel edit:

protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;

// fetch and rebind the data.
GridBind();
}

Update:

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)

{ //fetch datakey/primarykey for identifyning the row
int key = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);

TextBox Uname = (TextBox)GridView1.Rows[e.RowIndex].FindControl("Uname");
TextBox fname = (TextBox)GridView1.Rows[e.RowIndex].FindControl("fname");
TextBox lname = (TextBox)GridView1.Rows[e.RowIndex].FindControl("lname");

TextBox City = (TextBox)GridView1.Rows[e.RowIndex].FindControl("City");

SqlCommand com = new SqlCommand("update userlogin set
username='"+Uname.Text+"',Fname='"+fname.Text+"',Lname='"+lname.Text+"',city='"+City.Text+"'where
Id_Pk="+key+"",con);

con.Open();

com.ExecuteNonQuery();

con.Close();
GridView1.EditIndex = -1;

// fetch and rebind the data.
GridBind();
}

Delete:

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{

//fetch datakey/primarykey for identifyning the row

int key = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);

SqlCommand com = new SqlCommand("Delete from UserLogin where Id_Pk="+key +"",con);
con.Open();
com.ExecuteNonQuery();
con.Close();

// fetch and rebind the data.
GridBind();
}

Paging:

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{

GridView1.PageIndex = e.NewPageIndex;

// fetch and rebind the data.
GridBind();
}

for update/delete you have to define the datakey in gridview at aspx side:

<asp:GridView ID="GridView1" runat="server" DataKeyNames="Id_Pk" AutoGenerateColumns="False"
OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDeleting="GridView1_RowDeleting"
OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" AllowPaging="True"
OnPageIndexChanging="GridView1_PageIndexChanging" PageSize="5" >

Comments (13)

   
test_User
Very helpful Post!!!
2/26/2009
 · 
by
   
Monu
nice post!!!
2/26/2009
 · 
by
   
Manalee
Very useful thing in project.
3/18/2009
 · 
by
   
NIJITH
Thanks....
3/24/2009
 · 
by
   
Adam
Helpful post, but the update command is leaving you exposed to what's call an SQL Injection. You need to use parameterized queries or stored procedures instead.
4/23/2009
 · 
by
   
vinay_jss
yes Adam, I know this will cause sql injection. I used hard coded sql query so that even a new user can understand this easily.
4/23/2009
 · 
by
   
SIMO
thanks for your help
merci beaucoup pour ton aide  
4/24/2009
 · 
by
   
Shridhar
when i'm editing in gridview  Compiler shows this Error Message: CS0103: The name 'GridBind' does not exist in the current context..?

Any Solution for this?
7/7/2009
 · 
by
   
vinay_jss
Yes my friend this GridBind is a user defined method for binding the grid. So you have to use your own bind method instead of this.
7/7/2009
 · 
by
   
shridhara
k Thanks a lot friend... plz can u send ur girdbind code for reference
7/8/2009
 · 
by
   
shridhara
when i was updating row got this error "Specified argument was out of the range of valid values.." any solution for this...
I hv written own girdbind method then its working..
7/8/2009
 · 
by
   
mizmaar3
Hey thnx, really helpfull
but as im new in .net, can u tell me that is it necessary to use coltrol for this code ?
as i understant the following code using Control
>>>TextBox Uname = (TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[1];
3/16/2010
 · 
by
   
vinay_jss
I have made some change in GridView1_RowUpdating event handler:for example:TextBox Uname = (TextBox)GridView1.Rows[e.RowIndex].FindControl("Uname");instead of following line TextBox Uname = (TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[1];
            
9/6/2010
 · 
by

Top Posts