开发者

Update DataGridView DataSource Immediately When TextBox Value Changed

I have a simple DataGridView that is bound to a list of business objects (with just one editable field per object). I need any changes that the user makes to the value (which is in a DataGridViewTextBoxColumn) to 开发者_如何学JAVAupdate the bound object. That is, cause an update for every keypress in the same way that is available when binding a normal textbox and using the 'OnPropertyChanged' option.

The reason for this is that I simply need to enable the 'OK' button on the form when the bound object's 'IsDirty' property is true.

There seems to be many discussions on various websites about doing this for checkbox columns and using the CurrentCellDirtyStateChanged event and calling CommitEdit. I have tried this but this had the unfortunate side effect of clearing the cell of all text on the first keypress!

Any help much appreciated, thanks.


It sounds like what you want is to enable the button as soon as the user starts typing in any cell? Does the DataSource actually need to be updated before the button is clicked?

If not then CurrentCellDirtyStateChanged should work fine for that - just don't call the CommitEdit method, instead set your button to enabled there.

When the cell loses focus the changes will be applied to the DataSource - so when the button is clicked you will be able to work on the correct data.


I think I may have found a solution - not 100% sure since I don't have the MVC controller you have to wire up the IsDirty change and see it all work - I did put a break point in a property setter on my DataSource object and it gets fired with the following code and not without it, so I think this works.

It simply used the CurrentCellDirtyStateChanged and the CurrentCellChange enum for the DataErrorContext parameter.

void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
    dataGridView1.CommitEdit(DataGridViewDataErrorContexts.CurrentCellChange);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜