Click here to Skip to main content
13,091,300 members (52,693 online)
Rate this:
Please Sign up or sign in to vote.
See more:

I want to store row for row which a datareader reads from my sql database into a gridview.

basically :

While reader.Read()

//Here i want to get the entire row that it reads eg. theName,Description,number
//and store that as is so that it can be used to populate my gridview.

end while

The gridview must look exactly like a SQL table would when you run a query like :
SELECT * FROM tblClients

any help?
Posted 22-Aug-12 22:59pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1


A simple binding can't be an option???

public static DataTable GetData()
    DataTable table = new DataTable();
    string query = @"SELECT * FROM tblClients";
    using (SqlConnection cn = new SqlConnection(Configuration.DefaultConnectionString))
        using (SqlCommand cmd = new SqlCommand(query, cn))
            using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
    return table;
MyGrid.DataSource = GetData();


Public Shared Function GetData() As DataTable
	Dim table As New DataTable()
	Using cn As New SqlConnection("your connection string")
		Using cmd As New SqlCommand("SELECT * FROM tblClients", cn)
			Using reader As SqlDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
			End Using
		End Using
	End Using
	Return table
End Function

Member 9374423 23-Aug-12 5:12am
Hi I do appreciate the help. but could you please rewrite this in code? it seems to have trouble descripting in VB.
Mario Majčica 23-Aug-12 5:16am
I just updated the solution. Converting code is easy or .

Member 9374423 23-Aug-12 5:20am
It works Perfectly Thank you very much :}
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

The FieldCount property of the DataReader tells you the number of columns.
The GetFieldType(columnIndex) function gives infromation on the type of the data.
And then do the nested loops thru your data, and fill the gridview.
Member 9374423 23-Aug-12 5:14am
could you please supply an example?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.170813.1 | Last Updated 23 Aug 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100