Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bound Column in ASP Grid view when invisible cannot be accessed

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.

like image 506
chugh97 Avatar asked Jan 24 '23 18:01

chugh97


2 Answers

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;
like image 82
Cerebrus Avatar answered Jan 27 '23 12:01

Cerebrus


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'
like image 22
leppie Avatar answered Jan 27 '23 13:01

leppie