How can I read data from DataGridView
in C#? I want to read the data appear in Table. How do I navigate through lines?
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.
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.
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(); } }
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With