我正在研究ASP.NET C#。我正在使用数据库中的数据填充gridview。 GridView中有一些与DataTable绑定的列。我蚂蚁用Header Rows对这些列进行分组。我尝试将这些Header Rows添加到GridView中,如下所示(示例代码):
我尝试使用行索引进行操作但没有任何反应。
protected void AddHeaders()
{
GridViewRow vesselInfoRow = new GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert);
TableCell vesselInfotc = new TableCell();
vesselInfotc.Text = "Vessel Information ";
vesselInfotc.Style.Add("text-align", "center");
vesselInfotc.Style.Add("font-size", "medium");
vesselInfotc.Style.Add("font-weight", "bold");
vesselInfotc.Style.Add("background-color", "#b7dee8");
vesselInfotc.Style.Add("color", "black");
vesselInfotc.ColumnSpan = 4;
vesselInfoRow.Cells.Add(vesselInfotc);
this.BerthScoreCardGridView.Controls[0].Controls.AddAt(0, vesselInfoRow);
//
GridViewRow vesselArrivalRow = new GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert);
TableCell vesselArrivaltc = new TableCell();
vesselArrivaltc.Text = "Vessel Arrival ";
vesselArrivaltc.Style.Add("text-align", "center");
vesselArrivaltc.Style.Add("font-size", "medium");
vesselArrivaltc.Style.Add("font-weight", "bold");
vesselArrivaltc.Style.Add("background-color", "#b7dee8");
vesselArrivaltc.Style.Add("color", "black");
vesselArrivaltc.ColumnSpan = 2;
vesselArrivalRow.Cells.Add(vesselArrivaltc);
this.BerthScoreCardGridView.Controls[0].Controls.AddAt(0, vesselArrivalRow);
}
然而,行如下所示放在彼此之上:
如何将它们水平放置在彼此旁边。感谢您的帮助。
答案 0 :(得分:0)
您应该将所有单元格添加到同一行,并将该单行添加到GridView:
protected void AddHeaders()
{
GridViewRow topHeaderRow = new GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert);
TableCell vesselInfotc = new TableCell();
vesselInfotc.Text = "Vessel Information ";
...
vesselInfotc.ColumnSpan = 4;
topHeaderRow.Cells.Add(vesselInfotc);
TableCell vesselArrivaltc = new TableCell();
vesselArrivaltc.Text = "Vessel Arrival ";
...
vesselArrivaltc.ColumnSpan = 2;
topHeaderRow.Cells.Add(vesselArrivaltc);
// Add the other cells here
...
BerthScoreCardGridView.Controls[0].Controls.AddAt(0, topHeaderRow);
}
并在GridView的PreRender
事件中调用您的函数:
void BerthScoreCardGridView_PreRender(object sender, EventArgs e)
{
AddHeaders();
}