Reputation: 3
i have a datagridview that when i click a DataGridViewButtonColumn, a cell value change. but it doesnt. how can i do it? this datasource is entity framework when i click button search and fill 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;
}
}
Upvotes: 0
Views: 70
Reputation: 3217
The Problem:
You don't seem to Bind on a ViewModel but directly on a Model. And i guess your model does not implement INotifyPropertyChanged so your GUI has no chance to be notified, when a property (in that case your CellValue) is updated.
Source: https://en.wikipedia.org/wiki/File:MVVMPattern.png
It looks like you lack the ViewModel-Part of the image above.
Solution:
You should bind the ItemSource of your DataGrid onto a ObservableCollection where ViewModel implements INotifyPropertyChanged. The ViewModel should represent a single row in your grid.
Further reading: https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93viewmodel
Further reading (InotifyPropertyChanged): https://msdn.microsoft.com/en-us/library/ms229614(v=vs.100).aspx
Upvotes: 1