如何通过鼠标选择一行把datagridview里面的值赋给一个窗体进行修改和删除操作。
需要使用datagridview里的那个事件。

解决方案 »

  1.   

    窗体里面定义个id点击datagridview,得到当前idform1 x=new form1();
    x.id=当前id;
    ...
      

  2.   

    以前用CLICK  把datagridview设为共有的来实现
      

  3.   


    在Form2中添加你要接收相应的public变量数组: public string[] str;         private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
            {
                DataGridViewRow dr = this.dataGridView1.Rows[e.RowIndex];            if (dr != null)
                {
                    Form2 frm =new Form2();
                    int i=0;
                    foreach (DataGridViewCell cell in dr.Cells)
                    {
                        frm.str[i]=cell.Value.ToString();
                        i++;
                    }
                    frm.ShowDialog();
                }
            }
    当然了,你需要在form2中对str进行处理,显示出来
      

  4.   

    如果我要把查询出来的值分别赋给每一个textbox  这样怎么操作呢,窗体没法接收数据的阿
      

  5.   


     private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
    {
      this.textBox1.Text = this.dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
      this.textBox2.Text = this.dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
      this.textBox3.Text = this.dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
      this.textBox4.Text = this.dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString();
      .
      .
      .
      this.textBox5.Text = this.dataGridView1.Rows[e.RowIndex].Cells[n].Value.ToString();
    }//点击单元格或行获得值
     private void button1_Click(object sender, EventArgs e)
            {
                SqlCommand command= new SqlCommand("update set ="+"'"+this.textBox1.Text +"','"+this.textBox2.Text +"','"...
                    +" where 主键="+所选行主键值,Connection);
                command.ExecuteNonQuery();
                command.Dispose();        }//更新
      

  6.   

    你是想打开一个该行数据的明细窗体,可以查看修改之类的吧?思路参考一楼的,取出dataGridView中的id,传到另一个窗体。
    在新窗体中根据该id查询数据库,再把数据中的相关字段绑定到相应的TextBox。
      

  7.   

    6楼的思路和我想要得最接近,  只是怎么获取id   还有就是点别的地方的时候可能获取的是空值,用这个dataGridView1_CellClick  是不是能好点。
      

  8.   

    这是前面那个窗口 private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
            {
               // MessageBox.Show("" + this.dataGridView1.SelectedRows[0].Cells["name"].Value.ToString());            String name = this.dataGridView1.SelectedRows[0].Cells["name"].Value.ToString();            String password = this.dataGridView1.SelectedRows[0].Cells["password"].Value.ToString();            Form2 f2 = new Form2(name, password);            f2.Show();
            }
    后面调用的窗口 public Form2()
            {
                InitializeComponent();
            }        public Form2(String name,String password)
            {
                
                InitializeComponent();            textBox1.Text = name;
                textBox2.Text = password;
            }这样就可以传值了。。
      

  9.   

    我查询的sql语句 
    select username as name, password as password from userinfo
      

  10.   

    我是将datagridview和某个类直接绑定,每一行记录对应一个对象,双击的时候把当前对象传给编辑窗口。//双击修改对象
    private void grd_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
    {
        //获取界面上当前对象
        M_Customer mObject = dtsCustomer.Current as M_Customer;    //将该对象传入编辑窗口
        frmCustomer mfrm = new frmCustomer(mObject, FormType.Modify);    if (mfrm.ShowDialog() == DialogResult.OK)
        {
            CustomerHandler mHandler = new CustomerHandler();
            mHandler.Save(mObject);
        }
    }//编辑窗口的构造函数
    public frmCustomer(M_Customer mDtCustomer, FormType mFormType)
    {
        InitializeComponent();    //将传入对象与控件绑定
        this._Customer = mDtCustomer;
        this.dtsCustomer.DataSource = this._Customer;
    }用绑定方式和传对象是比较简单的,不需要到编辑界面中再根据ID查找出当前记录。