简体   繁体   中英

Getting all cell data from a specific column in a Datagridview

Let's say the datagridview has 3 columns. The Datagridview is filled with data form a SQL database. Now i want the data of the column 'age' and write it to a list so i can use the data late but i only want the INTs. Can some please help me? a code example would be lovely.

|---------|----------|----------|
|    id   |   name   |   age    |
|---------|----------|----------|
|    1    |   john   |   20     |
|---------|----------|----------| 
|    2    |   jane   |   21     |
|---------|----------|----------|
|    3    |   jack   |   22     |
|---------|----------|----------|  

That is pretty simple.

For Each row As DataRow In YourDataGridView.Rows
  Dim age = CInt(row("age"))
Next  

Then you can do whatever you want with age variable inside that loop.
Or you can use LINQ to get list of ages.

Dim allAges = (From row As DataGridViewRow
               In YourDataGridView.Rows
               Select DirectCast(row.Cells("age").Value, Integer)).ToList()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM