Hi smile,
Refer below sample.
Code
C#
private void Form1_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] { new DataColumn("AdmissionNo", typeof(string)), new DataColumn("Name", typeof(string)) });
dt.Rows.Add("R-00001", "Mudassar");
dt.Rows.Add("R-00002", "John");
dt.Rows.Add("R-00003", "Robert");
dGVMarks.DataSource = dt;
DataGridViewCheckBoxColumn checkBoxColumn = new DataGridViewCheckBoxColumn();
checkBoxColumn.HeaderText = "";
checkBoxColumn.Width = 30;
checkBoxColumn.Name = "checkBoxColumn";
dGVMarks.Columns.Insert(0, checkBoxColumn);
DataGridViewTextBoxColumn txtMax = new DataGridViewTextBoxColumn();
txtMax.HeaderText = "Max";
txtMax.Name = "Max";
txtMax.Width = 95;
dGVMarks.Columns.Insert(3, txtMax);
DataGridViewTextBoxColumn txtMark = new DataGridViewTextBoxColumn();
txtMark.HeaderText = "Mark";
txtMark.Name = "Mark";
txtMark.Width = 75;
dGVMarks.Columns.Insert(4, txtMark);
}
private void dGVMarks_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
if (e.ColumnIndex > 1)
{
if (!string.IsNullOrEmpty(dGVMarks.Rows[e.RowIndex].Cells["Mark"].EditedFormattedValue.ToString()) && !string.IsNullOrEmpty(dGVMarks.Rows[e.RowIndex].Cells["Max"].EditedFormattedValue.ToString()))
{
int maxMark = Convert.ToInt16(dGVMarks.Rows[e.RowIndex].Cells["Max"].EditedFormattedValue);
int obtainMark = Convert.ToInt16(dGVMarks.Rows[e.RowIndex].Cells["Mark"].EditedFormattedValue);
if (obtainMark > maxMark)
{
MessageBox.Show("obtained marks should be less than max Marks");
}
}
}
}
VB.Net
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn() {New DataColumn("AdmissionNo", GetType(String)), New DataColumn("Name", GetType(String))})
dt.Rows.Add("R-00001", "Mudassar")
dt.Rows.Add("R-00002", "John")
dt.Rows.Add("R-00003", "Robert")
dGVMarks.DataSource = dt
Dim checkBoxColumn As DataGridViewCheckBoxColumn = New DataGridViewCheckBoxColumn()
checkBoxColumn.HeaderText = ""
checkBoxColumn.Width = 30
checkBoxColumn.Name = "checkBoxColumn"
dGVMarks.Columns.Insert(0, checkBoxColumn)
Dim txtMax As DataGridViewTextBoxColumn = New DataGridViewTextBoxColumn()
txtMax.HeaderText = "Max"
txtMax.Name = "Max"
txtMax.Width = 95
dGVMarks.Columns.Insert(3, txtMax)
Dim txtMark As DataGridViewTextBoxColumn = New DataGridViewTextBoxColumn()
txtMark.HeaderText = "Mark"
txtMark.Name = "Mark"
txtMark.Width = 75
dGVMarks.Columns.Insert(4, txtMark)
End Sub
Private Sub dGVMarks_CellValidating(ByVal sender As Object, ByVal e As DataGridViewCellValidatingEventArgs) Handles dGVMarks.CellValidating
If e.ColumnIndex > 1 Then
If Not String.IsNullOrEmpty(dGVMarks.Rows(e.RowIndex).Cells("Mark").EditedFormattedValue.ToString()) AndAlso Not String.IsNullOrEmpty(dGVMarks.Rows(e.RowIndex).Cells("Max").EditedFormattedValue.ToString()) Then
Dim maxMark As Integer = Convert.ToInt16(dGVMarks.Rows(e.RowIndex).Cells("Max").EditedFormattedValue)
Dim obtainMark As Integer = Convert.ToInt16(dGVMarks.Rows(e.RowIndex).Cells("Mark").EditedFormattedValue)
If obtainMark > maxMark Then
MessageBox.Show("obtained marks should be less than max Marks")
End If
End If
End If
End Sub
Screenshot