在C#中格式化GridView的数据

时间:2018-03-26 08:58:52

标签: c# .net datagridview

我正在使用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

这是出现的错误:

  

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

从按钮单击调用代码,然后使用它为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文档的操作方法的代码

1 个答案:

答案 0 :(得分:0)

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

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");
        }