dynamically set value of DataKeyNames in gridview
How i could set the value of DataKeyNames of gridview in C# code?
since my gridview is generat开发者_运维百科ed 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"}
You can set DataKeyNames Property of your GridView.
{YourGriViewID}.DataKeyNames = new string[] {"ColV", "Col"};
精彩评论