I made a gridview table for Projects which has values ProjectName, ClientID, ProjectLeader, StartDate and all the employees that work on that Project. Since i cant fit all the employees that work on a single project inside a small table i made a button
inside that collumn that redirects you to another page where you can see table with all the employees. The employees are in another table in sql and i made third table which connects Projects and employees and it only contains ProjectID and EmployeeID. I have
a button inside every row but i cant find a way how to make in code behind so that it takes the id of that project and prints on another page only employees that are working on that project.
You have to set DataKeyNames on your Gridview so you can get it on code behind at Gridview.SelectedIndexChange
In sample, I set "ProjectID" as DataKeyNames, your datasource should provide it as all other column fields
On GridView SelectedIndexChanged, you get DataKeyName value
Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.SelectedIndexChanged
Dim myPrjID As String = GridView1.SelectedDataKey("ProjectID") ' Get ProjectID of row selected
' Here you do whatever you need with myPrjID
End Sub
What you not understand Gridview control or VB code behind?
Here is same code in C#
protected void GridView1_SelectedIndexChanged(object sender, System.EventArgs e)
{
string myPrjID = GridView1.SelectedDataKey("ProjectID"); // Get ProjectID of row selected // Here you do whatever you need with myPrjID
}
Member
5 Points
9 Posts
How to take ID of a row in gridview when you press a button in a row
Jun 02, 2020 05:04 PM|Markizzz|LINK
I made a gridview table for Projects which has values ProjectName, ClientID, ProjectLeader, StartDate and all the employees that work on that Project. Since i cant fit all the employees that work on a single project inside a small table i made a button inside that collumn that redirects you to another page where you can see table with all the employees. The employees are in another table in sql and i made third table which connects Projects and employees and it only contains ProjectID and EmployeeID. I have a button inside every row but i cant find a way how to make in code behind so that it takes the id of that project and prints on another page only employees that are working on that project.
Participant
1091 Points
673 Posts
Re: How to take ID of a row in gridview when you press a button in a row
Jun 02, 2020 07:19 PM|jzero|LINK
You have to set DataKeyNames on your Gridview so you can get it on code behind at Gridview.SelectedIndexChange
In sample, I set "ProjectID" as DataKeyNames, your datasource should provide it as all other column fields
On GridView SelectedIndexChanged, you get DataKeyName value
Member
5 Points
9 Posts
Re: How to take ID of a row in gridview when you press a button in a row
Jun 02, 2020 07:50 PM|Markizzz|LINK
sorry i dont seem to understand your code... Im using ASP.NET
Participant
1091 Points
673 Posts
Re: How to take ID of a row in gridview when you press a button in a row
Jun 02, 2020 07:57 PM|jzero|LINK
What you not understand Gridview control or VB code behind?
Here is same code in C#
Member
5 Points
9 Posts
Re: How to take ID of a row in gridview when you press a button in a row
Jun 02, 2020 08:30 PM|Markizzz|LINK
Thanks man i understand c# now and it works