如何更新datagridview选定的行值 [英] how to update datagridview selected row values

查看:93
本文介绍了如何更新datagridview选定的行值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有form1和form2

在form1中2个文本框和一个保存按钮

在form2中,一个datagridview和两个按钮Save和Update按钮save按钮用于打开form1以保存新记录以及如何更新datagridview选定的行值

这是我的表格1.
-----------------------

i have form1 and form2

in form1 2 textboxes and one save button

in form2 one datagridview and two buttons save and Update buttons save button is use to open the form1 to save new records and how to update datagridview selected row value

this is my form1.
-----------------------

public Form1()
        {
            InitializeComponent();
        }

        private void btnsave_Click(object sender, EventArgs e)
        {      
            try
            {
                SqlCommand cmd = new SqlCommand("insert into forms1(Name,Address) values('" + txtname.Text + "','" + txtadd.Text + "')", con);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();

                frmgrid obj = new frmgrid();
                obj.Bind();
                obj.ShowDialog();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
       private void Form1_Load(object sender, EventArgs e)
        {
           
        }


这是form2.
------------------


this is form2.
------------------

public frmgrid()
        {
            InitializeComponent();
        }
        private void frmgrid_Load(object sender, EventArgs e)
        {
            Bind();
        }
        public  void Bind()
        {
            con.Open();
            SqlDataAdapter da = new SqlDataAdapter("select * from forms1", con);
            DataTable dt = new DataTable();
            da.Fill(dt);
            dataGridView1.DataSource = dt;
            con.Close();
        }
        public void btnUpdate_Click(object sender, EventArgs e)
        {
           

        }
        public  void btnSave_Click(object sender, EventArgs e)
        {
            Form1 obj = new Form1();
            obj.ShowDialog();
        }
    }

推荐答案

如果要更新与所选datagridviewrow关联的数据库中的记录,请执行以下操作:

If you want to update the record in the database that is associated with the selected datagridviewrow:

public void btnUpdate_Click(object sender, EventArgs e)
        {
            //set MultiSelect Property for dataGridView1 to False 
            //set SelectionMode Property for dataGridView1 to fullRowSelect
            //set EditMode for dataGridView1 to "EditOnKeystrokeOrF2"
            DataGridViewRow selectedRow = dataGridView1.SelectedRows[0];
            
            //create a method that will return a string that is the Update Query
            //pass in the selectedRow and use Row.Cells[index].Value.ToString() to populate your Update Query
            string result = createUpdateSQLStatement(selectedRow);

            //similar to your Bind() function, use a sqlCommand to execute your update query
            executeUpdateQuery(result);
        }



如果要更新datagridview,则只需再次调用Bind方法,然后可能要包含一个dataGridView1.Refresh();.在您的Bind方法中.



If you want to update the datagridview, then just call for your Bind method again, and you may want to include a dataGridView1.Refresh(); in your Bind method.


尝试一下:

Try this:

public  void btnSave_Click(object sender, EventArgs e)
        {
            Form1 obj = new Form1();
            obj.txtname.Text = dataGridView1.SelectedRows[0].Cells[0].Text;
            obj.txtadd.Text = dataGridView1.SelectedRows[1].Cells[1].Text;
            obj.ShowDialog();
        }



现在,您已经填写了txtname和txtadd ... form1中的代码应该相同.

快乐的外衣:-D

-Pepin z Hane



Now you have filled txtname and txtadd... the code in form1 should be the same.

Happy Codding :-D

-Pepin z Hane


这篇关于如何更新datagridview选定的行值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆