Click here to Skip to main content
15,891,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have following code with table customer having id and name field .......when i insert new record i want to change datagrid simultaneously
so i performed this but on load() i am getting blank data grid view



C#
 private void load()
        {
            string sql = "select * from customer";
            SqlDataAdapter myAdapter = new SqlDataAdapter(sql, con);

            DataSet myDataSet = new DataSet("customer");
            myAdapter.Fill(myDataSet);

            dataGridView1.DataSource = myDataSet;
        }

private void button4_Click(object sender, EventArgs e)
       {

           if (txtname.Text != "" & txtage.Text != "")
           {
               con.Open();

              
               //simple
               com.CommandText = "insert into customer(customerName,customerCode) values ('" + txtname.Text + "','" + txtage.Text + "')";
               com.ExecuteNonQuery();
               con.Close();
               MessageBox.Show("record inserted");
               load();

           }
           else MessageBox.Show("data entry error");

       }

here button will add record ....................record is getting insert but datagrid view on load function gibes blank
plz tell wat i have missed

regards
Posted

1 solution

datagrid view on load function gibes blank
You point to dataset instead of table to grid. Try:
C#
DataTable myDataTable = new DataTable("customer");
myAdapter.Fill(myDataTable); 
dataGridView1.DataSource = myDataTable;
 
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