想构建一个DataSet 其有三列 分别为col1,col2,col3类型均为varchar(50)
有六个值:string val1,val2,val3,val4,val5,val6
值均已赋值,请给出代码,将val1-val3插入新建的DataSet对象ds中第一行,
cal4-val6当作ds的第二行

解决方案 »

  1.   

    DataSet set = new DataSet(); 
    SqlConnection connec = new SqlConnection("连接数据库字符串"); 
    connec.Open(); 
    SqlDataAdapter comm = new SqlDataAdapter("sql查询语句", connec); 
    comm.Fill(ds,'填充的表名'); 
    DataGridView1.DataSouce = set.tables['填充的表名']; 
    另外在创建控件的时候,将datagridview控件列的属性dataPropertName的值设置成数据库中表相对应的字段名
      

  2.   


    DataSet ds = new DataSet();
                DataTable dt = new DataTable();
                dt.Columns.Add("col1", typeof(string));
                dt.Columns.Add("col2", typeof(string));
                dt.Columns.Add("col3", typeof(string));            DataRow row = dt.NewRow();
                row["col1"] = "val1";
                row["col2"] = "val2";
                row["col3"] = "val3";
                dt.Rows.Add(row);            DataRow row2 = dt.NewRow();
                row2["col1"] = "val4";
                row2["col2"] = "val5";
                row2["col3"] = "val6";
                dt.Rows.Add(row2);            ds.Tables.Add(dt);
      

  3.   


    假设你的DataSet对象为ds,如下是添加2行新数据的代码:DataRow row1= ds.Tables[0].NewRow();
    row1["col1"] = val1;
    row1["col2"] = val2;
    row1["col3"] = val3;
    ds.Tables[0].Rows.Add(row1);DataRow row2= ds.Tables[0].NewRow();
    row2["col1"] = val4;
    row2["col2"] = val5;
    row2["col3"] = val6;
    ds.Tables[0].Rows.Add(row2);
      

  4.   


    你要建的应该是DataTable,而不是DataSet,不要混淆在二者的区别与联系。有了table,可以随便你绑定到随便哪个DataSet上去。
    DataTable table = new DataTable();
    table.Columns.Add(new DataColumn("col1", typeof(string)));
    table.Columns.Add(new DataColumn("col2", typeof(string)));
    table.Columns.Add(new DataColumn("col3", typeof(string)));DataRow row = table.NewRow();
    row[0] = val1;
    row[1] = val2;
    row[2] = val3;
      

  5.   

    你要建的应该是DataTable,
    dataset可以包含多个DataTable欢迎光临我的博客
      

  6.   

    正确的构造DataTable 学会就行了 添加列 添加数据