合并单元格的GridView行 [英] colspan gridview rows
问题描述
我添加行到GridView中。有在GridView的20列。我怎样才能在GridView控件做一个合并单元格状的功能,能告诉我下2-3列,其余为2-3列跨度行。
I have added rows into gridview. There are 20 columns in gridview. How can i do a colspan-like feature in gridview which could show me 2-3 rows under 2-3 columns and remaining as a colspan.
基本上我希望实现在GridView控件的行中的GridView合并单元格。
Basically i wish to implement colspan in gridview on the rows of the gridview.
因此,我的present GV就好;
hence my present gv is like ;
西1山口(3,3)山口4 ......山口20
Col 1 Col 2 Col 3 Col 4 ...... Col 20
电池1电池2 CELL3单元4 ...... 20细胞(对于行#1)
Cell1 Cell2 Cell3 Cell 4 ...... Cell 20 (For Rows # 1)
我希望有类似
西1山口(3,3)山口4 ......山口20
Col 1 Col 2 Col 3 Col 4 ...... Col 20
Cell1 Cell2 ...... Cell 20 (For Rows # 1)
让我知道的任何查询。
Let me know for any query.
感谢
推荐答案
您需要处理GridView控件的事件OnRowCreated如下:
You need to handle the OnRowCreated event of the GridView as follows:
protected void grid_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[2].ColumnSpan = 2;
//now make up for the colspan from cell2
e.Row.Cells.RemoveAt(4);
}
}
您的标记应该是这样的:
Your markup should be something like this:
<asp:GridView runat="server" ID="grid" OnRowCreated="grid_RowCreated" >
在上面的例子中,我填充网格与此
On the above example, I populated the grid with this:
DataTable dt = new DataTable();
for (int i = 0; i < 5; i++)
{
dt.Columns.Add("Col " + i);
}
for (int i = 0; i < 10; i++)
{
DataRow r = dt.NewRow();
r.ItemArray=new object[]{"row "+i,"row "+i,"row "+i,"row "+i,"row "+i};
dt.Rows.Add(r);
}
grid.DataSource = dt;
grid.DataBind();
和它产生的:
And it produces this:
我刚刚意识到你想拥有的行(不一定是头部)有一定的合并单元格,在这种情况下,你可以这样做:
I just realized that you wanted to have the ROWS (not necessarily the header) to have certain colspan, in which case you can do:
protected void grid_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[2].ColumnSpan = 2;
//now make up for the colspan from cell2
e.Row.Cells.RemoveAt(4);
}
}
和它会产生:
这篇关于合并单元格的GridView行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!