Solved

easy question binding dataset to a datagrid.

Posted on 2008-06-09
11
388 Views
Last Modified: 2013-12-17
I have the following:

private void Form1_Load(object sender, EventArgs e)
        {
           
            InitialPriceReportingWS.InitialPriceReportingService s = new InitialPriceReportingWS.InitialPriceReportingService();

            ds = s.GetPriceListHeader();
            this.dataGridView1.DataSource = ds.Tables[0];
           
========> how do I do my binding.
            this.MainTabControl.TabPages.RemoveAt(2); // Error/Warning invisible until Load is completed.
            this.MainTabControl.TabPages.RemoveAt(2); // Compare invisible until compare button is pressed.


            //System.Data.DataTable dt = new System.Data.DataTable();
            //dt.Columns.Add("LastName", Type.GetType("System.String"));
            //dt.Rows.Add(new object[] { "Smith" });
            //dt.Rows.Add(new object[] { "Jones" });
            //dt.Rows.Add(new object[] { "Clark" });
            //ds.Tables.Add(dt);
            //ds.AcceptChanges();
           
        }
0
Comment
Question by:mathieu_cupryk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 3
11 Comments
 
LVL 9

Expert Comment

by:Rob Siklos
ID: 21746569
try

this.dataGridView1.DataBind();
0
 

Author Comment

by:mathieu_cupryk
ID: 21746606
Error      7      'System.Windows.Forms.DataGridView' does not contain a definition for 'DataBind'      C:\SvnWork\InitialPriceReporting\DotNet\InitialPriceReporting\Form1.cs      104      32      InitialPriceReporting
0
 
LVL 9

Expert Comment

by:sevensnake77
ID: 21746684
try this maybe this will help
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
 
    public class Form1 : System.Windows.Forms.Form
    {
        private System.Windows.Forms.DataGrid dataGrid1;
        private System.ComponentModel.Container components = null;
 
        public Form1()
        {
            InitializeComponent();
        }
 
        private void InitializeComponent(){
         this.dataGrid1 = new System.Windows.Forms.DataGrid();
         ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
         this.SuspendLayout();
         this.dataGrid1.DataMember = "";
         this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
         this.dataGrid1.Location = new System.Drawing.Point(8, 8);
         this.dataGrid1.Name = "dataGrid1";
         this.dataGrid1.Size = new System.Drawing.Size(608, 256);
         this.dataGrid1.TabIndex = 0;
         this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
         this.ClientSize = new System.Drawing.Size(624, 272);
         this.Controls.Add(this.dataGrid1);
         this.Name = "Form1";
         this.Text = "Form1";
         this.Load += new System.EventHandler(this.Form1_Load);
         ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
         this.ResumeLayout(false);
 
      }
 
      static void Main() {
            Application.Run(new Form1());
      }
 
      private void Form1_Load(object sender, System.EventArgs e){
         string connString = "server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI";
 
         string sql = @"select * from employee";
 
         SqlConnection conn = new SqlConnection(connString);
         SqlDataAdapter da = new SqlDataAdapter(sql, conn);
         DataSet ds = new DataSet();
         da.Fill(ds, "customers");
 
         // Bind the data table to the data grid
         dataGrid1.SetDataBinding(ds, "customers");
      }
    }

Open in new window

0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 9

Expert Comment

by:sevensnake77
ID: 21746694
or here is another example I have within a button
private void DataSetBtn_Click(object sender, System.EventArgs e)
{
// Create a DataSet & add DataTable A and B to it
DataTable table = CreateDataTableA();
DataSet ds = new DataSet("ABSet");
ds.Tables.Add(table);
table = CreateDataTableB();
ds.Tables.Add(table);
// Bind DataSet to the DataGrid
dataGrid1.SetDataBinding(ds,"B");
}

Open in new window

0
 
LVL 9

Accepted Solution

by:
Rob Siklos earned 500 total points
ID: 21746701
Oh sorry - you don't need to do that.

Looking at the MSDN example ( http://msdn.microsoft.com/en-us/library/fbk67b6z(VS.80).aspx ) it seems that you don't need to do anything other than set the data source.  Maybe your data source is empty?
0
 

Author Comment

by:mathieu_cupryk
ID: 21746752
remember i am using datagridview.
0
 
LVL 9

Expert Comment

by:sevensnake77
ID: 21746951
doesn't matter the same methods should apply
0
 
LVL 9

Expert Comment

by:Rob Siklos
ID: 21746974
sevensnake77:

no - I thought the same thing.  But if you look at the MSDN docs for DataGridView, it doesn't have the SetDataBinding method.


author:

can you try ResetBindings()?
0
 

Author Comment

by:mathieu_cupryk
ID: 21747136
i have this right now?

 private void Form1_Load(object sender, EventArgs e)
        {
           
            InitialPriceReportingWS.InitialPriceReportingService WS = new InitialPriceReportingWS.InitialPriceReportingService();

            ds = WS.GetPriceListHeader();
            this.dataGridView1.DataSource = ds.Tables[0];

     
            dataGridView1.DataSource = PriceListHdrBindingSource;

not rows are there but the values are not.

it is empty.
I don't have an adapter because the query is called in a webservice and information is brought back in a dataset.
0
 

Author Comment

by:mathieu_cupryk
ID: 21747143
there is no resetbindings?
0
 

Author Comment

by:mathieu_cupryk
ID: 21747292
rob, the dataset is full then when I see it on the grid I see 7 rows of 24 columns epty?
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question