|
How to remove DataGrid column
This is one of the most frequently asked question by developers who are using
DataGrid control for displaying data. A lot of experts have suggested different
approaches. So we decided to do alittle research on this. And to our surprise,
the solution could not have been that simple.
The key to the solution is capturing ItemDataBound event. When the
page recieves this event, complete information about the data row is available
in DataGridItemEventArgs parameter of the event handler. Item
property of this parameter contains information about the table rows and cells
that will get rendered on the page. This is where you can take of the cells
that you want to or do not wantt o show. The only caveat is that you should
know the index of the column that you want to remove. The following code
snippet shows how you can get rid of first column from the grid.
private void OnDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
e.Item.Cells.Remove(e.Item.Cells[0]);
}
|