I have a GridView with BoundColumns. The first 2 columns are hidden and I would like to access them using gridView1.Rows[0].Cells[0].Text and gridView1.Rows[0].Cells[1].Text respectively and I get a empty string. When the columns are changed to visible, then I can access the values. I have tried changing the column width to zero as suggested on some other forums but that never fixed the problem. Does any one have any pointers as to what I may be doing wrong.
If the Columns are part of the DataKeyNames
collection, then you should get their values from the GridView.DataKeys[index].value
property, as demonstrated on the GridViewGuy site.
If however, they are not part of the DataKeyNames
collection, then you can use the following hack to make sure that the value is persisted in ViewState (as opposed to normal behaviour for hidden fields in ASP.NET 2+)
GridView1.DataSource = myDataSource;
// Set the column visibility to true before Databinding
GridView1.Columns[0].Visible = true;
GridView1.Columns[1].Visible = true;
GridView1.DataBind()
// Set the column visibility to false after Databinding
GridView1.Columns[0].Visible = false;
GridView1.Columns[1].Visible = false;
This is typical behaviour in ASP.NET, Visible = false, only makes the control available in code-behind.
The best for this would be to apply the following style on that column:
display:'none'
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