473,473 Members | 2,016 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to fill a datagrid with data in c#.net?

1 New Member
I've tried everything and nothing works.

I'm connecting to a Microsoft Access database using the following code:
Expand|Select|Wrap|Line Numbers
  1. OleDbConnection conn = new OleDbConnection(
  2. @"Provider=Microsoft.Jet.OLEDB.4.0; " + 
  3. @"Data Source=C:\Bob\Status\Timekeeping Status Sheets\Timekeeping.mdb");
  4.  
  5. conn.Open();
  6. System.Data.OleDb.OleDbDataAdapter da = new System.Data.OleDb.OleDbDataAdapter(
  7. "select charge, description from timesheet", conn);
  8.  
  9. DataTable t = new DataTable();
  10. dataGridView1.DataSource = t;
When I do this, absolutely nothing happens. I get a gray grid with no rows and no columns and NOTHING.

I've tried creating a binding source and setting its DataSource to "t", and setting the dataGridView's data source to the binding source, to no avail. Still nothing happens.

I've verified that the data table has data in it by manually populating a ListBox with the data read from the database. That part works. Getting the data in the datagrid does not work. Any clues, help or pointers would be appreciated. Thanks.
Feb 16 '11 #1
4 5604
Per Eriksson
4 New Member
I'm doing the same thing right now so I might have a tip for you.
You should probably try to use a DataSet as an "intermediate" storage facility of your table data.
- Insert a new DataSet (xsd) into your project.
- You will then get a "page" where it says that you can either drag in objects from the Server Explorer or add them by hand.
- This means that you should add a table onto the page that has the same format as the one in your DB.
-Do it by hand from the toolBox or by connecting to the Db via server explorer and dragging the table into it.(The latter option creates quite a lot of system generated code but could be easier since in this code is included all the SQL-queries you might want to use.
- It is then a matter of adding a dataset into your form that contains the datagrid and let it "use" the datasetfile you've just created.
-Then binding your binding source to the dataset and the datgridview to the binding source.
- To populate the grid is then done by calling the method Fill() from the (or a) tableadapter that is associated with the DataSet table.
This reply became very long so I'm stopping here.
But I hope this can guide you in the right direction to make it work.
If you want more info make another reply and I'll try to follow up with further descriptions.
/Per
Feb 24 '11 #2
bish1
7 New Member
Include the following namespace in your code window
Expand|Select|Wrap|Line Numbers
  1. using System.Data.SqlClient;
Write the following code in formload event or button click event
Expand|Select|Wrap|Line Numbers
  1. SqlConnection con=new SqlConnection();
  2. con.connectionString="Data Source=SqlserverName;initial catalog=databasename;user id=sqlusername;password=sqlpassword;integrated security=false";
  3. con.open();
  4. try
  5. {
  6.   SqlCommand cmd=new SqlCommand();
  7.   cmd.connection=con;
  8.   cmd.commandText="Select field1,field2,...field n from tablename";
  9. SqlDataAdapter adp=new SqlDataAdapter();
  10. adp.selectCommand=cmd;
  11. DataSet Ds=new DataSet();
  12. adp.fill(ds,"tempTablename");
  13. adp.dispose();
  14. if(ds.tables["tempTablename"].rows.count>0)
  15. {
  16.   GridView1.Datasource=ds.tables["tempTablename"];
  17. }
  18. else
  19. {
  20. GridView1.Datasource=ds.tables["tempTablename"];
  21. }
  22. }
  23. catch(systemException ex)
  24. {
  25.   Messagebox.show(ex.message);
  26. }
Aug 9 '12 #3
Frinavale
9,735 Recognized Expert Moderator Expert
I am failing to see where you are calling one of the OleDbDataAdapter's Fill methods in the code you posted.

If you do not call the fill method to fill a DataSet (or table), how do you expect to get the data?

The fill method retrieves data from the database and populates an in memory DataTable.

The Fill(DataTable) method would work well in your code since you're not using a DataSet.

-Frinny
Aug 9 '12 #4
bish1
7 New Member
Expand|Select|Wrap|Line Numbers
  1. //object of dataadapter
  2. SqlDataAdapter adp=new SqlDataAdapter(); 
  3.  
  4. // assign sqlquery to extract from database. refer previous //code for selecting data from db.
  5. adp.selectCommand=cmd;
  6.  
  7. //object of dataset
  8. DataSet Ds=new DataSet();
  9.  
  10. //filling dataset with records that exist in adapter
  11. adp.fill(ds,"tempTablename");
  12.  
  13. //realeasing data adapter
  14. adp.dispose();
  15.  
  16. //check whether inline table contain data or not
  17. if(ds.tables["tempTablename"].rows.count>0)
  18. {
  19.  
  20. // Fills data grid with records
  21.   GridView1.Datasource=ds.tables["tempTablename"];
  22. }
Aug 19 '12 #5

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

Similar topics

0
by: Jassim Rahma | last post by:
Hi, I wanted to know why should I clear the dataset and fill it again if I want to refresh the dataGrid control.. I am only adding a new record which I want it to appear immediatelly...
2
by: Josef Meile | last post by:
Hi, I'm using a ComboBox, some Textboxes, and a DataGrid to represent a many-to-many relationship between Person and Course. Each time that I change the value in the ComboBox (which for now is...
7
by: ruca | last post by:
Hi, How can I fill a datagrid without accessing to a database or any type of file, only filling with a command button like this: I have 3 textboxes that I must fill, and a button that when...
4
by: jaYPee | last post by:
I have already done some code to fill the datagrid. my problem is that the fill method is too slow after executing my code. here is the scenario. i have a parent/child form. all is datagrid....
4
by: jaYPee | last post by:
I have 1 dataset called "dataset1" that contains 2 tables called "course" and "courseload". in my form i have a datagrid. the datasource of this datagrid is "dataset1" and the datamember is...
2
by: Brett | last post by:
I have the following code in VS Studio .NET 2005 beta: Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Me.DataAdapter1.Fill(DataSet1) ...
1
by: johnlim20088 | last post by:
Hi All, I does have a question about export datagrid data to csv. Well, I know the common solution is 1) select the data again and put in the datatable, then export to csv. BUT I don't want...
3
by: vbdude | last post by:
Hi, I want help with my problem. How can i export datagrid data that comes from a ms sql server 2005 and will be going to ms excel.. Vbdude... P.S. Please Help me!!!
6
by: slinky | last post by:
I found the following code to transfer datagrid data to an Excel file. Is this written in C#?... I'm a vb.netter. I'm just not sure where to place the code to experiment on it. Should I place it in...
0
by: mahendra dubey | last post by:
Hi There I am using Datagrid control provided by wpftoolkit in wpf.I have set ItemsSource property of datagrid.Now Problem is that rows of datagrid takes only the space needed to display,...
0
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
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
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.