-1

Hi to all,

I like to reach all the cells by column name in a loop( foreach...) and
compare the content of the cell in datetime type and add a value to another cell depending on the result of the comparison. I do this for all the populated rows.
Question is how do I locate a particular cell in each row.

Thanks in advance
snakay

Comments
4
Contributors
7
Replies
8
Views
7 Years
Discussion Span
Last Post by snakay
0

Hi snakay, welcome to DANIWEB.

A DataGridView has a Rows property which is of type DataGridViewRowCollection. You could use that if you want.
Usually I just index like this : dataGridView[0, 2].Value = something...
Which is the first cell of the third row.

0

Hi ddanbe,

with - dataGridView[0, 2].Value = something... - you are assuming that you know the indexes of row and column. If it is sequential reading of grid with foreach, I mean the whole table, it should come from program. And column index I have to find it by giving the column name. So it is current row and index by column name. I hope I made myself clear.

thanks
snakay

0

Try this:

private void button3_Click(object sender, EventArgs e)
    {

      StringBuilder sb = new StringBuilder();
      foreach (DataGridViewColumn col in dataGridView1.Columns)
      {
        sb.AppendLine(col.Name);
      }
      MessageBox.Show("Column Names: " + Environment.NewLine + sb.ToString());


      foreach (DataGridViewRow gridRow in dataGridView1.Rows)
      {
        if (gridRow.IsNewRow)
          continue;

        string nameLast = Convert.ToString(gridRow.Cells["nameLastDataGridViewTextBoxColumn"].Value);
        int custId = Convert.ToInt32(gridRow.Cells["custNumberDataGridViewTextBoxColumn"].Value);
      }
    }
0

Try to search or manipulate data in datasource object (DataTable) instead of ViewControls (datagridView).

0

thank you very much sknake,

I did the job perfect, took sometime becouse confused with continue after the new row test.Good example

regards
snakay

0

You're welcome.

Please mark this thread solved as you have found an answer to your issue and good luck!

0

Like to attach what I did just in case someone needs;

// right after populating dataGridView

StringBuilder sb = new StringBuilder();
                   foreach (DataGridViewColumn col in myGridView.Columns)
                    { 
                       sb.AppendLine(col.Name); 
                    } 
                    MessageBox.Show("Column Names: " + Environment.NewLine + sb.ToString());

                   foreach (DataGridViewRow r in myGridView.Rows)
DateTime dt = Convert.ToDateTime(r.Cells["colDate"].Value);
TimeSpan MyDays = (Convert.ToDateTime(txtBoxDate.Text)).Subtract(dt);
 int MyMonths = ((Convert.ToDateTime(txtBoxDate.Text.Text)).Month) - ((dt).Month);
This question has already been answered. Start a new discussion instead.
Be sure to adhere to our posting rules.