如何将多列值合并到一列中?Asp.net网格视图C#
首先,我不知道这是否可行,正确的方法是否可行,但我希望你们能帮助我,我会尽力解释: 我在ASPX页面上有一个GridView控件:如何将多列值合并到一列中?Asp.net网格视图C#,c#,asp.net,gridview,C#,Asp.net,Gridview,首先,我不知道这是否可行,正确的方法是否可行,但我希望你们能帮助我,我会尽力解释: 我在ASPX页面上有一个GridView控件: <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="True" OnRowDataBound="GridView1_RowDataBound" GridLines="None" CssClass="table table-striped" /> 现在,我希望合并匹配的列值并添
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="True" OnRowDataBound="GridView1_RowDataBound" GridLines="None" CssClass="table table-striped" />
现在,我希望合并匹配的列值并添加适当的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++;
}
}
}
}
}
}
受保护的无效GridView1\u行数据绑定(对象发送方,GridViewRowEventArgs e)
{
如果(e.Row.RowType==DataControlRowType.DataRow)
{
如果(e.Row.RowIndex>=0)
{
int colSpanValue=2;
for(int i=0;i
所以上面的数据是这样的,类似这样的
-----------------------------------------
| | 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 |
-----------------------------------------
-----------------------------------------
||名称1 |名称2 |名称3 |名称4|
-----------------------------------------
|第1行| 1 | 1b |您可以尝试以下方法:
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;
}
}
}
}
}
受保护的无效GridView1\u行数据绑定(对象发送方,GridViewRowEventArgs e)
{
如果(e.Row.RowType==DataControlRowType.DataRow)
{
对于(int i=0;i1)
{
cell.ColumnSpan=colSpanValue;
cell.BackColor=颜色。米色;
cell.horizontallign=horizontallign.Center;
}
}
}
}
}
关于排序。最好管理数据表中的排序,并简单地重新绑定到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;
}
}
}
}
受保护的无效GridView1\u行数据绑定(对象发送方,GridViewRowEventArgs e)
{
如果(e.Row.RowType==DataControlRowType.DataRow)
{
//循环通过单元和轨道跨度指数(si)
for(int i=0,si=0;i1)
{
e、 Row.Cells[si]。背景色=颜色。米色;
e、 行.单元格[si].HorizontalAlign=HorizontalAlign.Center;
}
}
}
}
非常感谢,这非常有效,我已添加了更新信息,包括您回答结果的屏幕截图。感谢您提供有关列排序的建议,我将看看如何实现这一点。很高兴提供帮助。查看Linq
。这允许我按列中的值进行排序(从上到下),但是是否有办法对行进行排序,例如:行级别从左到右,如果您查看我的更新信息并检查第3行,您将看到以下结果:(2a、2b、2x2a)现在理想情况下,我希望将这些数据分组,但我不确定这是否是正确的方法,因为这将涉及移动整个列,但我可以想象,如果我们对第一行进行正确排序,并转到第二行,我们可能会再次弄乱第一行结果,不确定这是否会起作用。如何分组?这样你有(3x2a,2c)?按行排序将是一件麻烦事,因为通常数据表是按列排序的。您可以滚动自己的按行排序的排序过程。或者您可以“透视”数据表并正常排序。我不知道你总的来说想要完成什么。分组前是否先按列再按行排序?或者是列排序、组排序、行排序、重组的组合???嗨,fnostro,就像你说的:这样你就有了(3x2a,2c)?事实上,我可能需要为这个问题提出另一个问题,正如你提到的,我已经发现,按行排序似乎是一件麻烦事,而且确实注意到按列排序的一切都准备就绪,但是行是另一回事,完全可以理解,这就是为什么我认为带datatable的gridview控件可能不是满足我需求的理想解决方案,其背后的思想是将共享值彼此相邻并合并,以便您可以看到哪些列共享相同的行数据值。谢谢
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;
}
}
}
}
}
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;
}
}
}
}