Grid Row Comparisons

I’m trying to compare a grid value in a column to a value in a number box. I have the code below but I’m geeting this error:

private void gridInvoiceEntryGrid_DoubleClickRow(object sender, Infragistics.Win.UltraWinGrid.DoubleClickRowEventArgs args)
	{
		UltraGridRow activeRow = gridInvoiceEntryGrid.ActiveRow;
		epiTextBoxC7.Text = activeRow.Cells["Customer_CustID"].Value.ToString();
		foreach (UltraGridRow row in this.grdList.Rows.GetRowEnumerator(GridRowType.DataRow, null, null))
		{
		if (row["InvcHead_InvoiceNum"].ToString() == (neInvoiceNumberfromMemos.Value.ToString()))
			{
				MessageBox.Show("Invoice Number matches");
			}
			else
			{
				MessageBox.Show("Invoice Number Doesn't Match");
			}
		MessageBox.Show("Row");
		}

	}'```

Check out the GetCellValue() or GetCellText() methods for the UltraGridRow.

Something like this:

foreach (UltraGridRow row in this.grdList.Rows)
{
    if (row.GetCellText("InvcHead_InvoiceNum") == neInvoiceNumberfromMemos.Value.ToString())
    {
        EpiMessageBox.Show("Invoice Number matches.");
    }
    else
    {
        EpiMessageBox.Show("Invoice Number doesn't match.");
    }
}
1 Like

This is still not working. I keep gettting the message box about the numbers not matching. Any ideas?
How would I check to see what the value is for the Cell?

private void gridInvoiceEntryGrid_DoubleClickRow(object sender, Infragistics.Win.UltraWinGrid.DoubleClickRowEventArgs args)
	{
		UltraGridRow activeRow = gridInvoiceEntryGrid.ActiveRow;
		neInvoiceNumberfromMemos.Value = activeRow.Cells["InvcHead_InvoiceNum"].Value.ToString();
		foreach (UltraGridRow row in this.grdList.Rows.GetRowEnumerator(GridRowType.DataRow, null, null))
		{
		if (row.GetCellValue("InvoiceNum") == neInvoiceNumberfromMemos.Value)
			{
				MessageBox.Show(neInvoiceNumberfromMemos.Value.ToString());
				MessageBox.Show("Invoice Number matches");
			}
			
		MessageBox.Show("Invoice Number doesn't match");
		MessageBox.Show(neInvoiceNumberfromMemos.Value.ToString());
		}

	}

If they are both numbers, compare the numbers, not the text. 1.0000 != 1.00 != 1

1 Like

Shouldn’t those last two MessageBox lines be in an else?

	if (row.GetCellValue("InvoiceNum") == neInvoiceNumberfromMemos.Value)
		{
		MessageBox.Show(neInvoiceNumberfromMemos.Value.ToString());
		MessageBox.Show("Invoice Number matches");
		}
	else
		{		
		MessageBox.Show("Invoice Number doesn't match");
		MessageBox.Show(neInvoiceNumberfromMemos.Value.ToString());
		}
	}

Okay, How do I do that?

They are both integers?

  1. Determine the data types (int, decimal, etc)
  2. do the GetCellValue as described above
    3.Cast the results of GetCellValue (and Value) to the proper type decimal one = (decimal)myCell.Value, etc
  3. Compare the numbers one == two

Okay, they are both integers. I don’t know how to cast a field. Sorry I’m a newbie to C# coding.

(int)row.GetCellValue(“InvoiceNum”) == (int)(neInvoiceNumberfromMemos.Value)

Here is the most direct way

Alright, I’m getting this error:


	private void gridCreditCardExceptions_DoubleClickRow(object sender, Infragistics.Win.UltraWinGrid.DoubleClickRowEventArgs args)
	{
		UltraGridRow activeRow = gridCreditCardExceptions.ActiveRow;
		neInvoiceNumberfromMemos.Value = activeRow.Cells["InvcHead_InvoiceNum"].Value.ToString();
		foreach (UltraGridRow row in this.grdList.Rows.GetRowEnumerator(GridRowType.DataRow, null, null))
		{
		if ((int)row.GetCellValue("InvoiceNum") == (int)(neInvoiceNumberfromMemos.Value))
			{
				MessageBox.Show(neInvoiceNumberfromMemos.Value.ToString());
				MessageBox.Show("Invoice Number matches");
			}
			else
			{
			MessageBox.Show("Invoice Number doesn't match");
			MessageBox.Show(neInvoiceNumberfromMemos.Value.ToString());
		}
	}
	}

try

(int32)

Are you sure they are both ints? I suppose one could be null.

Also, as Calvin mentioned, if you dont put those message in an else, they are always going to show.

Instead of just casting the type, actually use the Int32.Parse() function

		if (Int32.Parse(row.GetCellValue("InvoiceNum") == Int32.Parse(neInvoiceNumberfromMemos.Value))

edit:

Whoops errors in the above (missed a closing parenthesis). Should be

		if (Int32.Parse(row.GetCellValue("InvoiceNum")) == Int32.Parse(neInvoiceNumberfromMemos.Value))

I can’t compile. I’m getting this:
Error: CS1502 - line 205 (4592) - The best overloaded method match for ‘int.Parse(string)’ has some invalid arguments
Error: CS1503 - line 205 (4592) - Argument 1: cannot convert from ‘object’ to ‘string’
Error: CS1502 - line 205 (4592) - The best overloaded method match for ‘int.Parse(string)’ has some invalid arguments
Error: CS1503 - line 205 (4592) - Argument 1: cannot convert from ‘object’ to ‘string’

Newest code:
if (Int32.Parse(row.GetCellValue(“InvoiceNum”)) == Int32.Parse(neInvoiceNumberfromMemos.Value))

This is my test grid and both items have data that match invoice numbers. And I changed the message box. I can’t get past the if statement now.

nvm…I got it working. Now I just have to figure out how to activate the row that matches.

1 Like
if (Int32.Parse(row.GetCellValue("InvoiceNum").ToString()) == Int32.Parse(neInvoiceNumberfromMemos.Value.ToString()))
1 Like

Just tossing this out for the sake of knowledge -

Int32.TryParse() is, in my opinion, a safer way to perform the conversion because it has all the error handling built in. This is my go-to shortcut unless there’s some specific error handling I need to apply.

Example:

string s = "";
int i;

Int32.TryParse(s, out i);	
/* result: i = 0 */

i = Int32.Parse(s);
/* result: System.FormatException */

Int32.TryParse() also returns a bool so you can safely use it to conditionally work with a string converted to an int when successful.

1 Like