C# 如何使用ASP.NETGridView实现多列和多标题行?
我的情况是,我们需要使用ASP.NETGridView实现多个colspan和两个标题行 像下面这样C# 如何使用ASP.NETGridView实现多列和多标题行?,c#,asp.net,gridview,html-table,C#,Asp.net,Gridview,Html Table,我的情况是,我们需要使用ASP.NETGridView实现多个colspan和两个标题行 像下面这样 +----------+--------------------------------+--------------------------------+ |Name |English |Math | | |----------+----------+-------
+----------+--------------------------------+--------------------------------+
|Name |English |Math |
| |----------+----------+----------+----------+----------+----------+
| |1st Term |2nd Term |3rd Term |1st Term |2nd Term |3rd Term |
+----------+----------+----------+----------+----------+----------+----------+
|Adam |50 |60 |70 |55 |65 |75 |
+----------+----------+----------+----------+----------+----------+----------+
|Smith |52 |62 |72 |57 |68 |70 |
+----------+----------+----------+----------+----------+----------+----------+
有可能吗
编辑:标题的行数和/或列数,甚至子标题数都不是固定的。通过
单元格
属性访问网格视图的行span
和列span
要访问标题单元格,请执行以下操作:
//Replace ColumnSpan with RowSpan if needed (and if you can get multiple header rows)
Gridview1.HeaderRow.Cells[CELL_INDEX].ColumnSpan = 2; //Need to know the cell index
要访问普通行的单元格,请执行以下操作:
//Replace ColumnSpan with RowSpan if needed
Gridview1.Rows[ROW_INDEX].Cells[CELL_INDEX].ColumnSpan = 2; //Need to know the row index and the cell index in that row
多标题行可能有点棘手。我从来没有这样做过,但这里的人似乎有一个很好的答案:这是可以做到的,但需要一些尝试和错误才能得到你想要的设计。使用GridView
OnRowDataBound
事件。在生成GridView之后,这样做会更容易,尤其是对于行span
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
int spanColumn_1_start = 1;
int spanColumn_1_length = 3;
//apply colspan
e.Row.Cells[spanColumn_1_start].ColumnSpan = 3;
//remove the spanned cells
for (int i = 1; i < spanColumn_1_length; i++)
{
e.Row.Cells.RemoveAt(spanColumn_1_start + 1);
}
//note that the startindex of the 2nd colspan is set after removing cells for 1st colspan
int spanColumn_2_start = 2;
int spanColumn_2_length = 3;
//apply colspan
e.Row.Cells[spanColumn_2_start].ColumnSpan = 3;
//remove the spanned cells
for (int i = 1; i < spanColumn_2_length; i++)
{
e.Row.Cells.RemoveAt(spanColumn_2_start + 1);
}
}
else if (e.Row.RowType == DataControlRowType.DataRow)
{
int rowIndex = e.Row.DataItemIndex;
//to make a rowspan you have to work backwards since the next row does not exist yet
if (rowIndex == 1)
{
GridView1.Rows[rowIndex - 1].Cells[0].RowSpan = 2;
e.Row.Cells.RemoveAt(0);
}
//span 4 rows in column 3 starting at row 6
if (rowIndex == 9)
{
int rowSpan = 4;
int columnIndex = 3;
//apply rowspan
GridView1.Rows[rowIndex - rowSpan].Cells[columnIndex].RowSpan = rowSpan;
//remove the spanned rows
for (int i = 1; i < rowSpan; i++)
{
GridView1.Rows[rowIndex - (rowSpan - i)].Cells.RemoveAt(columnIndex);
}
}
}
}
我也看到了答案。但我的问题是我在运行时获取所有列。因此,我甚至不知道存在多少列的列的名称。@ParvezMRobin我找到了一个用于动态标题的列。希望这能帮助你:事实上这不是我所要求的。我想实现多标题行。但是您所做的是创建一个具有多个colspan的单标题行。是的,我需要这个。但另外,我还需要一个包含每列标题的标题行。您现在知道如何使用我的代码段执行此操作,只需稍微调整一下即可。@VDWWD从您的代码段中根本看不出如何添加额外的标题行添加代码段以创建额外的标题行。
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 a new row
GridViewRow extraHeader = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert);
extraHeader.BackColor = Color.Green;
//loop all the columns and create a new cell for each
for (int i = 0; i < gv.Columns.Count; i++)
{
TableCell cell = new TableCell();
cell.Text = "ExtraHeader " + i;
//add the cell to the new header row
extraHeader.Cells.Add(cell);
}
//add the new row to the gridview
gv.Controls[0].Controls.AddAt(0, extraHeader);
}
}