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.