使用 DataView 填充 DataGridView

发布于 2024-11-30 23:01:54 字数 209 浏览 2 评论 0原文

我想用一个我们可以假设已经包含数据的 DataView 填充我的 DataGridView。我一直在寻找一种方法来做到这一点,但所有解决方案都涉及 DataView 之外的其他数据结构,或者涉及使用我无法合并到该项目中的其他库。我是否需要首先将 DataView 转换为其他内容并使用它来填充 DataGridView?或者除了 DataGridView 之外我还可以使用其他东西来以类似的方式显示信息?

I would like to fill in my DataGridView with a DataView that we can assume already contains data. I have been searching for a way to do this but all the solutions involve other data structures than a DataView or involve using other libraries that I am unable to encorporate in this project. Do I need to first convert the DataView to something else and use that to populate the DataGridView? Or is there something else I can use than a DataGridView that displays information in a similar way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜无邪 2024-12-07 23:01:55

尝试将 DataGridViewDataSource 属性设置为您的 DataView
DataSource 可以实现 IBindingListViewIList 接口(以及与本例无关的其他选项。),这两个接口均由 代码>数据视图。

有关详细信息,请查看 MSDN:

Try setting the DataSource property of DataGridView to your DataView.
A DataSource can implement either the IBindingListView or IList interfaces (with other options that are irelevant to this case.) that are both implemented by the DataView.

For more information check the MSDN:

柒七 2024-12-07 23:01:55

如何:将 DataView 对象绑定到 Windows 窗体 DataGridView 控件

这是所有本机,我只是用谷歌搜索“datagridview.datasource to dataview”也许我读错了,这并不能解决你的问题,但如果是的话请发表评论,我会尽力提供帮助

private void GetData()
{
    try
    {
        // Initialize the DataSet.
        dataSet = new DataSet();
        dataSet.Locale = CultureInfo.InvariantCulture;

        // Create the connection string for the AdventureWorks sample database.
        string connectionString = "Data Source=localhost;Initial Catalog=AdventureWorks;"
            + "Integrated Security=true;";

        // Create the command strings for querying the Contact table.
        string contactSelectCommand = "SELECT ContactID, Title, FirstName, LastName, EmailAddress, Phone FROM Person.Contact";

        // Create the contacts data adapter.
        contactsDataAdapter = new SqlDataAdapter(
            contactSelectCommand,
            connectionString);

        // Create a command builder to generate SQL update, insert, and
        // delete commands based on the contacts select command. These are used to
        // update the database.
        SqlCommandBuilder contactsCommandBuilder = new SqlCommandBuilder(contactsDataAdapter);

        // Fill the data set with the contact information.
        contactsDataAdapter.Fill(dataSet, "Contact");

    }
    catch (SqlException ex)
    {
        MessageBox.Show(ex.Message);
    }
}

How to: Bind a DataView Object to a Windows Forms DataGridView Control

This is all native, I just googled "datagridview.datasource to dataview" maybe I mis-read and this doesn't solve your problem but comment if so and I will try and help

private void GetData()
{
    try
    {
        // Initialize the DataSet.
        dataSet = new DataSet();
        dataSet.Locale = CultureInfo.InvariantCulture;

        // Create the connection string for the AdventureWorks sample database.
        string connectionString = "Data Source=localhost;Initial Catalog=AdventureWorks;"
            + "Integrated Security=true;";

        // Create the command strings for querying the Contact table.
        string contactSelectCommand = "SELECT ContactID, Title, FirstName, LastName, EmailAddress, Phone FROM Person.Contact";

        // Create the contacts data adapter.
        contactsDataAdapter = new SqlDataAdapter(
            contactSelectCommand,
            connectionString);

        // Create a command builder to generate SQL update, insert, and
        // delete commands based on the contacts select command. These are used to
        // update the database.
        SqlCommandBuilder contactsCommandBuilder = new SqlCommandBuilder(contactsDataAdapter);

        // Fill the data set with the contact information.
        contactsDataAdapter.Fill(dataSet, "Contact");

    }
    catch (SqlException ex)
    {
        MessageBox.Show(ex.Message);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文