5

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?

Chris Stillwell
  • 10,266
  • 10
  • 67
  • 77
daidai
  • 531
  • 5
  • 10
  • 22

3 Answers3

8

This may work for you:

DataTable data;
data.Columns[0].ColumnMapping = MappingType.Hidden;
Linh Vu
  • 130
  • 1
  • 3
1

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;
}
Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232
0

Try this:

 protected void  GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
 {
      e.Row.Cells[0].Visible = false;
 }
demonplus
  • 5,613
  • 12
  • 49
  • 68
bala
  • 36
  • 2