I am making a SharePoint web part in which I am using System.Web.UI.WebControls.DataGrid control. Think of web part as some component you develop in Net.
Its AutoGenerateColumns property is set to TRUE.
I am trying to hide a column at run time. I have written the following code on this controls ItemCreated event but it only works if I e.Item.Cells[0] and it doesn't work for any other value for e.g. e.Item.Cells[1] and e.Item.Cells[6].
There are 9 columns in my DataGrid control.
Code
protected void grd1_ItemCreated(object sender, DataGridItemEventArgs e)
{
e.Item.Cells[0].Visible = false; //works fine
e.Item.Cells[1].Visible = false; //gives error
e.Item.Cells[2].Visible = false; //gives error
}
Error
Specified argument was out of the range of valid values.
Parameter name: index
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
How to hide a particular column?
try like this
<asp:DataGrid ID="dgShowTiming"
runat="server" OnItemCreated="dgShowTiming_OnItemCreated"
AutoGenerateColumns="false">
protected void dgShowTiming_OnItemCreated(object sender, DataGridItemEventArgs e)
{
MyDataGrid.Columns[0].Visible = false;
// 0=represents your first column
}
You can make visible= true,false
by using Columns[index]
and its settting index