Solved

Populate a gridview control using data adapt...

Posted on 2013-11-02
3
516 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 350 total points
ID: 39619187
Can you try:
gwCategory.DataSource = ds.Tables[0];

Open in new window

0
 
LVL 63

Assisted Solution

by:Fernando Soto
Fernando Soto earned 150 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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

696 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