运行应用程序时弹出错误。
我试图设置GridView的第一列的宽度,但我无法做到。
行,列,此GridView的数据不受任何数据源限制。
//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);
使用此代码设置宽度。还有其他办法吗?没有关心这个值,只需设置宽度..
bedStats.Columns[1].HeaderStyle.Width = new Unit(55, UnitType.Percentage);
错误图片
答案 0 :(得分:1)
设置列值仅适用于TemplateField
和BoundField
列。自动生成的列不是GridView中的列集合的一部分。如果要为标题着色,则需要使用OnRowDataBound
事件。只有这样你才能访问这些列。
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;
}
}