For context read on but, ultimately, is it possible/(and how) to update (remove/hide a column) a GridView in the OnDataBound() or OnPreRender() events?
I query data from a database and display it on a gridview. I’m using the OnRowDataBound event to format numbers, etc. In that result set I have a column named ‘orderby’ that I use to order the result rows. I would like to remove this column from the gridview. If I autogeneratecolumns I can hide this column in the OnRowDataBound() event by doing the following (e.Row.Cells[r].Visible = false;) on the DataControlRowType.Header and DataControlRowType.DataRow. If I don’t autogenerate columns and I add hidden="true" in the column parameters of the .aspx, it does not work. I’m not looking for a solution in the .aspx btw…
For some reason I thought that the OnDataBound() and OnPreRender() events could be used to update GridViews (hide rows, columns, create total row) but that does not seem to be the case. If I try the below it does not work (gridView.Columns.Count is 0 even though there is data). I can get the desired effect if I use the OnRowDataBound() event but I was wondering:
- can the OnDataBound() and OnPreRender() events update a gridview? If so pls point me to an example.
- what can you do with the OnDataBound() and OnPreRender()/what are they good for? I haven’t really used them before and, again, I thought that they would be able to help with last minute updates/touchups on grid views.
Note: The OnRowDataBound() method is doing updates row by row, column by
column. With a complicated page you can easily have some nasty long
code. So I would imagine that once all of that is sorted out there
would be a way to ‘easily’ hide a column for example; but I could be
wrong…
btw I’m running: .Net version: 4.0.30319.42000, runtimeVer: .NET Framework 4.8.4645.0
protected void checks_DataBound(object sender, EventArgs e)
{
GridView gridView = (GridView)sender;
if (gridView.ClientID == "checks_db_sod01")
{
for (int c = 0; c < gridView.Columns.Count; c++)
{
if (gridView.Columns[c].HeaderText.ToLower().Trim() == "orderby")
{
gridView.Columns[c].Visible = false;
}
}
}
}
3
Answers
SOLUTION Thanks to Ortund and Rand Random I tried a few things. To be clear, Ortund's reply used gridView.Columns and for some reason, in OnPreRender my gridView had 3 rows and 0 columns. Without OnPreRender() the gridView whould display 4 rows (1 header, 3 data rows) and 3 columns.
What finally worked is posted below. Note that the solution is iterating through header rows and columns (O(N)) which is OK I guess. Also, if I don't explicitly hide the corresponding headerCell I still get the column but without data. If anyone has another solution, hopefully a faster/more efficient one, that would be most appreciated! Thank you again for those who responded. If anyone has any resource on how to properly do cool things with GridViews pls post it. I'm usually hacking away updates/changes in the OnRowDataBound() method only...
You should probably stick to one question per post but, to address your specific queries:
OnDataBound()
andOnPreRender()
can update certain aspects of theGridView
such as data formatting or applying styles. They’re not used for structural changes like removing columns; for that, you’d typically manipulate the GridView’s properties directly or handle such tasks during the GridView’s initialization or data binding phases.You could possibly use PreRender to hide your column though. Note that it’s been about 5 years since I used web forms, but this might get you started:
Hope this helps!
You could use the RowCreated Event of the GridView and some LINQ to achieve this