【问题标题】:my DataGridView is blank after setting DataSource for it为其设置 DataSource 后,我的 DataGridView 为空白
【发布时间】:2014-09-03 16:52:04
【问题描述】:

我有一个函数,它获取 DataGridView 作为参数并将其数据导出到 excel:

public Worksheet exportToExcel(DataGridView dgv)

我想对DataTable做同样的动作,所以我写了如下函数:

public Worksheet exportToExcel(System.Data.DataTable dataTable)
{
    DataGridView dgv = new DataGridView();
    dgv.DataSource = dataTable;
    return exportToExcel(dgv);
}

但是当我运行第二个函数时,DataGridView 没有任何列和行。 我确定我的 DataTable 有数据,当我在表单上显示 DataGridView 时,一切正常。

这是我的 exportToExcel 函数:

public Worksheet exportToExcel(DataGridView dgv)
{
    //create excel application
    Microsoft.Office.Interop.Excel.Application excelApplication = new ApplicationClass();
    excelApplication.Visible = false;

    //open excel template file
    object template = AppDomain.CurrentDomain.BaseDirectory + "Reports/Excel/General.xltx";
    Workbook excelWorkbook = excelApplication.Workbooks.Add(template);
    Worksheet excelWorksheet = (Worksheet)excelWorkbook.Worksheets.get_Item("Sheet1");

    //exporting column headers
    for (int i = 0; i < dgv.Columns.Count; i++)
    {
        excelWorksheet.Cells[1, i + 1] = dgv.Columns[i].HeaderText;
    }

    //exporting data
    for (int i = 0; i < dgv.Rows.Count; i++)
    {
        for (int j = 0; j < dgv.Columns.Count; j++)
        {
            excelWorksheet.Cells[i + 2, j + 1] = dgv.Rows[i].Cells[j].Value;
        }
    }

    //draw border for each cell
    Range range = excelWorksheet.get_Range("A1", excelWorksheet.Cells[dgv.Rows.Count + 1, dgv.Columns.Count]);
    range.Borders[XlBordersIndex.xlInsideHorizontal].LineStyle = XlLineStyle.xlContinuous;
    range.Borders[XlBordersIndex.xlInsideVertical].LineStyle = XlLineStyle.xlContinuous;
    range.BorderAround(Type.Missing, XlBorderWeight.xlThick, XlColorIndex.xlColorIndexAutomatic, Type.Missing);

    //resize columns automatically based on their data
    excelWorksheet.Columns.AutoFit();

    //show excel application
    excelApplication.Visible = true;

    return excelWorksheet;
}

【问题讨论】:

  • 我认为 DataGridView 在隐藏时不会刷新其数据。
  • 你试过给dgv.Refresh()打电话吗?
  • 如果您已经有一个表格,如果您没有显示它,为什么还要显示它。这是一个糟糕的实现,使用您拥有的数据表并将其导出到 excel...
  • 另外,在您返回时,您会调用该函数并传递数据网格视图而不是数据表...这将不起作用或仍然编译...
  • @Veland:是的,我试过了,但还不行。

标签: c# winforms datagridview


【解决方案1】:

“当我在表单上显示 DataGridView 时,一切正常”

为了读取(迭代)datagridview 的行/列,需要将其添加到控件(Form/UserControl)。我以为它也需要绘制,但事实并非如此。 这意味着您可以在调用 exportToExcel 之前将 DataGridView 添加到新表单中,然后 Dispose 表单和 DataGridView。

public Worksheet exportToExcel(System.Data.DataTable dataTable)
{
    //Wrap in using statements to make sure controls are disposed
    using (Form frm = new Form())
    {
        using(DataGridView dgv = new DataGridView())
        {   
            //Add dgv to form before setting datasource                             
            frm.Controls.Add(dgv);  
            dgv.DataSource = dataTable;   
            return exportToExcel(dgv);
        }
    }   
}

这应该可行,但我想指出,这不是我推荐的解决方案。我会创建一个 exportToExcel 方法 以 DataTable 作为参数。

【讨论】:

  • 感谢您的回答。我试过你的代码,但它也不起作用。
  • 查看我的编辑,在设置数据源之前尝试将 DataGridView 添加到表单中
猜你喜欢
  • 1970-01-01
  • 2013-12-24
  • 1970-01-01
  • 2010-11-22
  • 2013-03-28
  • 1970-01-01
  • 2021-08-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多