Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dynamically set value of DataKeyNames in gridview

Tags:

c#

asp.net

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

like image 748
MayureshP Avatar asked Jun 04 '11 11:06

MayureshP


3 Answers

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);
}
like image 143
AB Vyas Avatar answered Oct 04 '22 16:10

AB Vyas


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" ... />
like image 36
M4N Avatar answered Oct 04 '22 15:10

M4N


Something like this:

myGridView.DataKeyNames =new string[]{"PrimaryKeyId"} 
like image 20
danyolgiax Avatar answered Oct 04 '22 17:10

danyolgiax