Reputation: 541
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?
Upvotes: 6
Views: 41317
Reputation: 53
I received the "index out of range" error, then I realized I had to set the DataGridView.DataSource
to the DataTable
first:
// Set DataSource to DataTable
this.dataGridViewIncome.DataSource = dataResult;
this.dataGridViewIncome.Columns[0].Visible = false;
Upvotes: 0
Reputation: 36
Try this:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[0].Visible = false;
}
Upvotes: 0
Reputation: 150
This may work for you:
DataTable data;
data.Columns[0].ColumnMapping = MappingType.Hidden;
Upvotes: 10
Reputation: 67898
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;
}
Upvotes: 2