Sunday, August 23, 2009

How to get the current Primary Key from GridView(ASP.NET 2.0/3.5)?

Hi Readers,

Thank you for your emails .I am very grateful to your emails.They help me
post more blogs even i am very busy debug codes.

Take a good look at this grid:



What if i want to update Documents table with this statement:

Update Documents SET Status='Active' WHERE DocumentID=Current selected row on gridview!!!!(Remember Document is a SQL Keyword)

Here are the easy way to do it(VB.NET):

Step1: fire RowCommand event from Gridview( ex GridView1_RowCommand ...Handles GridView1.RowCommand)

Step2: Use CommandName and CommandArgument properties

Ex commandname are :Edit,Update,Delete,Insert....
commandargument is used to find the primary key of currently select item on gridview

Here is the code within GridView1_RowCommand :

Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand

Dim itm As Integer
If e.CommandName.Equals("Edit") Then

itm = Convert.ToInt32(e.CommandArgument) REM get primary key of selected row on gridview
UpdateStatus(itm)
REM your code goes here.Update() is a function with parameter named itm.
It goals is to update Documents table based on selected row on the grid.

End If

End Sub


That`s all i want to share with you today.
Any question or if you want private training a lower cost please send
me email at johnstalley@live.com

Thanks
Talley Ouro
.Net Developer(VB.NET,C#,PHP,ASP.NET 2.0/3.5)

No comments:

About Me

My photo
Raleigh, NC, United States
I am a software developer based in Raleigh,NC,USA.I design softwares and systems ;i also do consulting for companies worldwide.I program with these languages:VB.NET 2003/2005/2008;C#;Java(fun),SQL(200,2005,2008);ASP.NET 2.0/3.5;ASP.NET AJAX;ASP.NET MVC;JavaScript;JQuery;Windows Workflow Foundation;Web Services.I have 4 years + in programming.