C#:无法以编程方式填充DataGridView [英] C#: Can't populate DataGridView programmatically

查看:49
本文介绍了C#:无法以编程方式填充DataGridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有使用设计器,而是试图填充以编程方式放置在Winform中的DataGridView。当我在调试器下的表中查找时,它具有正确的列和行数。问题是网格在我的表单上显示为一个空的灰色框。当我通过VS 2008 Designer将网格绑定到数据库时,它运行良好。我如何找到问题所在?

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?

更新

我花了很多时间来自此 MSDN文章

I pretty much took this from this MSDN Article

更新

除了在Winform上放置网格之外,我是否还需要在设计器中做任何事情?

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.");
            }
        }


    }
}


推荐答案

我认为您需要指定DataMember属性。而且我认为您不需要绑定源对象,可以直接将DataTable绑定到DataGridView控件。

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.

我附上了有助于将gridview控件与SQL Server绑定的代码。数据库,对我来说很好。

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;
    }
}

对不起,我没有安装SQLite:(

Sorry I don't have SQLite installed :(

这篇关于C#:无法以编程方式填充DataGridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆