[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Populate a gridview control using data adapt...

Posted on 2013-11-02
3
Medium Priority
?
523 Views
Last Modified: 2013-11-02
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

0
Comment
Question by:Mike Eghtebas
[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
3 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 1400 total points
ID: 39619187
Can you try:
gwCategory.DataSource = ds.Tables[0];

Open in new window

0
 
LVL 64

Assisted Solution

by:Fernando Soto
Fernando Soto earned 600 total points
ID: 39619230
Hi eghtebas;

The code snippet should load the control for you.

BindingSource source = new BindingSource();

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

    using (var con = new SqlConnection(cs))
    {
        SqlDataAdapter da = new SqlDataAdapter("SELECT [CategoryName],[Description],[Picture] FROM [Categories];", con);
        DataSet ds = new DataSet();
        da.Fill(ds);
        source.DataSource = ds.Tables[0];
        gwCategory.DataSource = source;
    }
}

Open in new window

0
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 39619238
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
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

650 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