【问题标题】:Convert BindingSource to Datatable or Dataset将 BindingSource 转换为 Datatable 或 Dataset
【发布时间】:2013-06-24 13:05:56
【问题描述】:

我正在使用数据绑定源,该数据源与列表绑定。现在我将此数据源转换为数据集,但它抛出了如下异常:

无法将“System.Windows.Forms.BindingSource”类型的对象转换为“System.Data.DataSet”类型

我正在编写此代码来将数据源转换为数据集。

 if (childCtrl is DataGridView)
                    {
                        DataSet ds = new DataSet();
                        ds = (DataSet)(((DataGridView)childCtrl).DataSource);
                        ds.WriteXml(@"D:\AverageReporting.xml");
                    }

那么有什么方法可以将绑定源数据转换为数据集?

【问题讨论】:

    标签: sql-server-2008 c#-4.0


    【解决方案1】:

    试试这个

    DataSet ds = new DataSet();
    
    foreach(DataGridViewColumn col in dgv.Columns)
     ds.table[0].Columns.Add(col.HeaderText, typof(string));
    
    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
      foreach (DataGridViewCell cell in row.Cells)
      {
          ds.table[0].Rows[row.Index][cell.ColumnIndex] = cell.Value;   
      }          
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-13
      • 2015-10-20
      • 1970-01-01
      • 1970-01-01
      • 2012-07-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-27
      相关资源
      最近更新 更多