when user edit a cell value in the event dataGridViewStudents_CellValueChanged. Data grid values are sort accordingly.
private void form_Load(object sender, EventArgs e)
{
List<student> lststudent=new List<student>();
lststudent.add("1","Abc", 26);
lststudent.add("1","xyz", 31);
lststudent.add("1","pqr", 53);
lststudent.add("1", "def", 23);
DataGridView.DataSource= lststudent;
}
private void datagridStudent_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
DataGridViewCell cell = null;
if (e.RowIndex > -1 && e.ColumnIndex > -1)
{
cell = ((DataGridView)sender).Rows[e.RowIndex].Cells[e.ColumnIndex];
((DataGridView)sender).Rows[e.RowIndex].Cells[2].Value = 36; ((DataGridView)sender).Sort(((DataGridView)sender).Columns["marks"], ListSortDirection.Ascending);
}
}
In this code when user edit a cell in datagrid. it does not sort the datagrid according to that column. datagrid is bind with list. so, i want to sort the datagrid when user change cell value.
1 Answer