Link to home
Start Free TrialLog in
Avatar of Mike Eghtebas
Mike EghtebasFlag for United States of America

asked on

Populate a gridview control using data adapt...

I have been able to use data reader to populate gwCategory (a grid view control) using a data reader variable. But I need some help to do the same using a data adapter.

Question: How can I make the attached code work?

This is in vs 2010 for winForm.

Thank you.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;

namespace ado01_Connection
{
    public partial class Form10 : Form
    {
        public Form10()
        {
            InitializeComponent();
        }

        private void Form10_Load(object sender, EventArgs e)
        {
            string cs = ConfigurationManager.ConnectionStrings["csDatabase"].ConnectionString;
            SqlConnection con = new SqlConnection(cs);

            using(con)
            {
                //SqlCommand cmd = new SqlCommand("SELECT [CategoryName],[Description],[Picture] FROM [Northwind].[dbo].[Categories];", con);
                SqlDataAdapter da = new SqlDataAdapter("SELECT [CategoryName],[Description],[Picture] FROM [Northwind].[dbo].[Categories];", con);
                DataSet ds = new DataSet();
                da.Fill(ds);

                //BindingSource source = new BindingSource();
                //source.DataSource = ds;
                gwCategory.DataSource = da;// source;
            }
        }
    }
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Robert Schutt
Robert Schutt
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mike Eghtebas

ASKER

That is fantastic. I will try shortly but what if I have more than one data adapter?

How can I make reference to an specific data adapter? Perhaps by name?

gwCategory.DataSource = ds.Tables(rdr1);

Mike