0

Hello internet so first i am newbie at c# and i am doing project about changing data from a sql table. I passed the result of the query to the datatable and datagridview, but what i want next is i want the modified datagridview table passing to a dataset. this is how i get my table from the sql:

string query = "select Article,Barcode,\"Minimum Stock\",\"Current Stock\" from article where Fid=2 and \"Minimum Stock\" > \"Current Stock\"";

           SqlConnection conn = new SqlConnection(connString);
           SqlCommand cmd = new SqlCommand(query, conn);
           conn.Open();

           SqlDataAdapter da = new SqlDataAdapter(cmd);
           DataTable dt = new DataTable();
           da.Fill(dt);
           dataGridView1.DataSource = dt;
           conn.Close();
           da.Dispose();

Now the users will modifie the table and valid the information. How can i save it to a dataset? I tried to bind dataset to the datagridview:

Dataset1.Tables.Add(datagridview1.datasource);

But it threw a error where it says datasource doesn't use here Can anyone help?

8
  • I would suggest, from my own recent experience, that you should move your project to WPF rather than WinForms, and use MVVM pattern data binding, and finally using LINQ to SQL data classes for your model. Mar 5, 2015 at 15:51
  • Hello, this is very indeed interesting, but is there a way to move my winform project to wpf project?
    – thomasxd24
    Mar 5, 2015 at 16:39
  • You must create a new project, and then using xaml layout the windows. The logical code you can copy and paste into the new project files, but it takes some tweaking because WPF is a different set of controls (DataGrid rather than DataGridView for instance). For a good start use http://www.codeproject.com/Articles/124160/Using-WPF-MVVM-for-Database-Access as a reference, and will happily share my own experience doing the same thing for the past 6 weeks. Mar 5, 2015 at 16:47
  • Another great article on MVVM and binding to WPF controls is http://www.markwithall.com/programming/2013/03/01/worlds-simplest-csharp-wpf-mvvm-example.html. It presents a couple of helper classes that you'll want to use to simplify the binding from xaml to view model. Mar 5, 2015 at 16:59
  • Thank you i will try but i have a new problem, how can i remove rows in usercontrol?
    – thomasxd24
    Mar 5, 2015 at 17:28

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.