Home > DeveloperSection > Forums > How to get the First Column Values of Selected Rows of DataGridView in C#?
Ankita Pandey
Ankita Pandey

Total Post:183

Points:1285
Posted on    November-14-2013 12:51 AM

 C# C# 
Ratings:


 1 Reply(s)
 5689  View(s)
Rate this:

I am using the below code to get the ID of columns :

DataGridViewSelectedCellCollection DGV = this.dgvSearch.SelectedCells;

for (int i = 0; i <= DGV.Count - 1; i++)

{

string ID = Convert.ToString(dgvSearch.CurrentRow.Cells[0].Value);

MessageBox.Show(ID);

}

I am getting the ID but I want it once foe each column.

How to do it?



Pravesh Singh

Total Post:411

Points:2881
Posted on    November-14-2013 1:25 AM

Hi Ankita,

Use DataGridViewSelectedRowCollection instead of DataGridViewSelectedCellCollection and loop through the no of rows selected. inside the loop just give the same what u have given. replace this.dgvSearch.SelectedCells with this.dgvSearch.SelectedRows..

update:

 DataGridViewSelectedRowCollection DGV =this.dgvSearch.SelectedRows;

  foreach (DataGridViewRow row in DGV)

  {

 DataRow myRow = (row.DataBoundItem as DataRowView).Row;

  string ID = Convert.ToString(myRow.Cells[0].Value);

  MessageBox.Show(ID);

  }


Don't want to miss updates? Please click the below button!

Follow MindStick