C# 我的DataGridView在为其设置数据源后为空

C# 我的DataGridView在为其设置数据源后为空,c#,winforms,datagridview,C#,Winforms,Datagridview,我有一个函数,它获取一个DataGridView作为参数,并将其数据导出到excel中: public Worksheet exportToExcel(DataGridView dgv) 我想对数据表执行相同的操作,因此我编写了以下函数: public Worksheet exportToExcel(System.Data.DataTable dataTable) { DataGridView dgv = new DataGridView(); dgv.DataSource =

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

public Worksheet exportToExcel(DataGridView dgv)
我想对数据表执行相同的操作,因此我编写了以下函数:

public Worksheet exportToExcel(System.Data.DataTable dataTable)
{
    DataGridView dgv = new DataGridView();
    dgv.DataSource = dataTable;
    return exportToExcel(dgv);
}
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没有任何列和行。 我确信我的DataTable有数据,当我在表单上显示DataGridView时,一切都正常

这是我的exportToExcel功能:

public Worksheet exportToExcel(System.Data.DataTable dataTable)
{
    DataGridView dgv = new DataGridView();
    dgv.DataSource = dataTable;
    return exportToExcel(dgv);
}
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;
}
公共工作表导出到Excel(DataGridView dgv)
{
//创建excel应用程序
Microsoft.Office.Interop.Excel.Application excelApplication=新应用程序类();
excelApplication.Visible=false;
//打开excel模板文件
对象模板=AppDomain.CurrentDomain.BaseDirectory+“Reports/Excel/General.xltx”;
工作簿excelWorkbook=excelApplication.Workbooks.Add(模板);
工作表Excel工作表=(工作表)Excel工作簿。工作表。获取项目(“表1”);
//导出列标题
对于(int i=0;i
“当我在表单上显示DataGridView时,一切正常”

为了读取(迭代)datagridview的行/列,需要将其添加到控件(Form/UserControl)。我认为它也需要画出来,但事实并非如此。 这意味着您可以在调用exportToExcel之前将DataGridView添加到新表单中,然后处理表单和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在数据隐藏时不会刷新数据。您是否尝试调用
dgv.refresh()
?如果您已经有一个表,如果您没有显示它,为什么要使用DataGridView。这是一个糟糕的实现,使用您拥有的datatable并将其导出到excel…同样,在返回时,您调用该函数并传递datagridview而不是datatable。。。这不管用,也不管用…@Veland:是的,我试过了,但还不管用。谢谢你的回答。我尝试了您的代码,但它也不起作用。请参阅我的编辑,在设置数据源之前尝试将DataGridView添加到表单中