keshav singh

keshav singh

  • NA
  • 550
  • 256.4k

how can i dynamically bind the datagridview with database

Nov 19 2009 1:57 AM
i have feeding form which has four fields name,lastname,category,occupation in info table in database 
when i run the program first time the data is show in gridview control
but when i fill the  all fields and click on insert button. the data is inserted successfully but it does not show the new inserted data in datagridview control.
can you tell me the code for this

on form load
public Form1()
        {
            InitializeComponent();
            con.Open();
            SqlDataAdapter da = new SqlDataAdapter("select * from info",con);
            DataTable dt = new DataTable();
            da.Fill(dt);
            dataGridView1.DataSource = dt;
            con.Close();
        }
on insert button click
  private void button1_Click(object sender, EventArgs e)
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("insert into info values('"+textBox1.Text+"','"+textBox2.Text+"','"+textBox3.Text+"','"+textBox4.Text+"')",con);
            cmd.ExecuteNonQuery();
            con.Close();
            lblmsg.Text = "Data inserted Succesfully";
            dataGridView1.datasource= infodatasource   
        }

thanks in advance
keshav

Answers (1)