PC Review


Reply
Thread Tools Rate Thread

How to bind a DataGridView to a DataSet without a database?

 
 
bill
Guest
Posts: n/a
 
      26th Dec 2006
All,

Where can I find some sample code for binding a DataGridView to a
dataset without having the dataset attached to a database?

TIA,

Bill

 
Reply With Quote
 
 
 
 
=?Utf-8?B?UGV0ZXIgUml0Y2hpZSBbQyMgTVZQXQ==?=
Guest
Posts: n/a
 
      27th Dec 2006
A DataSet is just a representation of an in-memory data cache. The data
could come from anywhere. DataSet supports XML files directly and usually
DataAdapters are uses to fill the DataSet via a database connection. You can
manually create a DataSet by adding DataTable objects to the
DataTableCollection property "Tables". For an example of creating a DataSet
without using an XML file or populating via a database connection see
http://msdn2.microsoft.com/en-us/library/aeskbwf7.aspx

Binding that DataSet to the DataGridView would be the same as if the DataSet
was populate via a database connection and a DataAdapter.

--
Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.
http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#


"bill" wrote:

> All,
>
> Where can I find some sample code for binding a DataGridView to a
> dataset without having the dataset attached to a database?
>
> TIA,
>
> Bill
>
>

 
Reply With Quote
 
bill
Guest
Posts: n/a
 
      27th Dec 2006
Peter,

The following code results in nothing appearing in the DataGridView:

BindingSource bs = new BindingSource();
private void initDataGridView(DataGridView dgv)
{
// dsForDGV is a datset object on the form

dsForDGV.Tables[0].Rows.Add("abc");
dsForDGV.Tables[0].Rows[0][0] = 1234;
bs.DataSource = dsForDGV;
bs.DataMember = dsForDGV.Tables[0].TableName;
dgv.DataSource = bs;
dgv.Refresh();
}

What am I doing wrong? When the form opens the DataGridView control has
the correct number of rows and columns, but they are all blank.

Bill


Peter wrote:
> A DataSet is just a representation of an in-memory data cache. The data
> could come from anywhere. DataSet supports XML files directly and usually
> DataAdapters are uses to fill the DataSet via a database connection. You can
> manually create a DataSet by adding DataTable objects to the
> DataTableCollection property "Tables". For an example of creating a DataSet
> without using an XML file or populating via a database connection see
> http://msdn2.microsoft.com/en-us/library/aeskbwf7.aspx
>
> Binding that DataSet to the DataGridView would be the same as if the DataSet
> was populate via a database connection and a DataAdapter.
>
> --
> Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.
> http://www.peterRitchie.com/blog/
> Microsoft MVP, Visual Developer - Visual C#
>
>
> "bill" wrote:
>
> > All,
> >
> > Where can I find some sample code for binding a DataGridView to a
> > dataset without having the dataset attached to a database?
> >
> > TIA,
> >
> > Bill
> >
> >


 
Reply With Quote
 
ClayB
Guest
Posts: n/a
 
      27th Dec 2006
Have you added a DataTable to your DataSet that contains a string
column? The code below worked OK for me in a simple sample.

Clay Burch
Syncfusion, Inc.

DataSet dsForDGV = new DataSet();
BindingSource bs = new BindingSource();
private void Form1_Load(object sender, EventArgs e)
{
DataTable dataTable1 = new DataTable("MyTable");
dataTable1.Columns.Add(new DataColumn("Col0"));
dsForDGV.Tables.Add(dataTable1);

initDataGridView(this.dataGridView1);
}

private void initDataGridView(DataGridView dgv)
{
dsForDGV.Tables[0].Rows.Add("abc");
dsForDGV.Tables[0].Rows[0][0] = 1234;
bs.DataSource = dsForDGV;
bs.DataMember = dsForDGV.Tables[0].TableName;
dgv.DataSource = bs;
dgv.Refresh();
}

 
Reply With Quote
 
krishnen
Guest
Posts: n/a
 
      27th Dec 2006
Bill,

Make sure the DataPropertyName is set correctly for the columns is set
correctly so that the mapping is made corerctly between the Data Source
and the DatagridView

Krishnen

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bind DataGridView to Dataset at design time Vlad Microsoft Dot NET 2 3rd Aug 2007 09:11 AM
bind datagridview instance to dataset instance ? swartzbill2000@yahoo.com Microsoft VB .NET 0 18th Apr 2006 06:46 PM
Can I bind DataGridView to xml? Stan Microsoft Dot NET Framework 1 1st Feb 2006 08:38 AM
DataGrid Bind to DataSet, then Bind to DaTaview, GOT ERROR...PLS HELP A_PK Microsoft Dot NET Compact Framework 16 13th Apr 2005 04:27 AM
DataGrid Bind to DataSet, then Bind to DaTaview, GOT ERROR...PLS HELP A_PK Microsoft VB .NET 17 13th Apr 2005 04:27 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:48 AM.