DataGridView不显示数据:DataSet为空 [英] DataGridView doesn't show data: DataSet is empty

查看:257
本文介绍了DataGridView不显示数据:DataSet为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个项目,我已经使用了L2S。所以它工作正常。



所以在我使用相同的连接字符串和创建新的WinForm项目之后。我放置一个datagrid,选择这个连接字符串。我选择DataMember并显示行标题。但是当我开始一个应用程序,我正在看本地人,我看到DataSet的所有表都是空的。我建议他们将从DB自动填充,但是它们没有。所以我该怎么做? DB不为空。



我已经google了,真的。



所以它真的不会自动填充。我有一张表好处。所以我写了

  private void Form1_Load(object sender,EventArgs e)
{
RadikDataSet。 BenefitsDataTable benefitsDataTable = new BenefitsTableAdapter()。GetData();
dataGridView1.DataSource = benefitsDataTable;
}

它的工作原理。所以tnx。这是非常愚蠢的,我应该手动调用它。

解决方案

您是否使用BindingSource?确保你在你的表单加载事件中这样分配:

  private void Form1_Load(object sender,System.EventArgs e)
{
//将DataGridView绑定到BindingSource
//并从数据库加载数据。
dataGridView1.DataSource = bindingSource1;
GetData(select * from Alex_db);
}

更详细的示例这里可用。


I've created one project and i've used L2S. So it was working fine.

So after i'm using same connection string and creating new WinForm project. I'm placing a datagrid, selecting this connection string. I'm selecting DataMember and rows headers appears. But when i'm starting an app, i'm watching locals and i see that DataSet has all tables empty. I suggested that they will auto-populate from DB, but they doesn't. So what should I do? DB is not empty.

P.S. I've googled, really.

So it really doesn't auto-populate. I have a table "Benefits". So i've writed

private void Form1_Load(object sender, EventArgs e)
{
    RadikDataSet.BenefitsDataTable benefitsDataTable = new BenefitsTableAdapter().GetData();
    dataGridView1.DataSource = benefitsDataTable;
}

and it works. So tnx. It's so stupid that I should manualy call it.

解决方案

Are you using a BindingSource? Make sure your assigning it in your form load event like so:

    private void Form1_Load(object sender, System.EventArgs e)
{
    // Bind the DataGridView to the BindingSource 
    // and load the data from the database.
    dataGridView1.DataSource = bindingSource1;
    GetData("select * from Alex_db");
}

More detailed example available here.

这篇关于DataGridView不显示数据:DataSet为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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