By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,435 Members | 2,033 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,435 IT Pros & Developers. It's quick & easy.

GridView column invisible

P: n/a
I have a GetData methd which is returning a table using
sqldataadapter. I bound that datasource with GridView but now I want
to invisible Gridview's first column but it gives me following error
although I have 5 columns in my datatable.
Index was out of range. Must be non-negative and less than the size of
the collection.
Parameter name: index
GridView1.DataSource = GetData();
GridView1.DataBind();
GridView1.Columns[1].Visible = false;
Thanks for help me in advance
Jun 22 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Autogenerated columns are not included in the Columns collection. You have to
handle RowDataBound event instead:

protected void GridView1_RowDataBound(object sender,
GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow ||
e.Row.RowType == DataControlRowType.Header ||
e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[1].Visible = false;
}
}

hope this helps
--
Milosz
"bb****@yahoo.com" wrote:
I have a GetData methd which is returning a table using
sqldataadapter. I bound that datasource with GridView but now I want
to invisible Gridview's first column but it gives me following error
although I have 5 columns in my datatable.
Index was out of range. Must be non-negative and less than the size of
the collection.
Parameter name: index

GridView1.DataSource = GetData();

GridView1.DataBind();
GridView1.Columns[1].Visible = false;
Thanks for help me in advance

Jun 22 '07 #2

P: n/a
On Jun 22, 2:56 am, Milosz Skalecki [MCAD] <mily...@DONTLIKESPAMwp.pl>
wrote:
Autogenerated columns are not included in the Columns collection. You have to
handle RowDataBound event instead:

protected void GridView1_RowDataBound(object sender,
GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow ||
e.Row.RowType == DataControlRowType.Header ||
e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[1].Visible = false;
}

}

hope this helps
--
Milosz

"bba...@yahoo.com" wrote:
I have a GetData methd which is returning a table using
sqldataadapter. I bound that datasource with GridView but now I want
to invisible Gridview's first column but it gives me following error
although I have 5 columns in my datatable.
Index was out of range. Must be non-negative and less than the size of
the collection.
Parameter name: index
GridView1.DataSource = GetData();
GridView1.DataBind();
GridView1.Columns[1].Visible = false;
Thanks for help me in advance- Hide quoted text -

- Show quoted text -
Yes it works but I can't hide the name of the Column. Rows are not
showing any data but the column name is still there. So, the column
is still showing with empty rows

Jun 22 '07 #3

P: n/a
Hi there again,

It's working fine for me. Could you paste aspx code for gridview?
--
Milosz
"bb****@yahoo.com" wrote:
On Jun 22, 2:56 am, Milosz Skalecki [MCAD] <mily...@DONTLIKESPAMwp.pl>
wrote:
Autogenerated columns are not included in the Columns collection. You have to
handle RowDataBound event instead:

protected void GridView1_RowDataBound(object sender,
GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow ||
e.Row.RowType == DataControlRowType.Header ||
e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[1].Visible = false;
}

}

hope this helps
--
Milosz

"bba...@yahoo.com" wrote:
I have a GetData methd which is returning a table using
sqldataadapter. I bound that datasource with GridView but now I want
to invisible Gridview's first column but it gives me following error
although I have 5 columns in my datatable.
Index was out of range. Must be non-negative and less than the size of
the collection.
Parameter name: index
GridView1.DataSource = GetData();
GridView1.DataBind();
GridView1.Columns[1].Visible = false;
Thanks for help me in advance- Hide quoted text -
- Show quoted text -

Yes it works but I can't hide the name of the Column. Rows are not
showing any data but the column name is still there. So, the column
is still showing with empty rows

Jun 22 '07 #4

P: n/a
On Jun 22, 2:30 pm, Milosz Skalecki [MCAD] <mily...@DONTLIKESPAMwp.pl>
wrote:
Hi there again,

It's working fine for me. Could you paste aspx code for gridview?
--
Milosz

"bba...@yahoo.com" wrote:
On Jun 22, 2:56 am, Milosz Skalecki [MCAD] <mily...@DONTLIKESPAMwp.pl>
wrote:
Autogenerated columns are not included in the Columns collection. You have to
handle RowDataBound event instead:
protected void GridView1_RowDataBound(object sender,
GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow ||
e.Row.RowType == DataControlRowType.Header ||
e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[1].Visible = false;
}
}
hope this helps
--
Milosz
"bba...@yahoo.com" wrote:
I have a GetData methd which is returning a table using
sqldataadapter. I bound that datasource with GridView but now I want
to invisible Gridview's first column but it gives me following error
although I have 5 columns in my datatable.
Index was out of range. Must be non-negative and less than the size of
the collection.
Parameter name: index
GridView1.DataSource = GetData();
GridView1.DataBind();
GridView1.Columns[1].Visible = false;
Thanks for help me in advance- Hide quoted text -
- Show quoted text -
Yes it works but I can't hide the name of the Column. Rows are not
showing any data but the column name is still there. So, the column
is still showing with empty rows- Hide quoted text -

- Show quoted text -
Thanks again. Here is my code
if (e.Row.RowType == DataControlRowType.DataRow )

if (Convert.ToInt32(e.Row.Cells[5].Text) >=24)
{

e.Row.Cells[0].CssClass = "sdgStatusRed";
e.Row.Cells[5].Visible = false;
e.Row.Cells[5].Width = 0;
}
else
{

if (Convert.ToInt32(e.Row.Cells[5].Text) >= 20)
{

e.Row.Cells[0].CssClass = "sdgStatusOrange";
e.Row.Cells[5].Visible = false;
e.Row.Cells[5].Width = 0;
//GridView1.Columns[5].Visible = false;

}

Jun 22 '07 #5

P: n/a
Hi again,

Take a look again at my example and see you're not handling header and
footer items

e.Row.RowType == DataControlRowType.Header ||
e.Row.RowType == DataControlRowType.Footer

You should be fine from this point

Regards
--
Milosz
"bb****@yahoo.com" wrote:
On Jun 22, 2:30 pm, Milosz Skalecki [MCAD] <mily...@DONTLIKESPAMwp.pl>
wrote:
Hi there again,

It's working fine for me. Could you paste aspx code for gridview?
--
Milosz

"bba...@yahoo.com" wrote:
On Jun 22, 2:56 am, Milosz Skalecki [MCAD] <mily...@DONTLIKESPAMwp.pl>
wrote:
Autogenerated columns are not included in the Columns collection. You have to
handle RowDataBound event instead:
protected void GridView1_RowDataBound(object sender,
GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow ||
e.Row.RowType == DataControlRowType.Header ||
e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[1].Visible = false;
}
}
hope this helps
--
Milosz
"bba...@yahoo.com" wrote:
I have a GetData methd which is returning a table using
sqldataadapter. I bound that datasource with GridView but now I want
to invisible Gridview's first column but it gives me following error
although I have 5 columns in my datatable.
Index was out of range. Must be non-negative and less than the size of
the collection.
Parameter name: index
GridView1.DataSource = GetData();
GridView1.DataBind();
GridView1.Columns[1].Visible = false;
Thanks for help me in advance- Hide quoted text -
- Show quoted text -
Yes it works but I can't hide the name of the Column. Rows are not
showing any data but the column name is still there. So, the column
is still showing with empty rows- Hide quoted text -
- Show quoted text -

Thanks again. Here is my code
if (e.Row.RowType == DataControlRowType.DataRow )

if (Convert.ToInt32(e.Row.Cells[5].Text) >=24)
{

e.Row.Cells[0].CssClass = "sdgStatusRed";
e.Row.Cells[5].Visible = false;
e.Row.Cells[5].Width = 0;
}
else
{

if (Convert.ToInt32(e.Row.Cells[5].Text) >= 20)
{

e.Row.Cells[0].CssClass = "sdgStatusOrange";
e.Row.Cells[5].Visible = false;
e.Row.Cells[5].Width = 0;
//GridView1.Columns[5].Visible = false;

}

Jun 23 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.