【问题标题】:Two Columns with same header text in gridview in c#c#中gridview中具有相同标题文本的两列
【发布时间】:2017-03-23 07:19:16
【问题描述】:

我有一个网格视图,我不想在其中显示两列的标题文本,并且两列应该具有相同的标题名称 (INFA)。 Gridview 看起来像:

<asp:GridView ID="GridView6" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="DayOfWeek" HeaderText="" ItemStyle-Width="30" />
        <asp:BoundField DataField="DateOfMonth" HeaderText="" ItemStyle-Width="30" />
        <asp:BoundField DataField="Emp_Name" HeaderText="INFA" ItemStyle-Width="30" />
        <asp:BoundField DataField="Group_Name" HeaderText="INFA" ItemStyle-Width="30" />
        <asp:BoundField DataField="Emp_Id" HeaderText="Mainframe" ItemStyle-Width="30" />
    </Columns>
</asp:GridView>

我需要翻转这个 gridview 并将行变成列,将列变成行。当我在所有数据字段的标题文本中使用不同的名称时,逻辑工作正常。但在我的要求中,我不需要两列的标题文本,并且两列必须具有相同的标题文本(如 Gridview 中所示)。当我运行没有列名的逻辑时,如上所示,我收到此错误:

Exception Details: System.Data.DuplicateNameException: A column named '&nbsp;' already belongs to this DataTable.

我的逻辑是:

protected void btnConvert_Data()
{
    System.Data.DataTable dt = new System.Data.DataTable("GridView_Data");

    foreach (TableCell cell in GridView6.HeaderRow.Cells)
    {
        if (cell.Text == "")
        {
            dt.Columns.Add("");
        }
        else
        {
            dt.Columns.Add(cell.Text);
        }
    }

    dt.Rows.Add("IST Hours");
    //dt.Rows.Add("8:45AM-6PM");
    foreach (GridViewRow row in GridView6.Rows)
    {
        dt.Rows.Add();
        for (int i = 0; i < row.Cells.Count; i++)
        {
            dt.Rows[dt.Rows.Count - 1][i] = row.Cells[i].Text;
        }
    }

    gvColumnsAsRows.DataSource = FlipDataTable(dt);
    gvColumnsAsRows.DataBind();
    gvColumnsAsRows.HeaderRow.Visible = false;
}

将行翻转为列,反之亦然:

public static System.Data.DataTable FlipDataTable(System.Data.DataTable dt)
{
    System.Data.DataTable table = new System.Data.DataTable();
    //Get all the rows and change into columns
    for (int i = 0; i <= dt.Rows.Count; i++)
    {
        table.Columns.Add(Convert.ToString(i));
    }
    DataRow dr;
    //get all the columns and make it as rows
    for (int j = 0; j < dt.Columns.Count; j++)
    {
        dr = table.NewRow();
        dr[0] = dt.Columns[j].ToString();
        for (int k = 1; k <= dt.Rows.Count; k++)
            dr[k] = dt.Rows[k - 1][j];
        table.Rows.Add(dr);
    }

    return table;
}

我在 dt.Columns.Add(cell.Text); 行的 btnConvert_Data() 中遇到上述错误。谁能帮我解决这个问题?

我翻转 Gridview 时的最终输出应如下所示:

【问题讨论】:

  • 谁能帮我解决这个问题?

标签: c# asp.net gridview


【解决方案1】:

列标题文本和列名不同,哪个列名在DataTable中必须是唯一的。

因此,例如,您可以为没有列标题文本的列指定不同的随机名称

此外,您需要检测 Header 是否是真正的 string.Empty 或看起来像空的东西,在您的情况下,标题文本永远不是空值,它是 &amp;nbsp;

所以你的逻辑永远不会命中,我的意思是cell.Text=="" 将始终返回 false

这就是解决办法

foreach (TableCell cell in GridView6.HeaderRow.Cells)
{
    if (cell.Text == "&nbsp;")
    {
        dt.Columns.Add(Guid.NewGuid().ToString()); //just some random value, i use guid, you can use anything you like to keep it unique.
    }
    else
    {
        dt.Columns.Add(cell.Text);
    }
}

【讨论】:

  • 但我不想要任何价值。也没有随机值。它应该是空的。
  • 这对于列名是不可能的。它必须是唯一的。但不需要显示,可以在DataGrid控件中单独设置列标题文字
【解决方案2】:

您需要确保每列都有唯一的名称。您可以通过检查列名是否存在来做到这一点,如果存在,则通过将索引添加到列名来使其成为唯一的。

for (int i = 0; i < GridView1.HeaderRow.Cells.Count; i++)
{
    string cellText = GridView1.HeaderRow.Cells[i].Text;

    //check if the column name exists and if so make it unique
    if (dt.Columns.Contains(cellText))
    {
        dt.Columns.Add(cellText + "_" + i);
    }
    else
    {
        dt.Columns.Add(cellText);
    }
}

【讨论】:

  • 但这显示的是  我不需要。还有其他选择吗?
  • 我的 sn-p 中没有 &amp;nbsp;。但是,您必须以一种或另一种方式使列名独一无二。
  • 当我将此数据导出到 Excel 时,它给出了  
  • 然后在添加到列之前将其删除。
猜你喜欢
  • 2014-08-12
  • 2013-09-30
  • 1970-01-01
  • 2017-08-30
  • 1970-01-01
  • 2012-10-12
  • 1970-01-01
  • 2018-12-13
  • 1970-01-01
相关资源
最近更新 更多