Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide column in data table

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?

like image 849
daidai Avatar asked Mar 22 '23 21:03

daidai


2 Answers

This may work for you:

DataTable data;
data.Columns[0].ColumnMapping = MappingType.Hidden;
like image 95
Linh Vu Avatar answered Apr 06 '23 00:04

Linh Vu


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;
}
like image 34
Mike Perrenoud Avatar answered Apr 05 '23 23:04

Mike Perrenoud