使用DataView填充DataGridView

时间:2011-08-23 08:57:03

标签: c# .net datagridview dataview

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

2 个答案:

答案 0 :(得分:1)

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

这都是原生的,我只是用谷歌搜索“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);
    }
}

答案 1 :(得分:1)

尝试将DataSource的{​​{1}}属性设置为DataGridViewDataView可以实现DataSourceIBindingListView接口(以及与此案例相关的其他选项。),这两个接口都由IList实现。

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