DataTable searching a row with row value

I need to sum the data in 4 columns in the row that belong to today's date.

Tarih column is a Generic Formatted Data column. (44574 is today's date as generic format.)

Here's my table:

Tarih |Pres1Hedef|Pres2Hedef|Pres3Hedef|Pres4Hedef
------+-----------+---------+----------+-----------
44561 | 142      | 145      |  154     |  114
44562 | 120      | 110      |  114     |  110
44563 | 110      | 170      |  115     |  143
44564 | 110      | 130      |  146     |  154
44565 | 110      | 200      |  171     |  110
44566 | 110      | 214      |  201     |  204
44567 | 100      | 150      |  174     |  143

Here's my source code:

OleDbConnection excelBag = new OleDbConnection(excelAyar.ConnectionString); excelBag.Open();
OleDbDataAdapter adap = new OleDbDataAdapter("SELECT F2 as Tarih,F32 as Pres1Hedef,F34 as Pres2Hedef,F36 as Pres3Hedef,F38 as Pres4Hedef FROM [" + excelSayfaAdi + "$]", excelBag);
DataTable dt = new DataTable(); adap.Fill(dt);
dataGridView1.DataSource = dt;

string Gun1HedefPres1 = dt.Rows[9]["Pres1Hedef"].ToString();
string Gun1HedefPres2 = dt.Rows[9]["Pres2Hedef"].ToString();
string Gun1HedefPres3 = dt.Rows[9]["Pres3Hedef"].ToString();
string Gun1HedefPres4 = dt.Rows[9]["Pres4Hedef"].ToString();
double Gun1Hedef = Convert.ToDouble(Gun1HedefPres2) + Convert.ToDouble(Gun1HedefPres1) + Convert.ToDouble(Gun1HedefPres3) + Convert.ToDouble(Gun1HedefPres4);
textBox1.Text = Convert.ToInt32(Gun1Hedef).ToString();

What im trying to do is find values like that (Finding value with row value, not row index):

string Gun1HedefPres1 = dt.Rows["Tarih : 44561].["Pres1Hedef"];

I know there is no such thing as above. But how can i do what i want?

Any suggestions? Thanks.


The DataTable class has a Select method that allows you to pass a condition like a WHERE statement in SQL. The method returns an array of DataRow (zero or more) that match the condition.

From the resulting array of DataRows you can easily do you math.

   double Gun1Hedef = 0.0;
   string valueToSearchFor = "44561"; // Get this from your inputs

   var rows = dt.Select($"Tarih = {valueToSearchFor}");

   // Assuming that you have only one row with  valueToSearchFor,
   // otherwise you need a loop over the rows array
   if(rows.Length == 1)
   {
       // Field<T> is an extension method available from 
       // DataSetExtensions assembly, you need a reference 
       Gun1Hedef += rows[0].Field<double>("Pres1Hedef");
       Gun1Hedef += rows[0].Field<double>("Pres2Hedef");
       Gun1Hedef += rows[0].Field<double>("Pres3Hedef");
       Gun1Hedef += rows[0].Field<double>("Pres4Hedef");
   }

Note that this assumes that the Terih field is some numeric type. If not we need to apply different conditions for Date or Strings.