Click here to Skip to main content
16,018,653 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a datagridview that is displayed on the C# windows form.
I want to add new row simultanously to the dgv when user insert some record .
Please help me .....

private void btnInsert_Click(object sender, EventArgs e)
        { 
            try
            {
            SqlConnection scon = new SqlConnection("Data Source=ASIC-02\\SQLEXPRESS;Initial Catalog=Student;Integrated Security=True");
            scon.Open();
            SqlCommand scom = new SqlCommand("rec",scon);
            scom.CommandType = CommandType.StoredProcedure;
            scom.Parameters.Add("@Name", SqlDbType.VarChar).Value = txtName.Text;
            scom.Parameters.Add("@Reg", SqlDbType.VarChar).Value = txtReg.Text;
            scom.Parameters.Add("@Dep", SqlDbType.VarChar).Value = txtDep.Text;
            dataGridView1.Rows.Add(txtName.Text, txtReg.Text, txtDep.Text);
            scom.ExecuteNonQuery();
            MessageBox.Show("Information Successfully Inserted");
            scon.Close();
            this.Refresh();
            }
             catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }         
        }
Posted
Updated 21-Jul-10 21:38pm
v3

1 solution

For this, you need to add the row to the dataset used and then re-render the grid.
 
Share this answer
 

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