Click here to Skip to main content
15,063,944 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
-----------------------
C#
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)
        {
           
        }

this is form2.
------------------
C#
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();
        }
    }
Posted
Updated 25-Jul-12 20:20pm
v2
Comments
graciax8 26-Jul-12 4:57am
   
what is your problem?
rockpune 26-Jul-12 8:39am
   
i have to update datagridview selected row value
graciax8 26-Jul-12 22:28pm
   
is this a web form or an app? complete your information.

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

C#
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);
        }


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:

C#
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();
        }


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

Happy Codding :-D

-Pepin z Hane
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900