0

I have one GridControl in which I inserted, modified directly to the database.

I have several fields directly connected to a table Patient and one of my gridcontrol field is the id that was especielistas 1 TableSpecialistwhich has a 1:1 RelationshipEsp-> MuchosPacientesand this field shows you allID specialistsand lets me do the main functions. But to eliminate me says no sender == null, but implement the same code but for 1 event KeyDown and does it perfectly removed and sender != null which can be:

private void gridControl1_EmbeddedNavigator_ButtonClick(
        object sender,
         NavigatorButtonClickEventArgs e)
{           
    if (e.Button.ButtonType == NavigatorButtonType.Edit 
        || e.Button.ButtonType == NavigatorButtonType.EndEdit)
    {
        ColumnView view = gridControl1.FocusedView as ColumnView;
        view.CloseEditor();

        if (view.UpdateCurrentRow())
        {
            pacienteTableAdapter.Update(dBDataSet);
        }
    }
    else if (e.Button.ButtonType == NavigatorButtonType.Remove)
    {

        if (MessageBox.Show("Desea eliminar?", "Confirmación", MessageBoxButtons.YesNo) != DialogResult.Yes)
            return;

        GridView view = sender as GridView;   //AQUI ES EL ERROR
        view.DeleteRow(view.FocusedRowHandle);
        pacienteTableAdapter.Update(dBDataSet);

    }           
}

private void gridView1_KeyDown(object sender, KeyEventArgs e) 
{
    if (e.KeyCode == Keys.Delete)
    {
        if (MessageBox.Show("Desea eliminar?", "Confirmación", MessageBoxButtons.YesNo) != DialogResult.Yes)
            return;

        GridView view = sender as GridView;
        view.DeleteRow(view.FocusedRowHandle);
        pacienteTableAdapter.Update(dBDataSet);
    }
}
msarchet
  • 15,104
  • 2
  • 43
  • 66
user1813375
  • 101
  • 3
  • 12
  • Your question is very unclear. Please edit it to clarify - in particular, what are you actually *asking*? – Jon Skeet Jan 19 '13 at 16:37

2 Answers2

1

Have you tried to use "gridView1" instead of "view" (cast from "sender")?

Mats Magnem
  • 1,375
  • 1
  • 10
  • 21
1

The reason is that in the gridView1_KeyDown event the sender is gridView1 so when you cast to a GridView - all is well.

In gridControl1_EmbeddedNavigator_ButtonClick event your sender is gridControl1, thus, when you cast from sender you get null.

Try either casting to GridControl and then getting Views or renaming gridView1 to a more explanatory name and referencing it directly.

E.T.
  • 949
  • 8
  • 21