C# c中gridview中标题文本相同的两列#
我有一个gridview,其中我不想显示两列的标题文本,两列应该具有相同的标题名称(INFA)。Gridview看起来像:C# c中gridview中标题文本相同的两列#,c#,asp.net,gridview,C#,Asp.net,Gridview,我有一个gridview,其中我不想显示两列的标题文本,两列应该具有相同的标题名称(INFA)。Gridview看起来像: <asp:GridView ID="GridView6" runat="server" AutoGenerateColumns="false"> <Columns> <asp:BoundField DataField="DayOfWeek" HeaderText="" ItemStyle-Width="30" />
<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>
我的逻辑是:
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;
}
受保护的无效btnConvert_数据()
{
System.Data.DataTable dt=新的System.Data.DataTable(“GridView_数据”);
foreach(GridView6.HeaderRow.Cells中的TableCell单元格)
{
如果(cell.Text==“”)
{
dt.列。添加(“”);
}
其他的
{
dt.Columns.Add(cell.Text);
}
}
dt.行添加(“IST小时”);
//增加(上午8:45-下午6:00);
foreach(GridView6.Rows中的GridViewRow行)
{
dt.Rows.Add();
对于(int i=0;i
要将行翻转为列,或将行翻转为列,请执行以下操作:
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;
}
公共静态System.Data.DataTable FlipDataTable(System.Data.DataTable dt)
{
System.Data.DataTable table=新的System.Data.DataTable();
//获取所有行并将其更改为列
对于(int i=0;i列标题文本和列名不同,在数据表中哪个列名必须是唯一的
例如,您可以为没有列标题文本的列指定不同的随机名称
此外,您还需要检测标题是否为真正的字符串。空或看起来像空的东西,在您的情况下,标题文本永远不是空值,而是
因此,您的逻辑永远不会命中,我的意思是单元格。Text=''
将始终返回false
这就是解决方案
foreach (TableCell cell in GridView6.HeaderRow.Cells)
{
if (cell.Text == " ")
{
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);
}
}
列标题文本和列名不同,在数据表中,列名必须是唯一的
例如,您可以为没有列标题文本的列指定不同的随机名称
此外,您还需要检测标题是否为真正的字符串。空或看起来像空的东西,在您的情况下,标题文本永远不是空值,而是
因此,您的逻辑永远不会命中,我的意思是单元格。Text=''
将始终返回false
这就是解决方案
foreach (TableCell cell in GridView6.HeaderRow.Cells)
{
if (cell.Text == " ")
{
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);
}
}
您需要确保每个列都有一个唯一的名称。您可以通过检查列名是否存在来做到这一点,如果存在,则通过将索引添加到列名来使其成为唯一的列名
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);
}
}
for(int i=0;i
您需要确保每一列都有一个唯一的名称。您可以通过检查列名是否存在来做到这一点,如果存在,则通过向列名添加索引来使其成为唯一的列名
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);
}
}
for(int i=0;i
但我不需要任何值。也不需要随机值。它应该是空的。这对于列名是不可能的。它必须是唯一的。但您不需要显示它,您可以在DataGrid控件中单独设置列标题文本,但我不需要任何值。也不需要随机值。它应该是空的。对于列名来说,这是不可能的。它需要不要是唯一的。但是你不需要显示它,你可以在DataGrid控件中单独设置列标题文本。有人能帮我吗?有人能帮我吗?但这是我不需要的。还有其他选项吗?我的代码段中没有
。但是无论如何,你必须使列名称唯一。When我将此数据导出到Excel,然后在将其添加到列之前将其删除。但这显示了我不需要的内容。还有其他选项吗?在我的代码段中没有
。但无论如何,您必须使列名称唯一。当我将此数据导出到Excel时,它会给出,然后在将其添加到列之前将其删除。