gpt4 book ai didi

asp.net - GridView - 导出到 Excel 时删除编辑和删除列

转载 作者:行者123 更新时间:2023-12-02 12:03:20 24 4
gpt4 key购买 nike

感谢您查看我的问题并提供帮助。我有一个用 C# 构建的 asp.net 应用程序。该应用程序有一个简单的 GridView 。我有一个将 gridview 导出到 Excel 的按钮,它也有效。但是,我的 gridview 在第 1 列中有一个编辑链接,在第 2 列中有一个删除按钮。如果这些列也导出到 Excel,则两者都有。如何阻止这两列导出?我尝试过一些事情:

GridView1.Columns.RemoveAt(0);
GridView1.Columns.RemoveAt(1);

还有...

GridView1.Column(0).visible = false;

我在尝试时在每一个之后都进行了数据绑定(bind),但它根本不起作用。我将在下面发布我的代码,也许有人可以帮助我!谢谢!

 public void ExportGridToExcel(GridView grdGridView, string fileName)
{


Response.Clear();
Response.AddHeader("content-disposition",
string.Format("attachment;filename={0}.xls", fileName));
Response.Charset = "";
Response.ContentType = "application/vnd.xls";

StringWriter stringWrite = new StringWriter();
HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
grdGridView.RenderControl(htmlWrite);
GridView1.Columns.RemoveAt(0);
Response.Write(stringWrite.ToString());
Response.End();

GridView1.AllowPaging = false;

}

girdview的编辑功能设置为true,删除按钮是一个模板字段,如下所示:

<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnDelete" CommandName="Delete" runat="server" Text="Delete" CssClass="okbutton" OnClientClick="return confirm('Are you sure you want to delete this entry?');" />
</ItemTemplate>
</asp:TemplateField>

最佳答案

您需要隐藏标题行和数据行中的各个单元格,如下所示:

// Hides the first column in the grid (zero-based index)
GridView1.HeaderRow.Cells[0].Visible = false;

// Loop through the rows and hide the cell in the first column
for (int i = 0; i < GridView1.Rows.Count;i++ )
{
GridViewRow row = GridView1.Rows[i];
row.Cells[0].Visible = false;
}

关于asp.net - GridView - 导出到 Excel 时删除编辑和删除列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20384064/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com