HOW TO GET THE FIRST COLUMN VALUES OF SELECTED ROWS OF DATAGRIDVIEW IN C#?

Ankita Pandey

Total Post:183

Points:1285
Posted by  Ankita Pandey
C# 
C#
 7223  View(s)
Ratings:
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?

  1. Pravesh Singh

    Post:412

    Points:2888
    Re: How to get the First Column Values of Selected Rows of DataGridView in C#?

    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);

      }

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!