0

How can I change the value of data lines in the third colomn of the dgv, with this code,I can change the value of all lines in the third column of the dgv but I want only the lines in which I have data.

private void button4_Click(object sender, EventArgs e)
        {
            DateTime dt = DateTime.Now;
            dataGridView1.Rows.Add("" + dt.Year.ToString() + "", "En cours", "0");


            if (dataGridView1.SelectedRows.Count == 1)
            {
                DataGridViewRow row = dataGridView1.SelectedRows[0];

                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                   // int ik = (int)dataGridView1.Rows[i].Cells[3].Value;
                    dataGridView1.Rows[i].Cells[2].Value = 1;

                }
            }

How can I do it?

Edit: I tried to test the value of every cell in the rows if is it null I will break the foreach :

private void button4_Click(object sender, EventArgs e)
        {
            DateTime dt = DateTime.Now;
            dataGridView1.Rows.Add("" + dt.Year.ToString() + "", "En cours", "0");


            if (dataGridView1.SelectedRows.Count == 1)
            {
                DataGridViewRow row = dataGridView1.SelectedRows[0];

                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    // int ik = (int)dataGridView1.Rows[i].Cells[3].Value;
                    foreach (DataGridViewRow row1 in dataGridView1.Rows)
                    {
                        foreach (DataGridViewCell cell in row1.Cells)
                        {
                            if (cell.Value != null)
                            {
                                dataGridView1.Rows[i].Cells[2].Value = 1;
                            }
                            else  //if (string.IsNullOrEmpty( cell.Value.ToString()))
                            {
                                break;
                            }


                        }
                    }
                }
            }
        }

but I don't know why I still have the same result all the rows have the value "1" in the dataGridView any Help pleaze?

Lina
  • 451
  • 1
  • 8
  • 23
  • thanks Saverio Terracciano for the editing and excuse me for the errors :) – Lina Jun 08 '14 at 22:05
  • What do you mean with "I want only the lines in which I have data."? – Horaciux Jun 08 '14 at 22:20
  • I have a dataGidView in witch I have 4 lines as a result from my database's table,when I apply the code above all lines of the dataGridView(5 lines) are modified but I want only the 4 lines result rom my database :) – Lina Jun 08 '14 at 22:45
  • Are you allowing users to add rows? Where does 5th row came from? – Horaciux Jun 08 '14 at 23:02
  • yes my application is a database management in which users can add or delete informations from the dataGridView – Lina Jun 08 '14 at 23:33

0 Answers0