【问题标题】:Formatting the data of a Grid-view in C#在 C# 中格式化网格视图的数据
【发布时间】:2018-03-26 08:58:52
【问题描述】:

我正在使用 GridView,我需要从该网格中删除一列,我还需要重新排序该网格的列,

我尝试了一些在互联网上找到的解决方案,但都没有帮助。

这是我尝试过的几个解决方案,但对我不起作用。

myGridView.columns.RemoveAt(index);    //Index is the index of the column you want to remove
myGridView.Databind();

和:

  dataGridView1.Columns[index].Visible = false; // the index of the column to be hidden

这是出现的错误:

索引超出范围。必须是非负数且小于 集合。参数名称:索引

通过单击按钮调用代码,我用它来构建一个用于 excel 导出的网格。

这是我填充网格视图的代码:

    public ActionResult ExportToExcel()
    {
        ContactFormListViewModel viewModel = new ContactFormListViewModel();
        viewModel.ContactForm = contactFormRepository.GetAll();

        var gv = new GridView();
        gv.DataSource = viewModel.ContactForm;
        gv.DataBind();
        gv.Columns[2].Visible = false;
        Response.ClearContent();
        Response.Buffer = true;
        Response.AddHeader("content-disposition", "attachment; filename=DemoExcel.xls");
        Response.ContentType = "application/ms-excel";
        Response.Charset = "";
        StringWriter objStringWriter = new StringWriter();
        HtmlTextWriter objHtmlTextWriter = new HtmlTextWriter(objStringWriter);
        gv.RenderControl(objHtmlTextWriter);
        Response.Output.Write(objStringWriter.ToString());
        Response.Flush();
        Response.End();

        return View("Index");
    }

这是将网格的数据导出到excel文档的action方法的代码

【问题讨论】:

  • did not work for me 这是什么意思?你有什么错误吗?什么错误?另外,您何时执行此代码,在按钮单击时,在 page_load 上?是windows应用还是web应用?
  • 您遇到错误了吗?
  • var gv = new GridView(); 你为什么要这样做? GridView 尚未添加到页面或表单中?
  • Index was out of range. 你在dataGridView1.Columns[index] 中传递的index 的值是多少?
  • myGridView、dataGridView1 和 gv 是同一个网格视图吗?

标签: c# .net datagridview


【解决方案1】:

我认为有足够的讨论来理解这个问题,即使它不是最好的解决方案,我也会发布这个问题的潜在解决方案。

public ActionResult ExportToExcel()
        {
            ContactFormListViewModel viewModel = new ContactFormListViewModel();
            viewModel.ContactForm = contactFormRepository.GetAll();

            DataTable dt = new DataTable();
            DataRow dr;
            dt.Columns.Add(new DataColumn("Id", typeof(Int32)));
            dt.Columns.Add(new DataColumn("Title", typeof(string)));
            dt.Columns.Add(new DataColumn("FirstName", typeof(string)));
            dt.Columns.Add(new DataColumn("LastName", typeof(string)));
            dt.Columns.Add(new DataColumn("Address", typeof(string)));
            dt.Columns.Add(new DataColumn("City", typeof(string)));
            dt.Columns.Add(new DataColumn("State", typeof(string)));
            dt.Columns.Add(new DataColumn("Country", typeof(string)));
            dt.Columns.Add(new DataColumn("Zip", typeof(string)));

            foreach (var item in viewModel.ContactForm)
            {
                dr = dt.NewRow();

                dr[0] = item.ID;
                dr[1] = item.Title;
                dr[2] = item.FirstName;
                dr[3] = item.LastName;
                dr[4] = item.Address;
                dr[5] = item.City;
                dr[6] = item.State;
                dr[7] = item.Country;
                dr[8] = item.Zip;

                dt.Rows.Add(dr);
            }

            var gv = new GridView();
          
            gv.DataSource = dt;
            gv.DataBind();
            Response.ClearContent();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment; filename=DemoExcel.xls");
            Response.ContentType = "application/ms-excel";
            Response.Charset = "";
            StringWriter objStringWriter = new StringWriter();
            HtmlTextWriter objHtmlTextWriter = new HtmlTextWriter(objStringWriter);
            gv.RenderControl(objHtmlTextWriter);
            Response.Output.Write(objStringWriter.ToString());
            Response.Flush();
            Response.End();

            return View("Index");
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-14
    • 1970-01-01
    相关资源
    最近更新 更多