[英]how can i change the value of a cell datagridview
我有一个datagridview,当我单击DataGridViewButtonColumn时,单元格值发生了变化。 但事实并非如此。 我该怎么做? 当我单击按钮搜索并填充datagridview时,此数据源是实体框架。
private void dataGridViewBook_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
var senderGrid = (DataGridView) sender;
if (senderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn &&
e.RowIndex >= 0)
{
int countSelectedBook = Convert.ToInt32(dataGridViewBook.CurrentRow.Cells[5].Value);
countSelectedBook = countSelectedBook - 1;
dataGridViewBook.Rows[e.RowIndex].Cells[5].Value = countSelectedBook;
}
}
问题:
您似乎不是在ViewModel上绑定,而是直接在Model上绑定。 而且我猜想您的模型没有实现INotifyPropertyChanged,因此当属性(在这种情况下为CellValue)更新时,您的GUI没有机会被通知。
资料来源: https : //en.wikipedia.org/wiki/File : MVVMPattern.png
您似乎缺少上图的ViewModel-Part。
解:
您应该将DataGrid的ItemSource绑定到ObservableCollection上,其中ViewModel实现INotifyPropertyChanged。 ViewModel应该代表网格中的一行。
进一步阅读: https : //zh.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93viewmodel
进一步阅读(InotifyPropertyChanged): https ://msdn.microsoft.com/zh-cn/library/ms229614(v = vs.100).aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.