Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Solve "does not contain a definition for 'OfType'" error message?

Tags:

c#

This is my code and appear an unknown error:

private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
        for (int i = 0; i < dataGridView1.Rows.Count; ++i)
        {

            object a = dataGridView1.Rows[i].Cells["price"].Value;
            object b = dataGridView1.Rows[i].Cells["tedad"].Value;
            double aNumber = 0;
            double bNumber = 0;
            if (a != null)
                aNumber = Double.Parse(a.ToString());
            if (b != null)
                bNumber = Double.Parse(b.ToString());
            dataGridView1.Rows[i].Cells["total"].Value = aNumber * bNumber;
            dataGridView1.RefreshEdit();
        }
        decimal sum = dataGridView1.Rows.OfType<DataGridViewRow>()
           .Sum(row => Convert.ToDecimal(row.Cells["total"].Value));
        txtDate.Text  = sum.ToString();

an error like this meaning for OfType():

'System.Windows.Forms.DataGridViewRowCollection' does not contain a definition for 'OfType' and no extension method 'OfType' accepting a first argument of type 'System.Windows.Forms.DataGridViewRowCollection' WindowsFormsApplication1test

like image 878
Elnaz Avatar asked Sep 09 '13 16:09

Elnaz


1 Answers

Make sure you have :

using System.Linq;

Enumerable.OfType Method

  • Namespace: System.Linq
  • Assembly: System.Core (in System.Core.dll)

enter image description here

like image 163
Habib Avatar answered Sep 28 '22 05:09

Habib