gpt4 book ai didi

c# - GridView 在 C# 中添加第二个和第三个标题行

转载 作者:行者123 更新时间:2023-11-30 23:07:52 30 4
gpt4 key购买 nike

我有一个 ASP.NET c# GridView,它的列如下所示:

| Foo | Bar | Total1 | Total2 | Total3 |

是否可以在看起来像这样的行上创建标题?

|  General  |  Totals   | 
| | A | B | C |
| Foo | Bar | 1 | 2 | 3 |

我尝试过使用RowCreated 方法,但我停止了:

|  General  |  Totals   |  
| Foo | Bar | 1 | 2 | 3 |

下面是我的代码。

你能帮帮我吗?

在此先感谢您的帮助,非常感谢。

        protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{

GridView ProductGrid = (GridView)sender;
GridViewRow HeaderRow = new GridViewRow(0, 0,
DataControlRowType.Header, DataControlRowState.Insert);

TableCell HeaderCell = new TableCell();
HeaderCell.Text = "General";
HeaderCell.HorizontalAlign = HorizontalAlign.Center;
HeaderCell.ColumnSpan = 2;
HeaderCell.CssClass = "HeaderStyle";
HeaderRow.Cells.Add(HeaderCell);
ProductGrid.Controls[0].Controls.AddAt(0, HeaderRow);

HeaderCell = new TableCell();
HeaderCell.Text = "Totals";
HeaderCell.HorizontalAlign = HorizontalAlign.Center;
HeaderCell.ColumnSpan = 3;
HeaderCell.CssClass = "HeaderStyle";
HeaderRow.Cells.Add(HeaderCell);

}
}

最佳答案

这就是您可以做到的。此代码段以编程方式添加所有 3 个标题行。但您也可以使用普通的 GridView header 作为第 3 行。所以对于我的示例,gridview 应该看起来像这样并且有 5 列

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"
OnRowCreated="GridView1_RowCreated" ShowHeader="false">

然后是RowCreated方法

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
//cast the sender back to a gridview
GridView gv = sender as GridView;

//check if the row is the header row
if (e.Row.RowType == DataControlRowType.Header)
{
//create the first row
GridViewRow extraHeader1 = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert);
extraHeader1.BackColor = Color.LightSalmon;

TableCell cell1 = new TableCell();
cell1.ColumnSpan = 2;
cell1.Text = "General";
extraHeader1.Cells.Add(cell1);

TableCell cell2 = new TableCell();
cell2.ColumnSpan = 3;
cell2.Text = "Totals";
extraHeader1.Cells.Add(cell2);

//create the second row
GridViewRow extraHeader2 = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert);
extraHeader2.BackColor = Color.LightGreen;

TableCell cell3 = new TableCell();
cell3.ColumnSpan = 2;
extraHeader2.Cells.Add(cell3);

TableCell cell4 = new TableCell();
cell4.Text = "A";
extraHeader2.Cells.Add(cell4);

TableCell cell5 = new TableCell();
cell5.Text = "B";
extraHeader2.Cells.Add(cell5);

TableCell cell6 = new TableCell();
cell6.Text = "C";
extraHeader2.Cells.Add(cell6);

//create the third row
GridViewRow extraHeader3 = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert);
extraHeader3.BackColor = Color.LightBlue;

//loop all the columns and create a new cell for each
for (int i = 0; i < gv.Columns.Count; i++)
{
TableCell cell = new TableCell();
if (i == 0)
cell.Text = "Foo";
else if (i == 1)
cell.Text = "Bar";
else
cell.Text = (i - 1).ToString();

extraHeader3.Cells.Add(cell);
}

//add the new rows to the gridview
gv.Controls[0].Controls.AddAt(0, extraHeader3);
gv.Controls[0].Controls.AddAt(0, extraHeader2);
gv.Controls[0].Controls.AddAt(0, extraHeader1);
}
}

关于c# - GridView 在 C# 中添加第二个和第三个标题行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46906327/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com