How i could set the value of DataKeyNames of gridview in C# code?
since my gridview is generated dynamically, i need to set it from .cs file
Write code in cs file like
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 property of the grid:
myGrid.DataKeyNames = new string[] {"Id", "Name"};
This corresponds to the following declarative code:
<asp:GridView id="myGrid" DataKeyNames="Id,Name" ... />
Something like this:
myGridView.DataKeyNames =new string[]{"PrimaryKeyId"}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With