I have a WPF datagrid whose itemsource is bound to a ICollectionView and the datagrid columns are like
<DataGrid.Columns>
<DataGridTextColumn
Header="Vendor"
Binding="{Binding Path=Seller, Mode=OneWay}"
IsReadOnly="True" />
<DataGridTextColumn
Header="Invoice No."
Binding="{Binding Path=InvNo, Mode=OneWay}"
IsReadOnly="True" />
...
</DataGrid.Columns>
where not all database columns are displayed in the datagrid. I'm using SQLite database and using System.Data.SQLite, no entity framework or any other thing.
The model class looks like
public class Invoices : ViewModelBase
{
private int _id;
public int Id
{
get
{
return _id;
}
set
{
SetValue(ref _id, value);
}
}
private string _Seller;
public string Seller
{
get
{
return _Seller;
}
set
{
SetValue(ref _Seller, value);
}
}
...
}
Now when I select multiple rows of the datagrid and press a button I want the database to be updated. The update logic is simple no columns data is changed except some text say "Processed" is added to the last column of the database table of all the selected rows/items.
So, I guess first I have to get all the columns data of each of the selected rows somehow, store it and then use it to update ? But can't figure out how to following mvvm pattern or without it.
Generally I update database like below
string sql = "UPDATE mydataTable SET Seller = @seller, InvNo = @invNo, Info=@info WHERE Id = @Id";
SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
command.Parameters.AddWithValue("@seller", sellerData);
...
command.Parameters.AddWithValue("@info", "Processed");
command.ExecuteNonQuery();
But how do I modify it to use the selected rows to update the database?
Please help!
SelectedItemsproperty so you would have to handle theSelectionChangedevent and update the source collection of selected items yourself, for example using an attached behaviour: blog.magnusmontin.net/2014/01/30/…