如何将datatable中的数据添加到datagridview中显示出来阿?}code=C#]
 for(int i =0;i<dt.Rows.Count;i++)
            {
                DataRow dr =dt.NewRow();
                dr[i] = EnoughGridView.ToString();
                dt.Rows.Add(dr);
                EnoughGridView.DataSource = dt;
            [/code]
我运行以后出现错误提示:
输入字符串的格式不正确。不能在 QTY_GET 列中存储 <System.Windows.Forms.DataGridView>。所需类型是 Decimal。
数据库里的字段数据类型是不一样的阿。我该怎么办呢?
或者请指点一下新办法,如何将datatable中的数据添加到datagridview中显示出来解决了就结题,谢谢了

解决方案 »

  1.   


    SqlConnection conn = new SqlConnection("Data Source=.;User ID=sa;Password=sa;Initial Catalog=Test;");
    DataSet ds = new DataSet(); 
    string sql = "select * from Table1"; 
    SqlDataAdapter da = new SqlDataAdapter(sql, conn);
    da.Fill(ds); 
    dataGridView1.DataSource = ds.Tables[0]; 
      

  2.   

    查询出来了,但是datagridview中显示的列的列名是字段名,我怎么定义呢?谢谢,比如说我查询显示uesrname,我不要显示username,我自定义成用户名。请指点一下。谢谢
      

  3.   


    string sql = "select username 用户名 from Table1";
      

  4.   

    datagridview.columns["username"].headtext="用户名";
    也可以
    select username as 用户名 from table
      

  5.   

    上面是通过数据库查询语句中给字段名取别名的方法来自定义列名;你还可以通过界面设计去指定datagridview的列的 headertext属性  这个属性就是“列标题单元格的标题文本”
      

  6.   

    把你查询语句改动一下.
    如:select username as 用户名 from 你的表.
      

  7.   

    我用的是oracle。我使用as的话就提示找不到表哦!
      

  8.   

    使用这个就对了 ,谢谢阿
    datagridview.columns["username"].headtext="用户名"; 
      

  9.   

    那在前台绑定后,加上下面的语句.如:EnoughGridView.DataSource = dt; 
    for(int count=0;count<EnoughGridView.Columns.Count;count++)
    {
          if(this.EnoughGridView.Columns[count].DataPropertyName=="username")
          {
                this.EnoughGridView.Columns[count].HeaderText="用户名";
          }
    }
      

  10.   

    我想再请问一下,如何获取datagridview中选中的行的数据呢?
    谢谢了。刚学不久就要干活了,实在是斥力
      

  11.   

    dataGridView1.SelectedRows[0].Cells["Column2"].Value.ToString().Trim();
      

  12.   

    this.dataGridView1.CurrentRow.Cells["Column2"].Value.ToString()
      

  13.   

    you are right   thank you!谢谢上面的几个热心朋友