首先,我不知道这是否可行,正确的方法甚至行得通,但是我希望你们能帮助我,我将尽力解释:
我在ASPX页面上有一个GridView控件:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="True" OnRowDataBound="GridView1_RowDataBound" GridLines="None" CssClass="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,如下所示:
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++;
}
}
}
}
}
}
所以上面的数据就是这样的。
-----------------------------------------
| | 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 |
-----------------------------------------
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:
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:
我将尝试fnostro的建议,通过DataTable管理排序部分,然后将数据重新绑定到GridView1并保持发布状态。再次感谢您的回答。
您可以尝试以下方法:
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;
}
}
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句