0

I created a dynamic gridview which I send in an email. But all works except for the headertext. I can't seem to find how i can define the headertext.

My sqlCommand displays 4 columns, but when i use:

 LabelTest.Text = "Init Count: " +grd.Columns.Count;

it shows, Init Count: 0

So i'm trying to do grd.Columns[0].HeaderText = "Something"; to set the headertext but nothing seems to work. I also tried changing the 0 to 3,4,5 but same problem.

The error is:

System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index at System.Collections.ArrayList.get_Item(Int32 index) at..

Any ideas anyone? Thank you.


Edit: How i have created my gridview:

GridView grd = new GridView();

        // Css style voor de gridview

        grd.BorderStyle = System.Web.UI.WebControls.BorderStyle.None;
        grd.GridLines = GridLines.None;
        grd.RowStyle.HorizontalAlign = HorizontalAlign.Center;

        grd.Columns[0].HeaderText = "Something"; //error
        grd.Width = 600;
        LabelTest.Text = "Init Count: " +grd.Columns.Count;

        foreach (DataControlField field in grd.Columns)
        {
            field.ItemStyle.Width = Unit.Percentage(100 / grd.Columns.Count);
        }

        if (sendGrid != null)
        {
            grd.DataSource = sendGrid.ExecuteReader();
            grd.DataBind();
        }

sendGrid is the name of my sqlCommand in which I select 4 columns.

The foreach statement only sets my columns a bit more apart from each other. Other than that i'm not specifying any columns or so to set the headertext.

Dieter
  • 401
  • 1
  • 9
  • 31
  • @Niklas: What do you mean by that? grd is the id of my gridview. `GridView grd = new GridView();` – Dieter Apr 12 '11 at 09:53
  • Don't you need to bind it before you can count it? – Niklas Apr 12 '11 at 11:17
  • Also, this seem to address the same issue: http://stackoverflow.com/questions/686544/any-way-to-manipulate-the-columns-in-gridview-with-autogeneratecolumns-true – Niklas Apr 12 '11 at 11:18
  • Yes, I should have done that first Niklas. I simply changed the names of my database tables. Problem solved aswell. Not really what i wanted to do but ok. – Dieter Apr 12 '11 at 12:56

1 Answers1

0

If you are using the

DataControlField

to add Columns set its

RowHeaderColumn

property and then add it to the GridView.

pcraft
  • 156
  • 1
  • 10
  • I will upload my gridview code, because it's getting too complicated. I'm using non of the 2 u discribed except to set the width of the gridview. – Dieter Apr 12 '11 at 10:25