Solved

Trying to manually populate a DataGridView

Posted on 2008-10-05
1
440 Views
Last Modified: 2013-12-17
I am trying to call SetDataSource then Bind the DataSet to the control, but am missing a step.

Could someone tell me how?

Here is  my code so  far:

   public partial class Form1 : Form
    {
        private DataSet dataSet = null;
        private SqlDataAdapter sqlDataAdapter = null;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }

        private string ConnectString
        {
            get
            {
                string connectString = "Initial Catalog=ODBCsubsystem;Data Source=(local);Integrated Security=SSPI;";
;
                return connectString;
            }
        }

        private DataSet GetDataSet()
        {
            string sql = null;
            try
            {
                sql = "SELECT * FROM Alerts";

                if (sqlDataAdapter == null || dataSet == null)
                {
                    sqlDataAdapter = new SqlDataAdapter(sql, ConnectString);
                    dataSet = new DataSet();
                    sqlDataAdapter.Fill(dataSet, "Alerts");
                }
                else
                {
                    dataSet.Clear();
                    sqlDataAdapter.Fill(dataSet, "Alerts");
                }
            }
            catch (Exception ex)
            {
            }

            return dataSet;
        }

        private void fillBtn_Click(object sender, EventArgs e)
        {
            dataGridView2.DataSource = GetDataSet();
            dataGridView2.bind  <<<<<<<<<<<<<< how do I bind to the control?
        }
    }
0
Comment
Question by:newbieweb
[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
1 Comment
 
LVL 8

Accepted Solution

by:
rambovn earned 500 total points
ID: 22645359
see this
http://en.csharp-online.net/Working_with_Data%E2%80%94Using_the_DataGridView


try
private void fillBtn_Click(object sender, EventArgs e)
        {
            dataGridView2.DataSource = (GetDataSet()).Tables[0];
           
        }
0

Featured Post

How To Reduce Deployment Times With Pre-Baked AMIs

Even if we can't include all the files in the base image, we can sometimes include some of the larger files that we would otherwise have to download, and we can also sometimes remove the most time-consuming steps. This can help a lot with reducing deployment times.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
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…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

624 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