日期:2014-05-18  浏览次数:21175 次

大侠,求助!
怎么将我select 出来的结果插入到dt并显示到datagridview 中?小弟新人学习中.....

  DataTable dt = new DataTable("record");
  DataRow dr;
  dt.Columns.Add(new DataColumn("ID",typeof(Int32)));
  dt.Columns.Add(new DataColumn("时间",typeof(string)));
  dt.Columns.Add(new DataColumn("费用",typeof(decimal)));
  dt.Columns.Add(new DataColumn("说明",typeof(string)));
  MySqlConnection con = new MySqlConnection("Server=localhost;User ID=root;Password=pw;Persist Security Info=True;;DataBase=db;charset=utf8");
  string sql = "SELECT ID,concat(Years,'-',Months,'-',Days) AS 时间,Play as 费用,Explains as 说明 FROM record where id='" + this.tbx_id_query.Text + "'";
  ..........................
  ..........................
(这样? MySqlDataAdapter adp = new MySqlDataAdapter(sql,con);
  DataSet ds = new DataSet();
  adp.Fill(ds,"dt");
  bindingSource1.DataSource=ds.Tables["dt"];
  dataGridView1_Query.DataSource = bindingSource1;//我这样操作有错
)请各位大侠指点下..........
或者给出代码让小弟参考学习下

------解决方案--------------------
DataAdapter完全可以,怎不行,如果你非要插入,那就在datatable中增加Row
------解决方案--------------------
C# code

bindingSource1.DataSource=ds.Tables[0];
dataGridView1_Query.DataBind();

------解决方案--------------------
探讨

C# code

bindingSource1.DataSource=ds.Tables[0];
dataGridView1_Query.DataBind();