Solved

Trying to manually populate a DataGridView

Posted on 2008-10-05
1
434 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
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 our DevOps Teams Maximize Uptime

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

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

829 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