如何将多列值合并为一列?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    |
-----------------------------------------

目前,我设法得到了这个,请参见屏幕截图 在此处输入图片说明

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:

在此处输入图片说明

我将尝试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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

asp.net C# Gridview 嵌套 gridview

如何将值存储在gridview asp.net C#中的超链接所选择的行中?

C# 在 asp.net 中禁用 GridView 标头

ASP.NET C# GridView 分页选项

在 RowUpdating (ASP.NET GridView) C# 期间获取 GridView 中标签的旧值

如何在GridView Asp.net C#中将单独的查询绑定到单独的列

使用javaScript / jquery客户端在asp.net c#中的gridview计算中的gridview

如何在ASP.NET C#的Gridview中获取和显示特定值

c#使用asp.net在gridview页脚中显示总列数

在gridview单元格工具提示ASP.net/C#中显示列的名称

如何使用 ASP.NET C# 将图像动态添加到 Gridview Databound

GridView中的DropDownList绑定到ASP.NET C#中GridView内TextBox中DropDown的选定值

将数据从内部网格视图传递到主网格视图(gridview 模板中的 gridview)ASP.net C#

asp net c#使用复选框获取gridview中特定行的值

asp.net C# Gridview 包含数据但只返回单元格值 = ""

使用 asp.net C# 在标签中添加 gridview 的值

ASP.NET C# 将 XML 数据绑定到 GridView

在Asp.net C#中仅将Gridview数据导出为Excel格式

在将文件/列表绑定到gridview asp.net C#之前先对其进行排序

将gridview的所有行保存到数据库asp.net c#

foreach 循环只获取最后一条记录到 gridview c# asp.net

如何防止重复数据插入到 C# ASP.NET 中的 Gridview

如何在ASP.NET C#应用程序中向GridView添加行

如何在asp .net和c#中的gridview中启用选定的行

ASP.NET C#如何在GridView中更改数据并写入XML

查找“&nbsp;”时如何从“ while”语句退出 在gridView asp.net c#中

如何在ASP.Net C#中的GridView的EditMode中显示DropdownList?

如何在ASP.NET C#中从SQL Server到GridView中选择数据

如何使用 C# asp.net 在 GridView 中添加文件上传日期和/或时间?