datagridview not updating correctly (row selection blocking?)

I'm working on a project that's based off a datagridview that binds to an 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. 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.

Any help would be greatly appreciated.



Answer this question

datagridview not updating correctly (row selection blocking?)

  • John Bledsoe

    In this case you need to cause the edit you make to be committed which notifies the DataGridView that you made the change. This is due to the fact that the DataGridView starts an edit for the current row that is selected and this keeps the grid from being notified of your change. 

    To commit the edit you can call EndCurrentEdit on the CurrencyManager like so:

    this.BindingContext[customDataTable].EndCurrentEdit();
     


    Hope this helps,
    -mark
    DataGridView Program Manager
    Microsoft
    This post is provided "as-is"

  • datagridview not updating correctly (row selection blocking?)