hi all

in form1 I place 3 button and 1 datagridview ,btn refresh,btnload , btnnew and Datagridview dgv1

  1. I change the value of a cells in datagridview (right after Form1 is loaded),and then click btnrefresh
  2. I click btnnew to clear the dgv1
  3. I click btnload to re fill dgv1 with data from Database TblProduct.

why my dgv1 is empty .I think there must be something inside tblProduct to be
shown in dgv1,but nothing appear.Any body can teach me why ?

thank s

code :

  private void Form1_Load(object sender, EventArgs e)
            string server = "localhost";
            string dbname = "mydatabase";

            string uid = "root";
            string password = "rootpassword";

            string connectionString = "SERVER=" + server + ";" + "DATABASE=" + 
            dbname + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
            conn = new MySqlConnection(connectionString);

            da = new MySqlDataAdapter("select * from Tblproduct", conn);
            cbl = new MySqlCommandBuilder(da);
            ds = new DataSet();
            dgv1.DataSource = ds.Tables[0];


  private void btnrefresh_Click(object sender, EventArgs e)
            //now u can save changes to back end with

  private void btnnew_Click(object sender, EventArgs e)
            dgv1.DataSource = null;
            dgv1.DataMember = null;

You're not using dgv1.Databind() after naming the datasource. That should fix it but you also say you 'think' the database table has data in it. Maybe you should check that too...

Not true, it wont help.
This is what you can try:

dgv1.DataSource = new BindingSource(ds.Tables["myTable"], null); //but the same as using names is using table indexes, like you did!

Hi Mitja ,hi hericles

I tried mitja suggestion ,it show content in dgv1 but without any changes as if I have never done .It shows the same as if I never change it .(I did changed content of one cells.)


Did you thing using only:

//now u can save changes to back end with

will save the changes to the dataBase? Nope. YOu need some other code for UPDATE as well.
What does the SqlCommand for Update look like? I see the command but I don't see any SqlText, that's what you're missing.

You need to define what .Update does by setting .UpdateCommand property on the SqlDataAdapter

This link gives a pretty good breakdown on how to go about it here.

hi all

thank you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.