1

I have two DataGridViewTextbox Column in my Form. I want to compare one textbox with another textbox value.

See the image the information is provided there...,

which are in blue color they are readonly ......I tried and i get the answer

Anjali
  • 1,680
  • 4
  • 26
  • 48

2 Answers2

1

try this

if (DataGridView.Rows[0].Cells["Quantity"].Value > DataGridView.Rows[0].Cells["other"].Value)
{
//do something
}
Guru
  • 98
  • 7
  • Hi @Amir i am not asking the cell value.the sum of Quantity issued is not greater than the value of the consumable required – Anjali May 28 '13 at 10:51
0

Attach your grid to CellValidating event:

private void GridCellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
    if (e.ColumnIndex == ColQuantityIssues.Index)  // Quantity Issues TextBox value has changed
    {
        int quantityIssued = 0;

        if (!Int32.TryParse(e.FormattedValue.ToString(), out quantityRequired))  //value is not a valid number
        {
            e.Cancel = true;
        }

        int quantityRequired = Int32.Parse(dgv.Rows[e.RowIndex].Cells[ColQuantityRequired.Index].Value.ToString());

        if (quantityRequired < quantityIssued)
        {
            e.Cancel = true;
        }
    }
}

Event args has FormattedValue which is the new value user has entered. Setting e.Cancel to true won't allow user to leave current cell as long as value is not valid one.

You may change my ColQuantityIssues and ColQuantityRequired to access them by name (i.e. dgv.Rows[e.RowIndex].Cells["Required"]) but that should work.

gzaxx
  • 17,312
  • 2
  • 36
  • 54