473,320 Members | 1,840 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

how to fill the datagrid with a table

I have a sql server table and i want to read it in the datagrid view of C# .Net.
I think it is easy to do it. but i m not able to show any output in the datagrid...
here is my code.please guide me.where i m wrong..

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Data.SqlClient;
  10.  
  11. namespace grid
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.         public Form1()
  16.         {
  17.             InitializeComponent();
  18.         }
  19.  
  20.         private void Form1_Load(object sender, EventArgs e)
  21.         {
  22.             DataGrid dataGrid1;
  23.             dataGrid1 = new DataGrid();
  24.  
  25.             string strConn, strSQL;
  26.             strConn = "server=ABC,Database=test;user id = sa;password=manager;Trusted_Connection=Yes";
  27.             strSQL = "select * from info";
  28.  
  29.             SqlDataAdapter da = new SqlDataAdapter(strSQL, strConn);
  30.             DataSet ds = new DataSet();
  31.             da.Fill(ds, "information");
  32.  
  33.             dataGrid1.DataMember = "info";
  34.             dataGrid1.DataSource = ds;
  35.  
  36.         }      
  37.  
  38.     }
  39. }
Nov 10 '08 #1
3 1156
kenobewan
4,871 Expert 4TB
Try datagrid1.databind(); and report back.
Nov 10 '08 #2
Hi!

I think, using the last code, dataGrid1.DataSource = ds, you are refencing the whole dataset and not the specific table of the dataset which you named "information"...maybe thats the reason why your datagridview doesnt know which particular records to show....

you may try: dataGrid1.DataSource = ds.Tables("information");

NOTE: information is the name of table you created in dataset during fill method execution of dataadapter [da.Fill(ds, "information");]


hope this helps...cheers (",)
Nov 12 '08 #3
HI!!!

use

datagrid1.datasource=ds.tables["information"];
datagrid1.databind();
Nov 28 '08 #4

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

Similar topics

4
by: Mervin Williams | last post by:
I have several tables involved in my application, but the two in question here are the company and address tables. The company table has business_address_id and mailing_address_id columns, which...
6
by: JeffB | last post by:
I have tried several different methods of getting a datagrid to fill with information. Below is the code I'm now using. When viewed in the browser and the text box filled with a parameter value...
4
by: Dave Edwards | last post by:
I understand that I can fill a datagrid with multiple queries, but I cannot figure out how to fill a dataset with the same query but run against multiple SQL servers, the query , table structure...
0
by: Marcin Podle¶ny | last post by:
Hello! I use DataGrid which displays data in several columns. Number of columns depends on user prefferences (I mean: this is still the same query filling datatable but I use...
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) ...
10
by: Rich | last post by:
I have a stored procedure on Sql Server2k. I can fill a data table which I can append to a dataset using an ADODB recordset object which gets populated from a command object that runs the sp. I...
2
by: MDB | last post by:
Hello All, I have a data grid that I fill using a dataset. The results of the query has around 15 columns and 500 rows (and growing). The reason I am using the datagrid is so the end users can...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...

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.