I'm learning c# at the moment and have decided to look into WPF but I'm having a real mare!!
In my standard winforms apps I can populate a datagridview with the following code:
DBConnect DBConPL = new DBConnect();
string strSqlPL = "SELECT name AS 'Product Name', SUM(quantity) AS 'QTY' FROM order_product WHERE order_id IN (" + idString + ") GROUP BY name";
BindingSource bSourcePL = new BindingSource();
bSourcePL.DataSource = DBConPL.GetData(strSqlPL);
dataGridView1.DataSource = bSourcePL;
Unfortunately, I just cannot get anything similar using WPF. I have tried:
DataTable dt = new DataTable();
MySqlDataAdapter dap = new MySqlDataAdapter(strSql, connection);
if (OpenConnection() == true)
{
dap.Fill(dt);
this.CloseConnection();
}
dataGrid1.DataContext = dt.DefaultView;
But the datagrid just shows 1 empty row and no column headers. I'm not sure if I also need to edit the properties in design view or edit the xaml.
My xaml is:
Can anyone please, please point me in the right direction before I pull the rest of my hair out!?
Many thanks
Sarah
Sarah ReynoldsPosted Feb 7, 2012, 9:13 AM
ie: foreach (DataGridViewRow row in rowsToDelete)
datagridview.Rows.Remove(row);
I want to be able to remove rows from the datagrid in my WPF app but NOT from the database?
Can anyone advise how to do this?
This is my code to fill the datagrid:
MySqlConnection _Conn = new MySqlConnection(_ConnectionString);
// Open the Database Connection
_Conn.Open();
MySqlDataAdapter _Adapter = new MySqlDataAdapter("Select * from customer", _Conn);
DataSet _Bind = new DataSet();
_Adapter.Fill(_Bind, "MyDataBinding");
dataGrid1.DataContext = _Bind;
// Close the Database Connection
_Conn.Close();
I've looked through a lot of examples but there seems to be a lot of conflicting info on how to even determine which row the user clicked. If anyone could provide an example I would sooooo grateful!!
Thanks
Sarah
Mamta MPosted Feb 6, 2012, 11:21 PM
Data binding in WPF is vastly different from that in Winforms. Please look at the recommended articles section on the right of this page to see all datagrid related articles with WPF and they will answer your queries.