How to get primary key in GridView, but not display it?

Using <asp:GridView></asp:GridView> I am trying to display the columns of a database table. I want to get the primary key of the table, but obviously I don't want to display it. How can I achieve this?
This is my gridview

 <asp:GridView ID="MyGrid" runat="server" BorderColor="#0066FF" AllowPaging="false" PageSize="5" AllowSorting="true" AutoGenerateColumns="false" AutoGenerateEditButton="true" OnRowEditing="MyGrid_RowEditing" AutoGenerateDeleteButton="true" OnRowDeleting="MyGrid_RowDeleting" OnRowDataBound="MyGrid_RowDataBound" EmptyDataText="No Value" BorderWidth="0px" BorderStyle="Solid"> <Columns> <asp:BoundField DataField="PrimaryKey" HeaderText="UserId"/> <asp:BoundField DataField="Column1" HeaderText="Column1" /> <asp:BoundField DataField="Column2" HeaderText="Column2" /> <asp:BoundField DataField="Column3" HeaderText="Column3" /> </Columns> </asp:GridView> 

I also tried visible=false hide the primary key, it hides the primary key from the display, but also does not get its value, and I want this value.
Hope my question is clear.

+7
c # gridview
source share
5 answers

You need to set Visible = false to OnRowDataBound , this will mean that the data is still available to you, t on the page.

 <asp:GridView ID="MyGrid" runat="server" BorderColor="#0066FF" AllowPaging="false" PageSize="5" AllowSorting="true" AutoGenerateColumns="false" AutoGenerateEditButton="true" OnRowEditing="MyGrid_RowEditing" AutoGenerateDeleteButton="true" OnRowDeleting="MyGrid_RowDeleting" OnRowDataBound="MyGrid_RowDataBound" EmptyDataText="No Value" BorderWidth="0px" BorderStyle="Solid"> <Columns> <asp:BoundField DataField="PrimaryKey" HeaderText="UserId"/> <asp:BoundField DataField="Column1" HeaderText="Column1" /> <asp:BoundField DataField="Column2" HeaderText="Column2" /> <asp:BoundField DataField="Column3" HeaderText="Column3" /> </Columns> </asp:GridView> 

In Codebehind:

 protected void MyGrid_RowDataBound(object sender, GridViewRowEventArgs e) { e.Row.Cells[0].Visible = false; } 
+6
source share
 <asp:GridView ... DataKeyNames="PrimaryKey" /> 
+2
source share

You can use this:

 <asp:BoundField DataField="PrimaryKey" visible="false" HeaderText="PKId"/> 

Also use this

+1
source share
 <asp:BoundField DataField="PrimaryKey" visible="false" HeaderText="UserId"/> 

Get primary key

 <asp:GridView ID="MyGrid" runat="server" BorderColor="#0066FF" DataKeyNames="PrimaryKey" 

 MyGrid.DataKeys[e.NewEditIndex].Value 

OR

 MyGrid.Rows[e.NewEditIndex].DataBoundItem 
0
source share

If you want to hide the column by its name, not its index in the GridView. After creating a DataTable or Dataset, you need to find the index of the column by its name, and then save the index in a global variable, such as ViewStae, Session, etc., and then call it in a RowDataBound, for example, an example:

 string headerName = "Id"; DataTable dt = .... ; for (int i=0;i<dt.Columns.Count;i++) { if (dt.Columns[i].ColumnName == headerName) { ViewState["CellIndex"] = i; } } ... GridView_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.Header || e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Footer) { int index = Convert.ToInt32(ViewState["CellIndex"]); e.Row.Cells[index].Visible = false; } } 
0
source share

All Articles