Hi,
I have a
DataGrid
that is related to the book specifications.
Inventory
is one of these specifications. To avoid user error, I automatically calculate the
Inventory
of that book based on its
ISBN
and save these changes in the
Access
database after removing each row.
The value of the
IsReadOnly
property of the
Inventory
column in the
DataGrid
is set to
True
.
So, if I'm not mistaken, after deleting each row, we have two choices: reload the database data in the
BookDataGrid.ItemsSource
or change the changes made, which are saved in a
List<>
, and then transfer them into the
BookDataGrid.ItemsSource
.
The following codes are related to removing the row from
BookDataGrid
and then saving it to the database:
OleDbConnection OleDbConnect = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + System.Windows.Forms.Application.StartupPath + @"\Database.accdb");
DataView BookDataView = new DataView();
public MainWindow()
{
InitializeComponent();
BookDataView = BookDataGrid.ItemsSource as DataView;
}
private void DataGridDeleteMenu_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
List<object> Rows = new List<object>();
for (int i = 0; i < BookDataGrid.Items.Count; i++)
{
Rows.Add(BookDataGrid.Items[i]);
}
uint[] BookCodeSelectedItems = new uint[BookDataGrid.SelectedItems.Count];
string[] ISBN_Value = new string[BookDataGrid.SelectedItems.Count];
int j = 0;
foreach (DataRowView DRV in BookDataGrid.SelectedItems)
{
BookCodeSelectedItems[j] = uint.Parse(BookDataView.Table.Rows[BookDataView.Table.Rows.IndexOf(DRV.Row)][3].ToString());
ISBN_Value[j] = BookDataView.Table.Rows[BookDataView.Table.Rows.IndexOf(DRV.Row)][14].ToString();
j++;
}
for (int i = 0; i < BookDataGrid.SelectedItems.Count; i++)
{
Rows.Remove(BookDataGrid.SelectedItems[i]);
}
OleDbCommand OleDbCommand_Delete = new OleDbCommand();
OleDbCommand_Delete.Connection = OleDbConnect;
OleDbConnect.Open();
for (int i = 0; i < BookCodeSelectedItems.Length; i += 210)
{
OleDbCommand_Delete.CommandText = string.Join(null, "Delete From BookTable Where BookCode In (", string.Join(",", BookCodeSelectedItems.Skip(i).Take(210)), ")");
OleDbCommand_Delete.ExecuteNonQuery();
}
OleDbConnect.Close();
string[] UniqueISBN = ISBN_Value.Distinct().ToArray();
OleDbCommand OleDbCommand_Update = new OleDbCommand();
OleDbCommand_Update.Connection = OleDbConnect;
OleDbConnect.Open();
for (int i = 0; i < UniqueISBN.Length; i++)
{
OleDbCommand_Update.CommandText = string.Join(null, "Select Count(*) From BookTable Where ISBN = ", UniqueISBN[i]);
OleDbCommand_Update.CommandText = string.Join(null, "Update BookTable Set Inventory = ", (int)OleDbCommand_Update.ExecuteScalar(), " Where ISBN = ", UniqueISBN[i]);
OleDbCommand_Update.ExecuteNonQuery();
}
OleDbConnect.Close();
BookDataGrid.ItemsSource = Rows;
}
I don't mean convenience when I say
quickest and most efficient, but rather quick execution and low
RAM
usage.
I can reload the database in
BookDataGrid.ItemsSource
using the following method, but I think changing the
List<object>
(I'm referring to
Rows
, which is defined in line 11) is faster and uses less
RAM
than restoring the database, but I don't know exactly how to do it.
If the database is large, the following method will take a long time and will consume a lot of
RAM
:
public void BookDatagridRefresh()
{
DatabaseDataSet Database_DataSet = ((DatabaseDataSet)TryFindResource("Database_DataSet"));
DatabaseDataSetTableAdapters.BookTableTableAdapter BookTable_TableAdapter = new DatabaseDataSetTableAdapters.BookTableTableAdapter();
BookTable_TableAdapter.Fill(Database_DataSet.BookTable);
BookDataGrid.ItemsSource = Database_DataSet.Tables["BookTable"].DefaultView;
}
What I have tried:
I think that something like this would be quicker than reloading:
To view the link, click here
However, the following link may also assist in answering this question:
To view the link, click here
I use the following tools:
.NET Framework 4.5.2
,
WPF
Thanks