[英]How to merge multiple columns values into one column? Asp.net Gridview C#
首先,我不知道這是否可行,正確的方法甚至行得通,但我希望你們能幫助我,我將盡力解釋:
我在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 |
-----------------------------------------
但是,這並不是我真正想看到的,但是可以預期,因為OnRowDataBound代碼塊可能未正確完成。
所以我的問題是:
因此理想的結果是這樣的:
-----------------------------------------
| | 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 |
-----------------------------------------
更新信息
根據ConnorsFan和fnostro提供的答案更新代碼后,兩個答案都是正確的並且可以正常工作,感謝您的幫助。 我仍在學習中,因此選擇ConnorsFan的方法。
collspan現在是正確的,請參見下面的屏幕截圖:
我將嘗試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;
}
}
}
}
}
關於排序。 最好在DataTable中管理排序並簡單地重新綁定到GridView
這是我修改RowDataBound事件以合並數據的方式:
protected void GridView1_RowDataBound( object sender, GridViewRowEventArgs e )
{
if ( e.Row.RowType == DataControlRowType.DataRow )
{
// loop through cells and track span index (si)
for ( int i = 0, si = 0; i < e.Row.Cells.Count; si = i)
{
// compare adjacent cells for like data and hide as needed
while ( ++i < e.Row.Cells.Count && e.Row.Cells[ si ].Text == e.Row.Cells[ i ].Text )
e.Row.Cells[ i ].Visible = false;
// set span and, conditionally, special formatting
if ( ( e.Row.Cells[ si ].ColumnSpan = ( i - si ) ) > 1 )
{
e.Row.Cells[ si ].BackColor = Color.Beige;
e.Row.Cells[ si ].HorizontalAlign = HorizontalAlign.Center;
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.