jacobz
jacobz

Reputation: 3291

DataGridView, Get the value of the first column of the selected row

I'm wondering what would be the most efficient way to get the value of the first column in an DataGridView row.

Currently, I'm using this code:

List<string> SelectedRows = new List<string>();
foreach (DataGridViewRow r in dgv.SelectedRows)
{
    SelectedRows.Add(r.Cells[0].Value.ToString());
}

int index = Convert.ToInt32(SelectedRows[0]);

This works fine; however, is there a more efficient option to do this?

Upvotes: 0

Views: 18964

Answers (4)

Triarta
Triarta

Reputation: 61

To get the value of the first column of the selected row, in the dataGridView's contentClick we can put this code, and that value can be saved in id variable

  private void datagridview1_CellContentClick(object sender, DataGridViewCellEventArgs e)
            {

             var id = Convert.ToInt32(datagridview1.Rows[e.RowIndex].Cells[0].Value);
            }

Upvotes: 1

jacobz
jacobz

Reputation: 3291

I did some performance testing with the code given in the answers. The average time for each method over 1.000.000 times:

My original code:

List<string> SelectedRows = new List<string>();
foreach (DataGridViewRow r in dgv.SelectedRows)
{
    SelectedRows.Add(r.Cells[0].Value.ToString());
}

int index = Convert.ToInt32(SelectedRows[0]);

Time = 0,0159 ms

var results = dataGridView1.SelectedRows
                           .Cast<DataGridViewRow>()
                           .Select(x => Convert.ToString(x.Cells[0].Value));

Time = 0,0152 ms

var index = Convert.ToInt32(dataGridView1.CurrentRow.Cells[0].Value);

When single-select only: 0,0013438 ms

List<string> SelectedRows = new List<string>();
for (int count = 0; count < dgv.SelectedRows.Count; count++)
{
    SelectedRows.Add(dgv.SelectedRows[count].Cells[0].Value.ToString());
}
int index = Convert.ToInt32(SelectedRows[0]);

Time = 0,0057 ms

Upvotes: 3

Syed Farjad Zia Zaidi
Syed Farjad Zia Zaidi

Reputation: 3360

Copying this from Patrick Smacchia's Blog:

The fact that for is more efficient than foreach results from the fact that foreach is using an enumerator object behind the scene.

I prefer using for loop instead of foreach loop, for loop is preferably faster than foreach loop when you do not have to do something to each element, and can solve your problem by just using the index as follows:

List<string> SelectedRows = new List<string>();
for (int count = 0; count < dgv.SelectedRows.Count; count++)
{
    SelectedRows.Add(dgv.SelectedRows[count].Cells[0].Value.ToString());
}
int index = Convert.ToInt32(SelectedRows[0]);

Upvotes: 2

Grant Winney
Grant Winney

Reputation: 66449

"Most efficient" is often subjective. I don't know that the following code is faster or shorter than what you've already got, but it's the method I prefer to use.

If you're trying to get a list of all values from a particular column, try this:

var results = dataGridView1.SelectedRows
                           .Cast<DataGridViewRow>()
                           .Select(x => Convert.ToString(x.Cells[0].Value));

If you only allow one selected row at a time, and you want to convert a particular cell, try this:

var index = Convert.ToInt32(dataGridView1.CurrentRow.Cells[0].Value);

Upvotes: 7

Related Questions