想把查询的结果添加datagridview里去,不知道要如何写,查询的语句如下:
                string Goods_str = "SELECT Goods_ID, Goods_Name, Goods_Param, (SELECT Goods_Ctg_Name FROM tb_goods_ctg WHERE tb_goods_ctg.Goods_Ctg_ID = tb_goods.Goods_Ctg_ID) as Ctg, (SELECT UOM_Name FROM tb_uom WHERE tb_uom.UOM_ID = tb_goods.UOM_ID) as UOM, Goods_Barcode FROM tb_goods WHERE Goods_Barcode = '";
                Goods_str += this.txtEnterCode.Text.Trim() + "'";
                SqlDataReader Goods_DR = DBSetup.GetDR(Goods_str);
                Goods_DR.Read();
                if (Goods_DR.HasRows)
                {
                    //this.label1.Text = Goods_DR["Goods_Barcode"].ToString();
                    //this.label2.Text = Goods_DR["Goods_Name"].ToString();
                    //this.label3.Text = Goods_DR["Goods_Param"].ToString();
                    //this.label4.Text = Goods_DR["Ctg"].ToString();
                    //this.label5.Text = Goods_DR["UOM"].ToString();
                }
                else
                {
                    MessageBox.Show("无此商品","提示",MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                Goods_DR.Close();

解决方案 »

  1.   

    放在一个dataset里.
    然后两句就直接邦定
    datagridview.datasoure=dataset.table[0];
    datagridview.databind();
      

  2.   

    可以用绑定
    rows.add属性也可以加
      

  3.   

    我是想把Goods_DR["Goods_Barcode"].ToString();等五个值添加到dgv的一行,要怎么写语句?
      

  4.   

     
    datagridview.datasoure=Goods_DR;
    datagridview.databind();
    Goods_DR.Close();
      

  5.   

    绑定是不可以的,因为dgv是多表绑定,所以只能用dgv的添加语句,我google的时候看到有人用rows.add,但不知道怎么用。
      

  6.   

    刚才没注意,有一个办法只定可以,自己创建一个datatable对像。
    之后根据循环把Goods_DR的值,都加进去。
    之后,再用dgw的绑定。一定可以行的。要下班了。88
      

  7.   

    不绑定就这样//DataTable table=你查询出来的结果
           foreach (DataRow row in table.Rows)
                {
                    DataGridViewRow grow = dataGridView1.Rows[dataGridView1.Rows.Add()];
                    grow.Cells[0].Value = row[0];
                    grow.Cells[1].Value = row[1];
                    grow.Cells[2].Value = row[2];
                    grow.Cells[3].Value = row[3];
                }
      

  8.   


     //查询方法
            private void SearchMethod()
            {
                string sql = "select * from roomtype";
                conn = DBHelper.conn();
                da = new SqlDataAdapter(sql, conn);
                ds = new DataSet();
                da.Fill(ds);
                dataGridView1.AutoGenerateColumns = false;
                dataGridView1.DataSource = ds.Tables[0];
            }只要指定dataGridView的数据源就可以了
      

  9.   


     //查询方法
            private void SearchMethod()
            {
                string sql = "select * from roomtype";
                conn = DBHelper.conn();
                da = new SqlDataAdapter(sql, conn);
                ds = new DataSet();
                da.Fill(ds);
                dataGridView1.AutoGenerateColumns = false;
                dataGridView1.DataSource = ds.Tables[0];
            }只要指定dataGridView的数据源就可以了
      

  10.   

    //查询方法
            private void SearchMethod()
            {
                string sql = "select * from roomtype";
                conn = DBHelper.conn();
                da = new SqlDataAdapter(sql, conn);
                ds = new DataSet();
                da.Fill(ds);
                dataGridView1.AutoGenerateColumns = false;
                dataGridView1.DataSource = ds.Tables[0];
            }
      

  11.   

    刚想试试楼上几位的办法,却因为查询是在弹出窗口,而不能直接操作主窗口的dgv而失败,还是看看怎么让弹出窗口操作dgv才能试用各位推荐的办法了。
      

  12.   

    不行啊,说dgv绑定了数据源,不能这样添加数据。
      

  13.   

    datagridview.datasoure=dataset.table[0]
      

  14.   

    你那个sql能运行通过??我记忆中的sql可不是这种样子滴
      

  15.   

    用sql生成的记录源绑定就可以了!
    如果担心sql语句有问题,在分析器里运行的试试然后再写。
      

  16.   

    你的sql绝对是有问题滴,如果这种样子的sql都能通过的话ado年代就不会有数据整形的做法ado.net也不会出现,linq to sql也没必要存在了
      

  17.   


    你是说Goods_str?这是可以的,我还把它查询的结果赋值给几个lab来显示,没有出现问题。
      

  18.   

    我以前做到时候用的是最笨的方法了
    先将当前绑定清空this.dataGrid1.DataSource = null;
    然后在重新绑定this.dataGrid.DataSource = ds.Tables[0];
      

  19.   

    可第二次查询的结果添加到dgv的时候怎么办?若清空的话上次添加的记录就没有了。
      

  20.   

       string Goods_str = "SELECT Goods_ID, Goods_Name, Goods_Param, (SELECT Goods_Ctg_Name FROM tb_goods_ctg WHERE tb_goods_ctg.Goods_Ctg_ID = tb_goods.Goods_Ctg_ID) as Ctg, (SELECT UOM_Name FROM tb_uom WHERE tb_uom.UOM_ID = tb_goods.UOM_ID) as UOM, Goods_Barcode FROM tb_goods WHERE Goods_Barcode = '";
                    Goods_str += this.txtEnterCode.Text.Trim() + "'";
                    SqlDataReader Goods_DR = DBSetup.GetDR(Goods_str);
                    Goods_DR.Read();
                    if (Goods_DR.HasRows)
                    {
                        //this.label1.Text = Goods_DR["Goods_Barcode"].ToString();
                        //this.label2.Text = Goods_DR["Goods_Name"].ToString();
                        //this.label3.Text = Goods_DR["Goods_Param"].ToString();
                        //this.label4.Text = Goods_DR["Ctg"].ToString();
                        //this.label5.Text = Goods_DR["UOM"].ToString();
                    DataTable dt=new DataTable();
                    dt.Load(Goods_DR);
                    DataGridView1.Datasource=dt;
                    }
                    else
                    {
                        MessageBox.Show("无此商品","提示",MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    Goods_DR.Close();
      

  21.   

    你的表里是不是还有别的数据(除了这次查询得到的),
    那样的话你就需要获取数据的时候把数据添加到 已经绑定的表中.
    比如说原来已经有个数据源 date1  这次你得到的数据添加到原来的date1中对应的表中.
      

  22.   

    你的表里是不是还有别的数据(除了这次查询得到的),
    那样的话你就需要获取数据的时候把数据添加到 已经绑定的表中.
    比如说原来已经有个数据源 date1  这次你得到的数据添加到原来的date1中对应的表中.
      

  23.   

    你的表里是不是还有别的数据(除了这次查询得到的),
    那样的话你就需要获取数据的时候把数据添加到 已经绑定的表中.
    比如说原来已经有个数据源 date1  这次你得到的数据添加到原来的date1中对应的表中.
      

  24.   

    是的,我现在做的是一张PO,把扫描的结果显示在dgv里,然后按保存后存储到数据表里去。
      

  25.   

    完全可以不绑定数据源,然后            int currentcellcolumn = this.dgvPOLine.CurrentCell.ColumnIndex;
                int currentcellrow = this.dgvPOLine.CurrentCell.RowIndex;                                      
                string TheGoodsName = GoodsInfo_DR["Goods_Name"].ToString();
                string TheGoodsParam = GoodsInfo_DR["Goods_Param"].ToString();
                this.dgvPOLine[1, currentcellrow].Value = TheGoodsName;
                this.dgvPOLine[2, currentcellrow].Value = TheGoodsParam;