0

I want to check if values of datatable of the same column are equals to "int" or not , so if it's true i want to calculate with content value the sum. This is my code which return always when i click on sum button "pas tous entier" . Thank you in advance!

private void button7_Click(object sender, EventArgs e)
    {
        int i = 0, s = 0;
        String type ="int";

        DataTable dt = new DataTable("Table_insertion");

        bool exists = dt.AsEnumerable().Any(row => type == row.Field<String>("Type"));
        if (exists== true)
        {
            for (i = 0; i < dataGridView1.Rows.Count; ++i)
            {
                s += Convert.ToInt32(dataGridView1.Rows[i].Cells[2].Value);

            }
            label5.Text = s.ToString();           
        } 
        else
        {
            MessageBox.Show("pas tous entiers");
        }

    }
ninpi
  • 17
  • 7
  • Is it working when all values actually are integers, but throwing an exception if the string cannot be interpreted as an integer? – John Spiegel Nov 11 '19 at 20:25
  • Looks like you are wanting to do something similar to [How to check if a number is an integer in .NET?](https://stackoverflow.com/questions/194089/how-to-check-if-a-number-is-an-integer-in-net) – jazakari Nov 11 '19 at 20:31
  • @John Spiegel when all values integers it works (without adding the bool variable and the if condition ) but if i add the condition it return always when i click on the sum button "pas tous entiers".when i add to the datatable a string it shows me an error! – ninpi Nov 11 '19 at 20:42
  • @jazakari no , i want to check string value named "int" in datatable – ninpi Nov 11 '19 at 20:47
  • `DataTable dt = new DataTable("Table_insertion");` <== That will _**never**_ have any data in it. – Joel Coehoorn Nov 11 '19 at 20:53
  • @Joel Coehoorn I'm sorry i didn't understand what you said perfectly, so how can i get data from that table? – ninpi Nov 11 '19 at 21:17
  • The code in the question creates a brand new empty table with no rows and no columns. There's no data there yet to get. – Joel Coehoorn Nov 11 '19 at 21:51
  • @Joel Coehoorn no i have already a table named Table_insertion with data , i didn't know how can i call it! – ninpi Nov 11 '19 at 22:05
  • This may be true: you may have that table. But the code in this question is looking at a **different** datatable instance which just happens to use the same name, and has no rows or columns. To help you look at the right table, we need to know more about where your other table is located. – Joel Coehoorn Nov 11 '19 at 22:09

2 Answers2

1

The datatable seems unnecessary here.

Maybe the following is enough.

for (i = 0; i < dataGridView1.Rows.Count; ++i)
{
  var str = dataGridView1.Rows[i].Cells[2].Value?.ToString();

  if( !string.IsNullOrEmpty(str) && Int32.TryParse(str, out var parsed)
  {
     s += parsed;
  }
}

If you want to check the type of the column in a datatable you can check its DataType.

foreach (var col in datatable1.Columns) 
{
      if ( col.DataType == typeof(System.Int16) || col.DataType == typeof(System.Int32)) // Or other types 
      {
           ....

tymtam
  • 20,472
  • 3
  • 58
  • 92
0

Are you just trying to evaluate if the string returned contains only numbers? If so, you may want to use a regex match on the data. There is a great example here: Regex for numbers only

RobertC
  • 311
  • 1
  • 7
  • no i want just to check the string value named "int" if it exists in rows of the column named "type" or not – ninpi Nov 11 '19 at 21:08