1

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!

4
  • How is this related to MVVM? Do you know how to update a database? Commented Oct 19, 2022 at 14:13
  • @mm8 I'm talking about using the selectedrows in the code as the mvvm pattern approach I believe is quite different and yes I'm aware how to update a SQLIte databse. Commented Oct 19, 2022 at 14:20
  • There is no bindable SelectedItems property so you would have to handle the SelectionChanged event and update the source collection of selected items yourself, for example using an attached behaviour: blog.magnusmontin.net/2014/01/30/… Commented Oct 19, 2022 at 14:23
  • @mm8 okay, what about non mvvm approach. Commented Oct 19, 2022 at 14:26

1 Answer 1

1

There is no bindable SelectedItems property so you would have to handle the SelectionChanged event and update the source collection of selected items yourself, for example using an attached behaviour.

The non-MVVM approach would be to simply get the selected items directly from the SelectedItems property of the DataGrid:

var selectedItems = dataGrid1.SelectedItems.OfType<Invoices>();

Once you have the selected items, inserting the relevant data into the database should be a no-brainer.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.