Tutorials
ODP.NET
ODP.NET - Retrieving Multiple Rows on to the Grid
Pulling Information Using Table NameIn the previous section, we tried to retrieve only one row using OracleDataReader. In this section, we will try to retrieve more than one row (or a result set) and populate a DataGridView on a WinForm.
The following code lists out the details of all employees available in the emp table:
Except the highlighted section, the rest of the code is already explained as part of the previous section. You can observe that the SELECT statement now tries to retrieve all rows from emp as follows:
SQL = String.Format("SELECT empno, ename, job, mgr,
hiredate, sal, comm, deptno FROM emp")
Once the OracleDataReader is ready with rows, we need to start with clearing the rows already displayed in the DataGridView with the help of the following code:
With Me.DataGridView1
'remove existing rows from grid
.Rows.Clear()
Once the rows are cleared, the first issue is the header of the grid. The moment we add columns to the grid, the header row gets automatically populated (with the column names). Before adding columns to the header, we should know the number of columns being added (just for the loop iterations) with the FieldCount property of DataGridView. The following is the code fragment that finds the number of columns and adds the columns to DataGridView:
Dim ColumnCount As Integer = rdr.FieldCount
For i As Integer = 0 To ColumnCount - 1
.Columns.Add(rdr.GetName(i), rdr.GetName(i))
Next
All the columns get auto-sized based on the column header with the following statement:
.AutoSizeColumnsMode =
DataGridViewAutoSizeColumnsMode.ColumnHeader
Once the columns are added, we need to read every successive row from the OracleDataReader and add it to the DataGridview. To add all column values at a time, we make use of the GetValues() method of OracleDataReader to push all the values in to an array and finally add the array itself as a row to the DataGridView. The following code fragment accomplishes this.
The output for this code would look similar to the following figure:

Next Page: Pulling Information Using Table Name