Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / December 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
bill - 26 Dec 2006 19:37 GMT
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
Peter Ritchie [C# MVP] - 27 Dec 2006 02:50 GMT
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.

Signature

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

> All,
>
[quoted text clipped - 4 lines]
>
> Bill
bill - 27 Dec 2006 12:41 GMT
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
[quoted text clipped - 20 lines]
> >
> > Bill
ClayB - 27 Dec 2006 14:32 GMT
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();
}
krishnen - 27 Dec 2006 14:35 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.