2

I'm trying to match user input in TextBox1 to values of first column in a DataGridView and if the value matched first column of a row, then return the value of second column in that row.

For example, I have these 2 rows of data:

DataGridView1.Rows.Add(new object[] { 80000f, 1.000f, 1.120f });
DataGridView1.Rows.Add(new object[] { 85000f, 1.044f, 1.158f });

If the user enters 80000 into TextBox1 I want to return 1.000

How can I do this?

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
Joe Cambareri
  • 115
  • 2
  • 13

1 Answers1

3

If I understand your question correctly:

var rseult= this.dataGridView1.Rows.Cast<DataGridViewRow>()
                .Where(r=>(float)r.Cells[0].Value==float.Parse(textBox1.Text))
                .Select(r=>(float)r.Cells[1].Value)
                .FirstOrDefault();

The result for 80000 is 1.000
The result for 85000 is 1.044

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • It could cause problems to compare floats without epsilon, see http://stackoverflow.com/questions/1088216/whats-wrong-with-using-to-compare-floats-in-java or http://stackoverflow.com/questions/10334688/how-dangerous-is-it-to-compare-floating-point-values – Tomas Kubes Oct 12 '15 at 20:58
  • @qub1n I think C# `==` operator works fine. I tested `1.00==float.Parse("1.0000000")` and `1f==float.Parse("1.0000000")` and the result was true. – Reza Aghaei Oct 12 '15 at 21:07
  • Yes, that it is, install the provider from Nuget. I entity framework specific setting will appear in web.config. – Tomas Kubes Oct 13 '15 at 18:08