How can I set the value of a DataKeyNames gridview in C # code?
since my gridview is dynamically generated, I need to install it from a .cs file
Enter the code in the cs file, for example
Gridview d
protected void Page_Load(object sender, EventArgs e) { d = new GridView(); d.DataKeyNames = new string[] { "Column1", "Column2" }; form1.Controls.Add(d); }
You can set it like any other grid property:
myGrid.DataKeyNames = new string[] {"Id", "Name"};
This corresponds to the following declarative code:
<asp:GridView id="myGrid" DataKeyNames="Id,Name" ... />
- :
myGridView.DataKeyNames =new string[]{"PrimaryKeyId"}
DataKeyNames GridView.
{YourGriViewID}.DataKeyNames = new string[] {"ColV", "Col"};