Solved

Populate a gridview control using data adapt...

Posted on 2013-11-02
3
513 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
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 62

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 33

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

777 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