I have been trying to get the active datagrid's name upon a cell editing event.
Firstly, I don't know if this is a good practice, but I have a event that runs when a datagrid's cell is edited. I then am trying to test if the user added a row to the table. I want a way to see which Table is being edited so as I can put in an if clause to direct it to the correct code so it won't throw an error.
private void DataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
Staff_Time_TBL selectedRow = e.Row.Item as Staff_Time_TBL;
long id = selectedRow.ID;
if (id == -1)
{
// give a GUID and then insert it into the database when saved
selectedRow.ID = DateTime.UtcNow.Ticks;
sql.Staff_Time_TBLs.InsertOnSubmit(selectedRow);
}
try
{
sql.SubmitChanges();
LastSavedTextBlock.Text = "Last saved: " + DateTime.Now.ToLongTimeString();
}
catch(Exception ex)
{
Alerts.Error("Couldn't save changed to the database", "Database Error", ex);
}
}
At present, obviously if this table in the code below is not accessed it throws an error,
Staff_Time_TBL selectedRow = e.Row.Item as Staff_Time_TBL;
long id = selectedRow.ID;
My attempts at getting the datagrid's name, this just returns DataGrid
var tblName = sender.GetType().Name;
And this returns null
for the variable tblName2
and throws an exception on the last line due to that.
string dataGridName = "";
DataObject tblName2 = sender as DataObject;
dataGridName = tblName2.ToString();
There is this thread that gets all tables names and This thread that checks to see if one exists, but I can't find anything of how to get the sender
datagrid's name.
Obviously if this is not good practise I would like to know. Thanks.