Question

Believe me I have googled it. Its become clear that the C# code used on win-forms doesn't work on C# WPF for semi-obvious reasons. What's not obvious though is how to Fill a MYSQL table with modified or completely new data from a data grid. It works fine the other way around though (filling the datagrid with MYSQL data).

    private void Save_Click(object sender, RoutedEventArgs e)
    {
        string sqlcon = "datasource = localhost; port = 3306; username = root; password = Avalisque";
        string queryadd = "insert into users.login (user_name,pass_word,gender,first_name,second_name,third_name,surname,security_question,answer);";
        MySqlConnection con = new MySqlConnection(sqlcon);
        MySqlCommand cmd = new MySqlCommand(queryadd, con);
        MySqlDataAdapter da = new MySqlDataAdapter(cmd);
        DataSet ds = new DataSet();


        try
        {
            con.Open();
            cmd.ExecuteNonQuery();

            DataTable dt = new DataTable("login");
            da.Fill(ds);
            DG.datasource = ds.Tables[0];
            da.Update(dt);

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }


    }

(DG being the datagrid). and this is the error message:

'System.Windows.Controls.DataGrid' does not contain a definition for 'datasource' and no extension method 'datasource' accepting a first argument of type 'System.Windows.Controls.DataGrid' could be found (are you missing a using directive or an assembly reference?)

Any form of help much appreciated. Kinda desperate here. Please and thanks.

Was it helpful?

Solution

I think you are looking for DG.ItemsSource = ds.Tables[0].AsEnumerable();

You are getting that error because the System.Windows.Controls.DataGrid doesn't have a property named datasource.

DataTableExtensions.AsEnumerable shows you how to make a DataTable into an Enumarable where T : DataRow

OTHER TIPS

I think you have a property ItemsSource.

Also do not forget that wpf works well with the mvvm pattern. In fact your itemsource probably should be an ObservableCollection available in your viewModel.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top