Answered by:
How to take ID of a row in gridview when you press a button in a row

Question
-
User-1130188263 posted
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.
Tuesday, June 2, 2020 5:04 PM
Answers
-
User-943250815 posted
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<asp:LinqDataSource ID="LinqDataSource1" runat="server" ContextTypeName="MyDBDataContext" EntityTypeName="" TableName="Projects"> </asp:LinqDataSource> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ProjectID " DataSourceID="LinqDataSource1"> <Columns> <asp:CommandField ShowSelectButton="True" ButtonType="Button" /> <asp:BoundField DataField="ProjectName" HeaderText="ProjectName" InsertVisible="False" SortExpression="ProjectName" /> <asp:BoundField DataField="ClientID" HeaderText="ClientID" ReadOnly="True" SortExpression="ClientID" /> <asp:BoundField DataField="ProjectLeader" HeaderText="ProjectLeader" SortExpression="ProjectLeader" /> <asp:BoundField DataField="StartDate" HeaderText="StartDate" SortExpression="StartDate " /> </Columns> </asp:GridView>
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
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, June 2, 2020 7:19 PM
All replies
-
User-943250815 posted
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<asp:LinqDataSource ID="LinqDataSource1" runat="server" ContextTypeName="MyDBDataContext" EntityTypeName="" TableName="Projects"> </asp:LinqDataSource> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ProjectID " DataSourceID="LinqDataSource1"> <Columns> <asp:CommandField ShowSelectButton="True" ButtonType="Button" /> <asp:BoundField DataField="ProjectName" HeaderText="ProjectName" InsertVisible="False" SortExpression="ProjectName" /> <asp:BoundField DataField="ClientID" HeaderText="ClientID" ReadOnly="True" SortExpression="ClientID" /> <asp:BoundField DataField="ProjectLeader" HeaderText="ProjectLeader" SortExpression="ProjectLeader" /> <asp:BoundField DataField="StartDate" HeaderText="StartDate" SortExpression="StartDate " /> </Columns> </asp:GridView>
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
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, June 2, 2020 7:19 PM -
User-1130188263 posted
sorry i dont seem to understand your code... Im using ASP.NET
Tuesday, June 2, 2020 7:50 PM -
User-943250815 posted
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 }Tuesday, June 2, 2020 7:57 PM -
User-1130188263 posted
Thanks man i understand c# now and it works
Tuesday, June 2, 2020 8:30 PM