0

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.

Community
  • 1
  • 1
KyloRen
  • 2,691
  • 5
  • 29
  • 59

2 Answers2

1

If it is the name of the data grid you want, this should work.

DataGrid dg = (DataGrid)sender;  // Will throw an exception if not a DataGrid
string name = dg.Name;

Not sure why you use DataObject.

  • Datagrid was not in intellisense. That what I figured I needed, but b/c it was not there, I through a whole heap of controls and objects to that. I have a lot to learn. – KyloRen Jun 14 '16 at 03:13
  • Now that I realize you can do that, to get around the exception you could do this, `DataGrid dg = sender as DataGrid; if (dg != null) { string dgName = dg.Name; }` – KyloRen Jun 14 '16 at 03:21
  • It all depends on if you want the exception or not. Sometimes you do, sometimes you don't. – George Jonsson Jun 14 '16 at 04:49
1

Use VisualTreeHelper class :

private void Dgrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
        {
            FrameworkElement source = e.EditingElement;

            while (!(source is DataGrid))
                source = VisualTreeHelper.GetParent(source) as FrameworkElement;

            MessageBox.Show(((DataGrid)source).Name);
        }
AnjumSKhan
  • 9,647
  • 1
  • 26
  • 38