Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - How to select the values of rows in DataGridView in c#?
How to select the values of rows in DataGridView in c#?
1, get (specify) the value of the cell:

dataGridView 1。 Row [i]. Cell [j]. Value;

2. Get the total number of selected banks:

dataGridView 1。 SelectedRows . Count

3. Get the index of the currently selected line:

dataGridView 1。 Current row index;

4. Get the value of the currently selected cell:

dataGridView 1。 Current cell value;

5. Take the data of the selected line.

string[]str = new string[dataGridView。 rows . Count];

for(inti; I & ltdataGridView 1. Number of rows. Count; i++)

{

if(dataGridView 1。 Line [i]. Selected==true)

{

str[i]=dataGridView 1。 Line [i]. cells[ 1]. value . tostring();

}

}

6. Get some data of the selected line.

inta=dataGridView 1。 SelectedRows . Index

DataGridView 1。 Ok [a]. Cell ["Write as many indexes of a column as you want"]. Value;

7. Get (specify) the value of the cell: datagridview 1. Row [i]. Cell [j]. Value; Line [i] should be line [i]

inta=dataGridView 1。 Current row index;

stringstr=dataGridView 1。 Row [a]. cells[" strName "]. value . tostring();

SelectedRows[0] currently selected row.

. Cell [column index]. Values is the value of a cell in the currently selected row.

DataGridView 1。 Selected cell (0). Value. Tostring gets the contents of the currently selected cell.

DataGridView 1。 Line (e.rowindex). Cell (2). Value. Tostring currently selects the content in column n of the cell.

Extended data

C#DataGridView selects multiple rows and deletes them.

if(this.dataGridView 1。 Number of rows. Count==0)

{

MessageBox。 Show ("No record of disembarking");

Return;

}

DialogResultdr=MessageBox。 Show ("cannot be restored after deletion. Are you sure you want to delete the selected computer user? , "tips", MessageBoxButtons. ok cancel);

if(dr==DialogResult。 Ok)

{

for(inti = 0; I & ltdataGridView 1. SelectedRows . Counti++)

{

if(dataGridView 1。 Select line [i]. Cells[0].Value.ToString()=="√")

{

this.dataGridView 1。 rows . remove at(I);

}

}

}

}