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

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

问题描述

而不是使用设计器,我试图填充一个DataGridView,我已经把我的Winform程序化了。当我在调试器下面的表中看到它有正确的列和行数。问题是网格在我的表单上显示为一个空的灰色框。当我通过VS 2008 Designer将网格绑定到数据库时,它工作正常。任何想法如何追踪问题?谢谢。



更新



我几乎从这个 MSDN文章



更新



除了在Winform上放置网格之外,我必须在设计器中执行任何操作吗?

 使用系统; 
使用System.Data;
使用System.Data.SqlClient;
使用System.Data.SQLite;
使用System.Windows.Forms;

命名空间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 =汽车;
}

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;

//调整DataGridView列的大小以适应新加载的内容。
dataGridView1.AutoResizeColumns(
DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
}
catch(SqlException)
{
MessageBox.Show(要运行此示例,请使用连接字符串替换+
connectionString变量的值这是+
对您的系统有效。);
}
}


}
}


解决方案

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



我附加了一个有助于将gridview控件绑定到SQL Server的代码数据库,它对我来说工作正常。

  using(SqlDataAdapter sqlDataAdapter = 
new SqlDataAdapter(SELECT * FROM Table1,
使用(DataTable dataTable = new DataTable())
{
sqlDataAdapter.Fill(); $ {
$数据表);
this.dataGridView1.DataSource = dataTable;
}
}

对不起我没有安装SQLite: / p>

Rather than use the designer I'm trying to populate a DataGridView I've put on my Winform programatically. 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. Any idea how I can track down the problem? Thanks.

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


    }
}

解决方案

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 :(

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

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