如何将多个列值合并到一列中? Asp.net Gridview C# [英] How to merge multiple columns values into one column? Asp.net Gridview C#
问题描述
我在ASPX页面上有一个GridView控件:
< asp:GridView ID =GridView1 runat =serverAutoGenerateColumns =TrueOnRowDataBound =GridView1_RowDataBoundGridLines =NoneCssClass =table table-striped/>
我在代码隐藏中创建了一个DataTable,它包含以下数据并将其绑定到Gridview控制:
------------------------ -----------------
| | Name1 | Name2 | Name3 | Name4 |
-----------------------------------------
| Row1 | 1 | 1 | 1 | 1b |
| Row2 | 1a | 2b | 2b | 4b |
| Row3 | 2a | 2c | 2a | 2a |
| Row4 | 1d | 1d | 1d | 4d |
| Row5 | 1e | 1e | 1e | 1e |
| Row6 | 1f | 2f | 3f | 4f |
-----------------------------------------
现在我想匹配要合并的列值并添加适当的colspan。我已经添加到GridView控件的一个OnRowDataBound如下:
pre $ protected void GridView1_RowDataBound(object sender,GridViewRowEventArgs e)
{
if(e.RowType == DataControlRowType.DataRow)
{
if(e.Row.RowIndex> = 0)
{
int colSpanValue = 2;
for(int i = 0; i< e.Row.Cells.Count; i ++)
{
if(i + 1< e.Row.Cells.Count)
{
if(e.Row.Cells [i] .Text == e.Row.Cells [i + 1] .Text)
{
e.Row.Cells [i ] .BackColor = Color.Beige;
e.Row.Cells [i] .ColumnSpan = colSpanValue;
e.Row.Cells [i] .HorizontalAlign = HorizontalAlign.Center;
e.Row.Cells [i + 1] .Visible = false;
colSpanValue ++;
}
}
}
}
}
}
所以上面的数据会是这样的,像这样。
----- ------------------------------------
| | Name1 | Name2 | Name3 | Name4 |
-----------------------------------------
| Row1 | 1 | 1b | <! - 问题
| Row2 | 1a | 2b | 4b |
| Row3 | 2a | 2c | 2a | <! - 问题
| Row4 | 1d | 4d |
| Row5 | 1e |
| Row6 | 1f | 2f | 3f | 4f |
-----------------------------------------
目前我设法弄到这个,看截图
然而,这并不是我真正想看到的,但可以预期,因为OnRowDataBound代码块可能没有做到正确。
所以我的问题是:
- 我怎样才能合并一行中的所有相等的列并向它们添加colspan? / li>
- 现在对于棘手的位,我是否可以对列进行排序,以便匹配的列单元格能够正确显示? (对此不确定)
所以理想的结果是这样的:
-------------------------------------- ---
| | Name1 | Name2 | Name3 | Name4 |
-----------------------------------------
| Row1 | 1 | 1b | <! - 问题
| Row2 | 1a | 2b | 4b |
| Row3 | 2a | 2c | 2a | <! - 问题
| Row4 | 1d | 4d |
| Row5 | 1e |
| Row6 | 1f | 2f | 3f | 4f |
-----------------------------------------
更新资讯
$ b 更新后代码根据 ConnorsFan 和 fnostro 提供的答案,两种答案都是正确的并且可行,谢谢你的帮助。
我选择ConnorsFan的方法,因为我还在学习。
大小写正确无误,请参阅下面的截图:
我会尝试 fnostro's 建议通过DataTable管理排序部分,并将数据重新绑定到GridView1并保持发布。再次感谢您的回答。
您可以试试这个:
protected void GridView1_RowDataBound(object sender,GridViewRowEventArgs e)
{
if(e.RowType == DataControlRowType.DataRow)
{
for(int i = 0; i< e.Row.Cells.Count - 1; i ++)
{
TableCell cell = e.Row.Cells [i];
if(cell.Visible)
{
int colSpanValue = 1;
for(int j = i + 1; j {
TableCell otherCell = e.Row.Cells [j] ;
if(otherCell.Text == cell.Text)
{
colSpanValue ++;
otherCell.Visible = false;
}
else
{
break;
}
}
if(colSpanValue> 1)
{
cell.ColumnSpan = colSpanValue;
cell.BackColor = Color.Beige;
cell.HorizontalAlign = HorizontalAlign.Center;
}
}
}
}
}
First of I don't know if this is possible, the correct way to go or even will work, but I hope you guys can help me out, I will try to explain:
I have a GridView Control on the ASPX page:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="True" OnRowDataBound="GridView1_RowDataBound" GridLines="None" CssClass="table table-striped" />
I created a DataTable in the code-behind which holds the following data and have bind it to the Gridview control:
-----------------------------------------
| | Name1 | Name2 | Name3 | Name4 |
-----------------------------------------
| Row1 | 1 | 1 | 1 | 1b |
| Row2 | 1a | 2b | 2b | 4b |
| Row3 | 2a | 2c | 2a | 2a |
| Row4 | 1d | 1d | 1d | 4d |
| Row5 | 1e | 1e | 1e | 1e |
| Row6 | 1f | 2f | 3f | 4f |
-----------------------------------------
Now I would like matching column values to be merged and add the appropriate colspan. I have added to the GridView control a OnRowDataBound as following:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowIndex >= 0)
{
int colSpanValue = 2;
for (int i = 0; i < e.Row.Cells.Count; i++)
{
if (i+1 < e.Row.Cells.Count)
{
if (e.Row.Cells[i].Text == e.Row.Cells[i + 1].Text)
{
e.Row.Cells[i].BackColor = Color.Beige;
e.Row.Cells[i].ColumnSpan = colSpanValue;
e.Row.Cells[i].HorizontalAlign = HorizontalAlign.Center;
e.Row.Cells[i + 1].Visible = false;
colSpanValue++;
}
}
}
}
}
}
so the above data would like this, something like this.
-----------------------------------------
| | Name1 | Name2 | Name3 | Name4 |
-----------------------------------------
| Row1 | 1 | 1b | <!-- problem
| Row2 | 1a | 2b | 4b |
| Row3 | 2a | 2c | 2a | <!-- problem
| Row4 | 1d | 4d |
| Row5 | 1e |
| Row6 | 1f | 2f | 3f | 4f |
-----------------------------------------
Currently I managed to get this, see screenshot
However this is not really what i would like to see, but would expect as the OnRowDataBound code block is probably not done the right.
So my question is:
- How could I merge all equal columns in a row and add colspan to them?
- Now for the tricky bit, would I be able to sort the columns so, that matching column cells are displayed properly? (not sure about this)
So the ideal result would be like this:
-----------------------------------------
| | Name1 | Name2 | Name3 | Name4 |
-----------------------------------------
| Row1 | 1 | 1b | <!-- problem
| Row2 | 1a | 2b | 4b |
| Row3 | 2a | 2c | 2a | <!-- problem
| Row4 | 1d | 4d |
| Row5 | 1e |
| Row6 | 1f | 2f | 3f | 4f |
-----------------------------------------
UPDATED INFORMATION
After updating the code according to the answers provided by ConnorsFan and fnostro, both answers are correct and work, thank you for the help. I choose for ConnorsFan's approach, as i'm still learning.
The collspan's are now correct, see screenshot below:
I will try to fnostro's advise to manage the sorting part via the DataTable and rebind the data to the GridView1 and keep you posted. once again thank you for your answers.
You can try this:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
for (int i = 0; i < e.Row.Cells.Count - 1; i++)
{
TableCell cell = e.Row.Cells[i];
if (cell.Visible)
{
int colSpanValue = 1;
for (int j = i + 1; j < e.Row.Cells.Count; j++)
{
TableCell otherCell = e.Row.Cells[j];
if (otherCell.Text == cell.Text)
{
colSpanValue++;
otherCell.Visible = false;
}
else
{
break;
}
}
if (colSpanValue > 1)
{
cell.ColumnSpan = colSpanValue;
cell.BackColor = Color.Beige;
cell.HorizontalAlign = HorizontalAlign.Center;
}
}
}
}
}
这篇关于如何将多个列值合并到一列中? Asp.net Gridview C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!