T
Tony Johansson
Hello!
I have two different forms. Each of these have a DataGridView where the data
that is displayed is fetched from a textfile.
When I change in one DataGridView I want the other to reload the data from
the textfile and redisplay the DataGridView.
I have listed relevant code for my question below. The class is called Cash
and the other is called Stock.
It's in the Stock form that I change the textfile.
Valiables that start with _ is class instance object.
So here is what happens when I change the textfile from the other
form(Stock) I know that the
watcher_Changed is called because in Cash form I have set a breakpoint here.
When I saw that the watcher_Changed was called I just hit F5(continue) but
the DataGridView in the Cash form didn't
reload. I made another try and when the breakpoint was trigged I tried to
step in with F11 but that was not possible.
When I hit step in(F11) it just as if I had hit F5.
So can anybody give me some hint why the form with the DataGridView is not
reloading.
public partial class Cash : Form
{
DataGridViewRow _currentRow;
DataTable _dataTable = new DataTable();
BindingSource _bindingSource = new BindingSource();
List<Product> _myReceiptList = new List<Product>();
FileSystemWatcher _watcher;
public Cash()
{
InitializeComponent();
_bindingSource.DataSource = _dataTable;
dgCash.DataSource = _bindingSource;
_watcher = new FileSystemWatcher();
_watcher.Changed += new FileSystemEventHandler(watcher_Changed);
WatchFile();
}
private void WatchFile()
{
watcher.Path =
Path.GetDirectoryName(FileManager.GetFullFileName());
watcher.Filter = Path.GetFileName(FileManager.GetFullFileName());
watcher.NotifyFilter = NotifyFilters.LastWrite |
NotifyFilters.FileName | NotifyFilters.Size;
watcher.EnableRaisingEvents = true;
}
void watcher_Changed(object sender, FileSystemEventArgs e)
{
Common.FormLoad(_dataTable, dgCash);
}
private void Cash_Load(object sender, EventArgs e)
{
Common.FormLoad(_dataTable, dgCash);
dgCash.Columns[ColumnName.Name.ToString()].ReadOnly = true;
dgCash.Columns[ColumnName.Price.ToString()].ReadOnly = true;
dgCash.Columns[ColumnName.Count.ToString()].ReadOnly = true;
dgCash.AllowUserToAddRows = false;
}
This method FormLoad is located in class Common
*****************************
public static void FormLoad(DataTable dataTable, DataGridView dgv)
{
List<string> myList = new List<string>();
string[] rowData;
if (dataTable.Columns.Count == 0)
{
dataTable.Columns.Add(Common.GetNewColumn(ColumnName.Name.ToString(),
ColumnName.Name.ToString(), "System.String"));
dataTable.Columns.Add(Common.GetNewColumn(ColumnName.Price.ToString(),
ColumnName.Price.ToString(), "System.Int32"));
dataTable.Columns.Add(Common.GetNewColumn(ColumnName.ProdNr.ToString(),
ColumnName.ProdNr.ToString(), "System.Int32"));
dataTable.Columns.Add(Common.GetNewColumn(ColumnName.Count.ToString(),
ColumnName.Count.ToString(), "System.Int32"));
dataTable.Columns.Add(Common.GetNewColumn(ColumnName.Type.ToString(),
ColumnName.Type.ToString(), "System.String"));
}
if (FileManager.ExistProductFile())
myList = FileManager.GetAllProducts();
foreach (string line in myList)
{
rowData = line.Split(new char[] { ',' });
Common.CreateNewRow(rowData, dataTable);
}
dgv.Columns[ColumnName.Name.ToString()].Width = 160;
dgv.Columns[ColumnName.Price.ToString()].Width = 80;
dgv.Columns[ColumnName.ProdNr.ToString()].Width = 80;
dgv.Columns[ColumnName.Count.ToString()].Width = 80;
dgv.AllowUserToDeleteRows = false;
dgv.Columns[ColumnName.ProdNr.ToString()].ReadOnly = true;
dataTable.AcceptChanges();
}
//Tony
I have two different forms. Each of these have a DataGridView where the data
that is displayed is fetched from a textfile.
When I change in one DataGridView I want the other to reload the data from
the textfile and redisplay the DataGridView.
I have listed relevant code for my question below. The class is called Cash
and the other is called Stock.
It's in the Stock form that I change the textfile.
Valiables that start with _ is class instance object.
So here is what happens when I change the textfile from the other
form(Stock) I know that the
watcher_Changed is called because in Cash form I have set a breakpoint here.
When I saw that the watcher_Changed was called I just hit F5(continue) but
the DataGridView in the Cash form didn't
reload. I made another try and when the breakpoint was trigged I tried to
step in with F11 but that was not possible.
When I hit step in(F11) it just as if I had hit F5.
So can anybody give me some hint why the form with the DataGridView is not
reloading.
public partial class Cash : Form
{
DataGridViewRow _currentRow;
DataTable _dataTable = new DataTable();
BindingSource _bindingSource = new BindingSource();
List<Product> _myReceiptList = new List<Product>();
FileSystemWatcher _watcher;
public Cash()
{
InitializeComponent();
_bindingSource.DataSource = _dataTable;
dgCash.DataSource = _bindingSource;
_watcher = new FileSystemWatcher();
_watcher.Changed += new FileSystemEventHandler(watcher_Changed);
WatchFile();
}
private void WatchFile()
{
watcher.Path =
Path.GetDirectoryName(FileManager.GetFullFileName());
watcher.Filter = Path.GetFileName(FileManager.GetFullFileName());
watcher.NotifyFilter = NotifyFilters.LastWrite |
NotifyFilters.FileName | NotifyFilters.Size;
watcher.EnableRaisingEvents = true;
}
void watcher_Changed(object sender, FileSystemEventArgs e)
{
Common.FormLoad(_dataTable, dgCash);
}
private void Cash_Load(object sender, EventArgs e)
{
Common.FormLoad(_dataTable, dgCash);
dgCash.Columns[ColumnName.Name.ToString()].ReadOnly = true;
dgCash.Columns[ColumnName.Price.ToString()].ReadOnly = true;
dgCash.Columns[ColumnName.Count.ToString()].ReadOnly = true;
dgCash.AllowUserToAddRows = false;
}
This method FormLoad is located in class Common
*****************************
public static void FormLoad(DataTable dataTable, DataGridView dgv)
{
List<string> myList = new List<string>();
string[] rowData;
if (dataTable.Columns.Count == 0)
{
dataTable.Columns.Add(Common.GetNewColumn(ColumnName.Name.ToString(),
ColumnName.Name.ToString(), "System.String"));
dataTable.Columns.Add(Common.GetNewColumn(ColumnName.Price.ToString(),
ColumnName.Price.ToString(), "System.Int32"));
dataTable.Columns.Add(Common.GetNewColumn(ColumnName.ProdNr.ToString(),
ColumnName.ProdNr.ToString(), "System.Int32"));
dataTable.Columns.Add(Common.GetNewColumn(ColumnName.Count.ToString(),
ColumnName.Count.ToString(), "System.Int32"));
dataTable.Columns.Add(Common.GetNewColumn(ColumnName.Type.ToString(),
ColumnName.Type.ToString(), "System.String"));
}
if (FileManager.ExistProductFile())
myList = FileManager.GetAllProducts();
foreach (string line in myList)
{
rowData = line.Split(new char[] { ',' });
Common.CreateNewRow(rowData, dataTable);
}
dgv.Columns[ColumnName.Name.ToString()].Width = 160;
dgv.Columns[ColumnName.Price.ToString()].Width = 80;
dgv.Columns[ColumnName.ProdNr.ToString()].Width = 80;
dgv.Columns[ColumnName.Count.ToString()].Width = 80;
dgv.AllowUserToDeleteRows = false;
dgv.Columns[ColumnName.ProdNr.ToString()].ReadOnly = true;
dataTable.AcceptChanges();
}
//Tony