jog
jog

Reputation: 7

update dataGridView in winform app

I hope I could clarify the question, I need to display data in dataGridView, and after the user change data return it back to the dataBase . in the code I have a dal class ,I will show the function and then the form.

public static DataSet Dis()
    {
        DataSet DataSet= new DataSet();
            string query = "SELECT  last_name, first_name  FROM  T1 INNER JOIN T2 ON T1.bil_id = T2.member_id";
            conS.Open();
             da_to_change = new SqlDataAdapter(query1, conString);
             da_to_change.Fill(DataSet, "dis");
             conS.Close();
        return DataSet;
    }

In the form

  DataSet ds2;
  ds2 = DAL_CLASS.Dis();
  dataGridView1.DataSource = ds2;
  dataGridView1.DataMember = "dis";

I tried several ways to update the dataBase and I could not, even if another function In the DAL_CLASS Please if anyone can help?

Upvotes: 0

Views: 514

Answers (1)

Mitja Bonca
Mitja Bonca

Reputation: 4546

If you use SqlDataAdpter class, you have to create both queries, insert, and update. Take a look at here. 1st you will execute the Select commannd to get the data our and populate dgv (set dataTable as a binding source to dgv). Next when you will want to update dataBase table, you will call an Update command. But you need to keep the reference to the sqlDataAdapter alive (do not overwrite it again with "da = new SqlDataAdapter(); - in this case it will be gone, and you will have to call that common method again).

Upvotes: 1

Related Questions