GridView column invisible

Discussion in 'ASP .Net' started by bbawa1@yahoo.com, Jun 22, 2007.

  1. Guest

    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, 2007
    #1
    1. Advertising

  2. 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


    "" 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

    >
    >
    =?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=, Jun 22, 2007
    #2
    1. Advertising

  3. Guest

    On Jun 22, 2:56 am, Milosz Skalecki [MCAD] <>
    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
    >
    >
    >
    > "" 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, 2007
    #3
  4. Hi there again,

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


    "" wrote:

    > On Jun 22, 2:56 am, Milosz Skalecki [MCAD] <>
    > 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
    > >
    > >
    > >
    > > "" 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
    >
    >
    =?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=, Jun 22, 2007
    #4
  5. Guest

    On Jun 22, 2:30 pm, Milosz Skalecki [MCAD] <>
    wrote:
    > Hi there again,
    >
    > It's working fine for me. Could you paste aspx code for gridview?
    > --
    > Milosz
    >
    >
    >
    > "" wrote:
    > > On Jun 22, 2:56 am, Milosz Skalecki [MCAD] <>
    > > 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

    >
    > > > "" 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, 2007
    #5
  6. 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


    "" wrote:

    > On Jun 22, 2:30 pm, Milosz Skalecki [MCAD] <>
    > wrote:
    > > Hi there again,
    > >
    > > It's working fine for me. Could you paste aspx code for gridview?
    > > --
    > > Milosz
    > >
    > >
    > >
    > > "" wrote:
    > > > On Jun 22, 2:56 am, Milosz Skalecki [MCAD] <>
    > > > 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

    > >
    > > > > "" 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;
    >
    > }
    >
    >
    =?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=, Jun 23, 2007
    #6
  7. emre2008

    Joined:
    May 10, 2008
    Messages:
    2
    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: May 10, 2008
    emre2008, May 10, 2008
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?am9l?=

    access invisible column inside gridview

    =?Utf-8?B?am9l?=, Sep 28, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    5,322
    Eliyahu Goldin
    Sep 28, 2006
  2. Mariano
    Replies:
    2
    Views:
    472
    =?Utf-8?B?RG91ZyBTYWxvbW9u?=
    Jul 19, 2007
  3. Mel
    Replies:
    9
    Views:
    667
    Hardeep Kaur
    Aug 9, 2007
  4. Andy B
    Replies:
    5
    Views:
    587
    Andy B
    May 29, 2008
  5. Julia B
    Replies:
    2
    Views:
    3,914
    Julia B
    Oct 2, 2008
Loading...

Share This Page