Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
343 views
in Technique[技术] by (71.8m points)

c# - How do I make a DataGridView immediately commit edits?

I have a master-detail layout with a section of popup menus (the Details) and a section with a DataGridView which holds the rows.

The popup-menu state is updated when the selected row in the DataGridView changes and the state in the DGV's selected row should update when the popup-menu changes.

All of this works except the row in the DataGridView doesn't immediately update when I change the value of the popup-menu. I have to select a different row in order to see my edits.

I'm assuming this is because the edit hasn't been committed until the selection changes.

My question is: How do I make the change to the popup become immediately reflected in the DataGridView?

I have experimented with calling EndEdit() in the SelectionChangeCommitted handler for the popup-menu, but this has no effect. I'm interested in a technique that would allow me to create a DataGridView that would behave as if there were no Undo mechanism to begin with. Ideally the solution would be generic and transplantable to other projects.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It looks like existing answers work well with BindingSource. In my case, where DataTable was directly used as a DataSource, they didn't work for some reason.

// Other answers didn't work in my setup...
private DataGridView dgv;

private Form1()
{
   var table = new DataTable();
   // ... fill the table ...
   dgv.DataSource = table;
}

After some hair-pulling, I got it work without adding BindingSource indirection:

// Add this event handler to the DataGridView
private void dgv_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    dgv.BindingContext[dgv.DataSource].EndCurrentEdit();
}

private Form1()
{
   dgv.CellEndEdit += dgv_CellEndEdit;
   // ...
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...