【问题标题】:Export DataGridView SelectedRows to a DataTable将 DataGridView SelectedRows 导出到 DataTable
【发布时间】:2016-09-22 14:08:43
【问题描述】:

我想将DataGridView 的多个选定行传输到DataTable,然后将其设置为我的水晶报表的数据源。

首先我通过存储过程加载来自我的数据库的所有数据。

dataGridView1.DataSource = clsPayroll.view_Employee();

然后我将下面的代码放在不限制多选的打印选择按钮中。

foreach(DataGridViewColumn column in dataGridView1.Columns)
table.Columns.Add(column.Name, typeof(string));

for (int i = 0; i < dataGridView1.SelectedRows.Count; i++) {
    table.Rows.Add();
    for (int j = 0; j < dataGridView1.Columns.Count; j++) {
        table.Rows[i][j] = dataGridView1[j, i].Value;
    }
}
rpt.SetDataSource(table);

我的数据库中有一些条目:

EMP_ID      Emp_Name    Gender  
EMP-000013  Dummy       Male    
EMP-000014  Teresa      Female  
EMP-000015  Dutcry      Male    

当我选择 EMP-000014 和 EMP-0000015 行时。

我希望 Crystalreport 查看器会列出它,但它会显示 EMP-000013 和 EMP-000014

【问题讨论】:

    标签: c# winforms datagridview datatable


    【解决方案1】:

    感谢那些发布答案的人,很抱歉没有更新我是否已经修复了它。

    我能够弄清楚为什么我的代码不能像以前那样工作了。

    我应该使用 datagridview.selectedrows 而不是从 datagridview 获取数据。

    这是代码

    foreach(DataGridViewColumn column in dataGridView1.Columns)
    table.Columns.Add(column.Name, typeof(string));
    
    for (int i = 0; i < dataGridView1.SelectedRows.Count; i++) {
        table.Rows.Add();
        for (int j = 0; j < dataGridView1.Columns.Count; j++) {
           table.Rows[i][j] = dataGridView1.SelectedRows[i].Cells[j].Value;
    
        }
    }
    rpt.SetDataSource(table);
    

    【讨论】:

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