Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#: Can't populate DataGridView programmatically

Rather than use the designer I'm trying to populate a DataGridView I've put on my Winform programmatically. When I look in the table under the debugger it has the correct columns and number of rows. The problem is the grid appears as an empty grey box on my form. When I bind the grid to the database via VS 2008 Designer it worked fine. How can I track down the problem?

UPDATE

I pretty much took this from this MSDN Article

UPDATE

Do I have to do anything in the designer other than drop the grid on the Winform?

using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SQLite;
using System.Windows.Forms;

namespace CC
{
    public partial class Form1 : Form
    {

        private BindingSource bindingSource1 = new BindingSource();
        private SQLiteDataAdapter dataAdapter = new SQLiteDataAdapter();

        public Form1()
        {
            InitializeComponent();
            dataGridView1 = new DataGridView();

            this.Load += new System.EventHandler(Form1_Load);
            this.Text = "Cars";
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            dataGridView1.DataSource = bindingSource1;
            GetData("select * from Cars");


        }

        private void GetData(string selectCommand)
        {
            string dbPath = "c:\\temp\\cars.db";

            try
            {

                var connectionString = "Data Source=" + dbPath + ";Version=3";

                dataAdapter = new SQLiteDataAdapter(selectCommand, connectionString);

                SQLiteCommandBuilder commandBuilder = new SQLiteCommandBuilder(dataAdapter);


                DataTable table = new DataTable();
                table.Locale = System.Globalization.CultureInfo.InvariantCulture;
                dataAdapter.Fill(table);
                bindingSource1.DataSource = table;

                // Resize the DataGridView columns to fit the newly loaded content.
                dataGridView1.AutoResizeColumns(
                    DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
            }
            catch (SqlException)
            {
                MessageBox.Show("To run this example, replace the value of the " +
                    "connectionString variable with a connection string that is " +
                    "valid for your system.");
            }
        }


    }
}
like image 540
halfpint Avatar asked Oct 31 '25 08:10

halfpint


1 Answers

I think you need to specify the DataMember property. And I think you don't require binding source object, directly you can bind DataTable to DataGridView control.

I am attaching a code which helps to bind gridview control with SQL Server database, and it works fine for me.

using(SqlDataAdapter sqlDataAdapter = 
    new SqlDataAdapter("SELECT * FROM Table1",
        "Server=.\\SQLEXPRESS; Integrated Security=SSPI; Database=SampleDb"))
{
    using (DataTable dataTable = new DataTable())
    {
        sqlDataAdapter.Fill(dataTable);
        this.dataGridView1.DataSource = dataTable;
    }
}

Sorry I don't have SQLite installed :(

like image 180
Anuraj Avatar answered Nov 02 '25 23:11

Anuraj



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!