2

An error popped up upon running the application.

I'm trying to set the Width of the First Column of my GridView but i can't do it.

Rows, Columns, Data of this GridView is not bounded to any DataSource.

//By Class Statistics

int A1Available = get.countAvailA1();
int A1Alloted = get.countUnavailA1();
int B1Available = get.countAvailB1();
int B1Alloted = get.countUnavailB1();
int B2Available = get.countAvailB2();
int B2Alloted = get.countUnavailB2();
int C1Available = get.countAvailC1();
int C1Alloted = get.countUnavailC1();

DataTable dtClass = new DataTable();
dtClass.Columns.Add("Class");
dtClass.Columns.Add("A1");
dtClass.Columns.Add("B1");
dtClass.Columns.Add("B2");
dtClass.Columns.Add("C1");

DataRow r;
r = dtClass.NewRow();
r["Class"] = "Number of Available Beds";
r["A1"] = A1Available.ToString();
r["B1"] = B1Available.ToString();
r["B2"] = B2Available.ToString();
r["C1"] = C1Available.ToString();
dtClass.Rows.Add(r);

r = dtClass.NewRow();
r["Class"] = "Number of Unavailable Beds";
r["A1"] = A1Alloted.ToString();
r["B1"] = B1Alloted.ToString();
r["B2"] = B2Alloted.ToString();
r["C1"] = C1Alloted.ToString();
dtClass.Rows.Add(r);

bedStats.DataSource = dtClass;
bedStats.DataBind();   
bedStats.Columns[1].HeaderStyle.Width = new Unit(55, UnitType.Percentage);

Used this code to set the Width. Is there any other way to? Doesn't have be bothered about the value, just setting the Width..

bedStats.Columns[1].HeaderStyle.Width = new Unit(55, UnitType.Percentage);

Image of Error

<code>enter image description here</code>

2
  • Looking at the screenshot, bedStats.Columns[1] seems to be the issue. If you but a break point on this line, and inspect the bedStats columns before trying to access the second column ([1]). What do you see? Commented Feb 4, 2017 at 9:47
  • I added a break point, looking at autos, may i ask what is needed? @FredrikLundin
    – domster
    Commented Feb 4, 2017 at 10:18

1 Answer 1

1

Setting column values will only work with TemplateField and BoundField columns. Autogenerated columns are not part the column collection in the GridView. If you want to color the Headers you need to use the OnRowDataBound event. Only then you can access the columns.

protected void bedStats_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.Header)
    {
        e.Row.Cells[1].Width = new Unit(55, UnitType.Percentage);
        e.Row.Cells[1].BackColor = Color.Pink;
    }
}
0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.