GridView column invisible

B

bbawa1

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
 
G

Guest

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


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
 
B

bbawa1

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



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
 
G

Guest

Hi there again,

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


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



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
 
B

bbawa1

Hi there again,

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



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
:
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;

}
 
G

Guest

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


Hi there again,

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



On Jun 22, 2:56 am, Milosz Skalecki [MCAD] <[email protected]>
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
:
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;

}
 
Joined
May 10, 2008
Messages
2
Reaction score
0
Thanks Milosz Skalecki :)

This had been quite a big problem for me too. I couldn't find a proper solution to manage column properties while GridView1.AutoGenerateColumns was set to true. (And I had to set it to true.)
So I defined all columns dynamically in C#. But I had many problems with that.
Your Solution is the simplest answer! Using AutoGenerateColumns set to true and still manage column (or cell) properties easily :) Thank you so much!

I still don't understand how people like you, can find solutions to problems easily like the one you wrote. Probably reading more books (about ASP.NET & C#) than I do :)
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,222
Members
46,810
Latest member
Kassie0918

Latest Threads

Top