I have DataTable which I filled using an SQL Query. Then I filled the GridView with the DataTable.
DataTable table = new DataTable();
table.Load(reader);
gvAktivne.DataSource = table;
gvAktivne.DataBind();
This works fine but now I want to hide the first column. When I add this:
gvAktivne.Columns[0].Visible = false;
I get an IndexOutOfRange exception.
Does anybody have an idea how to fix this?
This may work for you:
DataTable data;
data.Columns[0].ColumnMapping = MappingType.Hidden;
Based on the problem statement it appears you have AutoGenerateColumns set to true. This is going to be problematic when you want to hide columns. You need to make sure that you issue that line of code after the DataBind() and I would do it in OnPreRender.
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
gvAktivne.Columns[0].Visible = false;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With