none
datagridview doesn't update properly after changing binded table RRS feed

  • Question

  • <i originally asked this post in a different area, but no answer, so i'm trying a different forum>

    I'm working on a project that's based off a datagridview that binds to a datatable in an xml dataset. I'm having this strange issue. Basically I update the datatable through code, and I want the changes to be reflected back in the datagridview. This works sometimes, however other times the grid won't update. Usually the grid won't update if I programmatically change a row that is currenlty selected in the grid (but if i go back to the grid, deselect the row and highlight another row, the datagridview will update itself). One way I temporarily got around this was to manually change the row selection before I change the data, but this is a hack and doesn't work (as sometimes the data change will effect multiple rows).

    My code is below...


    //code in main form

    private void next_Click(object sender, EventArgs e)

    {
    //pcnpc_grid.Update();

    //pcnpc_grid.CurrentCell = pcnpc_grid.Rows[4].Cells[1];

    _initiative_list.Next();

    //pcnpc_grid.CurrentCell = pcnpc_grid.Rows[4].Cells[1];

    this.pcnpc_grid.Refresh();

    }

    //code in class that contains datatable (in dataset)

    public void Next()

    {

    DataView SortedInit = pcnpcs.pcnpcs.DefaultView;

    //SortedInit.RowFilter = "done <> 'X'";

    SortedInit.Sort = "done,init desc";

    SortedInit[0]["done"] = "2";

    Debug.WriteLine(SortedInit[0]["pcnpcs"]);

    Debug.WriteLine(SortedInit[0]["done"]);

    OrderOnInit();

    }

    So what i'm wondering is what is the correct way to update a datagridview after you've altered data in a xml dataset it's binded too. Should I use some command to deselect all the items? I also notice that if I call update after the dataset has been changed, soemtimes this will undo changes as the grid hasn't changed (make sense?). Also, if you notice anything else that looks blatantly wrong let me know.

    I've seen things like a .endedit method or something. Does that have to be called before update? I'm just not sure.

    Any help would be greatly appreciated.

    Tuesday, June 28, 2005 3:28 PM

Answers

  • Yes, you have to call EndCurrentEdit on the CurrencyManager behind the controls' bindings.  Add this code to the Validation event handlers for the controls that edit the data in the grid:


    this.BindingContext[yourDataTable].EndCurrentEdit();



     



    Tuesday, June 28, 2005 5:34 PM

All replies

  • Yes, you have to call EndCurrentEdit on the CurrencyManager behind the controls' bindings.  Add this code to the Validation event handlers for the controls that edit the data in the grid:


    this.BindingContext[yourDataTable].EndCurrentEdit();



     



    Tuesday, June 28, 2005 5:34 PM
  • Thank you soooo much for that!!!!

    Worked like a charm

    Thursday, July 6, 2006 12:20 PM