scitch
scitch

Reputation: 65

Select/Decelect Row in DataGridView

I'm trying to insert/update Data into a DataGridView but ran into a small Problem.

In the DataGridVieware certain cells with a date and a starting time (datetime). Now what i'm trying to do is when a button is pressed the current date should be searched for, the row selected, then the start Time should be read and with the current time a TimeSpan should be calculated.

Right now i'm just trying to get the row selected to take out the data of the starttime cell.

var Today = DateTime.Now.ToShortDateString();

        dataGridView1.SelectedRows.Clear();

        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            if (row.Cells[0].Value.Equals(Today))
            {
                row.Selected = true;
            }
        }

But this gives me an error that the Listing is read only...i'm not really sure what i did wrong here?

So i'd appreaciate it if someone could help me with this or give me a tip on how to solve this Problem.

Thanks in advance to everyone. :)

Upvotes: 3

Views: 831

Answers (1)

Reza Aghaei
Reza Aghaei

Reputation: 125197

If you try to clear selection in your DataGridView using dataGridView1.SelectedRows.Clear(); you will receive an exception: Operation not supported. Collection is read-only.

To clear selection, you can use ClearSelection method:

dataGridView1.ClearSelection();

Upvotes: 7

Related Questions