我有一个datagridview里面显示了可多数据,我想让datagridview中的数据给了rDataset,这样可以吗?
  rSqlDataAdapter = new SqlDataAdapter(sql, rSqlConnection)得到的是sql数据库中的数据,我想让这句变成获取datagridview中的数据可以吗?谢谢
                //将结果集填充到rDataSet本身就有可多数据,想                rDataSet = new DataSet();
                //实例化,创建SQL连接对象
                rSqlConnection = new SqlConnection("Data Source=192.9.156.28;Database=wgh;User id=sa;PWD=abdb");
                //打开连接
                rSqlConnection.Open();                rSqlDataAdapter = new SqlDataAdapter(sql, rSqlConnection);/////
                //将结果集填充到rDataSet
                rSqlDataAdapter.Fill(rDataSet, "Zhigshj");
                //关闭连接                rSqlConnection.Close();

解决方案 »

  1.   

    for(int i=0;i<datagridview.Rows.Count;i++)
    {
    DataRow dr= rDataSet.Tables[0].NewRow();dr[0]=datagridview.Rows[i].Cells[0].Value
    ......rDataSet.Tables[0].Row.Add(dr);
    }
      
    *****************************************************************************
    欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码) http://feiyun0112.cnblogs.com/
      

  2.   

            
    您好!我这样写了一下.可不审提示错误.Zhigshj是个数据集,里面没有数据.
     for(int i=0;i <this.dataGridView1.Rows.Count;i++) 

    DataRow dr2= rDataSet.Tables[0].NewRow(); dr[0]=this.dataGridView1.Rows[i].Cells[0].Value ;
    dr[1] = this.dataGridView1.Rows[i].Cells[1].Value;
    dr[2] = this.dataGridView1.Rows[i].Cells[2].Value;
    dr[3] = this.dataGridView1.Rows[i].Cells[3].Value;
     rDataSet = new DataSet();
    rDataSet.Tables[0].Rows.Add(dr2);
    rSqlDataAdapter.Fill(rDataSet, "Zhigshj");
      

  3.   

    如果你的datagridview控件是绑定数据源的话,那就直接转换为DataTable;
    DataSet ds = new DataSet();
    DataTable dt = this.datagridview.DataSource as DataTable;
    ds.Tables.Add(dt);
      

  4.   

               rDataSet = new DataSet();
                    //实例化,创建SQL连接对象
                    rSqlConnection = new SqlConnection("Data Source=192.9.151.248;Database=制版管理系统;User id=sa;PWD=datayczb");
                    //打开连接
                    rSqlConnection.Open();                rSqlDataAdapter = new SqlDataAdapter(sql, rSqlConnection);
                    //将结果集填充到rDataSet
                    rSqlDataAdapter.Fill(rDataSet, "Zhigshj");
                    //关闭连接                rSqlConnection.Close();我以前这样写就是对的,但是我把查询后的结果在datagridview中又处理了一下,所以想用此来打印一下.
      

  5.   


    我用二楼的方法取出了:
    for(int i=0;i <this.dataGridView1.Rows.Count;i++) 

    DataRow dr2= rDataSet.Tables[0].NewRow(); dr[0]=this.dataGridView1.Rows[i].Cells[0].Value ; 
    dr[1] = this.dataGridView1.Rows[i].Cells[1].Value; 
    dr[2] = this.dataGridView1.Rows[i].Cells[2].Value; 
    dr[3] = this.dataGridView1.Rows[i].Cells[3].Value; 
    rDataSet = new DataSet(); 
    rDataSet.Tables[0].Rows.Add(dr2); 
    rSqlDataAdapter.Fill(rDataSet, "Zhigshj"); 

    但是Zhigshj这个数据集还是空的.
      

  6.   


            SqlDataAdapter rSqlDataAdapter;
            /// <summary>
            /// SQL连接对象
            /// </summary>
            SqlConnection rSqlConnection;
            /// <summary>
            /// 创建数据集
            /// </summary>
            DataSet rDataSet;
    这是我定义的类型
      

  7.   

    你用索引取出看一下
    rDataSet.Tables[0]
      

  8.   

                    for (int i=0;i <this.dataGridView1.Rows.Count;i++) 

    DataRow dr2= rDataSet.Tables[0].NewRow(); //执行到这句的时候出错了.