Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading data from DataGridView in C#

Tags:

How can I read data from DataGridView in C#? I want to read the data appear in Table. How do I navigate through lines?

like image 860
sharon Avatar asked Jun 27 '11 01:06

sharon


People also ask

How show data from DataGridView from database in C#?

Step 1: Make a database with a table in SQL Server. Step 2: Create a Windows Application and add DataGridView on the Form. Now add a DataGridView control to the form by selecting it from Toolbox and set properties according to your needs.

How do you use data grid view?

The DataGridView control provides a powerful and flexible way to display data in a tabular format. You can use the DataGridView control to show read-only views of a small amount of data, or you can scale it to show editable views of very large sets of data.


2 Answers

something like

for (int rows = 0; rows < dataGrid.Rows.Count; rows++) {      for (int col= 0; col < dataGrid.Rows[rows].Cells.Count; col++)     {         string value = dataGrid.Rows[rows].Cells[col].Value.ToString();      } }  

example without using index

foreach (DataGridViewRow row in dataGrid.Rows) {      foreach (DataGridViewCell cell in row.Cells)     {         string value = cell.Value.ToString();      } } 
like image 169
CliffC Avatar answered Sep 30 '22 14:09

CliffC


If you wish, you can also use the column names instead of column numbers.

For example, if you want to read data from DataGridView on the 4. row and the "Name" column. It provides me a better understanding for which variable I am dealing with.

dataGridView.Rows[4].Cells["Name"].Value.ToString(); 

Hope it helps.

like image 30
macrobook Avatar answered Sep 30 '22 14:09

macrobook