GridView Hidden Column Data Access
Again I faced an issue, as usual before .net version 2.0 I set a column's visibility property of a gridview to false.
If you are a developer by now you know why I did that. I know you guessed it. I just wanted to use that column's value
in one of my calculations, and didn't want to show it to the user.
Grid is working fine it hides the column I wanted to hide, I am happy!., Suddenly I realized that I don't get the value from
the hidden (visibility= false) column's cell value in the following method.
protected void grd_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = grd.SelectedRow;
string aValue = row.Cells[8].Text; //this is the column I set false visibility
}
I was looking my coding deeply I am sure this is correct 100%, then what has gone wrong?
With 2.0 and above versions on .Net when you set a column of a gridview to visibility false
it doesn't bind the data to this hidden column. You must be really disappointed hearing this
news, even I did. Wait you don't have to worry so long.
Don't set your column's property visibility to false in the design view. If you do so it will
call before the databind. When there is no column databing is not take place and ignores this hidden column.
Obvious we don't get data (you are lucky only you didn't get the data, worse case this might throw an error)
Trick to overcome this issue,
RowCreated event of a GridView takes place after the data binding, so we can do something like following
code snippet.
protected void grd_RowCreated(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[8].Visible = false;
}
This hides the column, still you can access the cell value,
protected void grd_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = grd.SelectedRow;
string aValue = row.Cells[8].Text; //I get the cell value, I am happy!
}
Enjoy working on Gridview control,
Blog Archive
Important Blogs
-
-
Git Workflow For Enterprise development6 years ago
-
Watch Live Cricket Online Free14 years ago
-
-
-
-
2 comments:
man you are genius, you solved my problem , ton of thanks
Thank you,
you solved my problem
Post a Comment