02 January 2013

Compare Two Cell Values in Datagridview using C#.Net

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
 {
if (e.ColumnIndex == 2)
{
 int minValue = Convert.ToInt32(dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["MinValue"].Value.ToString()); int maxValue = Convert.ToInt32(dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["MaxValue"].Value.ToString()); bool chkValue = compareTwoCellValues(minValue, maxValue);
if (chkValue)
{
 dataGridView1.AllowUserToAddRows = false; dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["MaxValue"].Selected = true; chkValue = false;
}
 else
 {
 dataGridView1.AllowUserToAddRows = true; chkValue = false;
 }
}
}
bool statusofCompareValue;
bool compareTwoCellValues(int MinValue, int MaxValue)
{
 if (MinValue > MaxValue)
 {
 MessageBox.Show("Please enter greater than value"); statusofCompareValue = true;
 }
 else
 {
statusofCompareValue = false;
}
 return statusofCompareValue;
 }

No comments: