473,503 Members | 2,066 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

present sql query in data grid view c#

17 New Member
hi,

I'm using access db in my c# program I have a select query and I want to present
the result in data grid view ,I succeed doing that with this while loop

Code: ( c )
int i = 0,j=0;
while (dbReader.Read())
{
dataGridView2.Rows.Add();
dataGridView2[j, dataGridView2.Rows.Count - 1].Value = (string) [ dbReader.GetValue(0).ToString(); j++;
dataGridView2[j, dataGridView2.Rows.Count - 1].Value = (string)dbReader.GetValue(1); j++;
dataGridView2[j, dataGridView2.Rows.Count - 1].Value = (string)dbReader.GetValue(2);
i++;
j = 0;
}

is there an appropriate way doing it that sent the all result to the table at once?
thanks,
Mar 21 '08 #1
2 4131
balabaster
797 Recognized Expert Contributor
You haven't specified what DataGridView is - I'm assuming that it's a GridView object. In which case you can just set the DataSource property to your DataReader

Expand|Select|Wrap|Line Numbers
  1. Dim oCmd As New SqlCommand("Select....", oCon)
  2. Dim oRdr As SqlDataReader = oCmd.ExecuteReader()
  3. DataGridView2.DataSource = oRdr
  4. DataGridView2.DataBind
  5. oCmd.Dispose()
This should do the job for you without making you iterate through every row in your data reader.

[Edit: Oops, overlooked the fact that you said C#, sorry]
C#
Expand|Select|Wrap|Line Numbers
  1. SqlCommand oCmd = New SqlCommand("Select...", oCon);
  2. SqlDataReader oRdr = oCmd.ExecuteReader();
  3. DataGridView2.DataSource = oRdr;
  4. DataGridView2.Databind();
  5. oCmd.Dispose();
Mar 21 '08 #2
eranby
17 New Member
You haven't specified what DataGridView is - I'm assuming that it's a GridView object. In which case you can just set the DataSource property to your DataReader

Expand|Select|Wrap|Line Numbers
  1. Dim oCmd As New SqlCommand("Select....", oCon)
  2. Dim oRdr As SqlDataReader = oCmd.ExecuteReader()
  3. DataGridView2.DataSource = oRdr
  4. DataGridView2.DataBind
  5. oCmd.Dispose()
This should do the job for you without making you iterate through every row in your data reader.

[Edit: Oops, overlooked the fact that you said C#, sorry]
C#
Expand|Select|Wrap|Line Numbers
  1. SqlCommand oCmd = New SqlCommand("Select...", oCon);
  2. SqlDataReader oRdr = oCmd.ExecuteReader();
  3. DataGridView2.DataSource = oRdr;
  4. DataGridView2.Databind();
  5. oCmd.Dispose();
Hi,
Thank you very much ,it was very helpful and solve my the problem!!

Eran
Apr 15 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
4102
by: Mike N. | last post by:
Hello to all: First let me apologize for the length of this question, I've made an attempt to include as much information as is needed to help with the question. I am having problems putting...
3
579
by: John young | last post by:
I have been looking for an answer to a problem and have found this group and hope you can assist . I have been re doing a data base I have made for a car club I am with and have been trying to...
1
2072
by: Michael | last post by:
I have a query that uses two joined tables. The query contains data. I would like to pull in a data drom another table but when I add that table to the query design and make the join,the existing...
4
6302
by: Peter Bailey | last post by:
I have a vba string taht dynamically creates the query which has two dates in it that it grabs off an open form as a string from the textbox. What I generate in vba is: SELECT DOSMBK.Date,...
1
3136
by: James | last post by:
Access 2003, trying to count the number of records that meet a criteria. According to Help: "In the Database window, click Queries under Objects, and then click New on the database window...
1
2001
by: cindy | last post by:
Get data into datatable, add to dataset dsSearch " Get data into datatable, add to dataset dsSearch Using In-Memory SQL Engine join the tables and select the filenames from the join, add to...
4
7152
by: amy | last post by:
Hi to Everyone: I need big help on how to query the Age range. Age field is text data type, Age are from 0 wk, 1 wk, 2wk.....up to 15 wk. Try to set up query in Query desing mode with criteria is...
1
1637
by: eranby | last post by:
hi, I'm using access db in my c# program I have a select query and I want to present the result in data grid view ,I succeed doing that with this while loop int i = 0,j=0; while...
6
2929
by: insirawali | last post by:
Hi all, I have this problem, i need to know is there a way i cn use the data adapter's update method in this scenario. i have 3 tables as below create table table1{ id1 int identity(1,1)...
0
7205
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7093
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7287
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7348
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
5021
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4685
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3166
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1519
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.