C# 使用DataView填充DataGridView

C# 使用DataView填充DataGridView,c#,.net,datagridview,dataview,C#,.net,Datagridview,Dataview,我想用我们可以假设已经包含数据的DataView来填充我的DataGridView。我一直在寻找一种方法来实现这一点,但所有的解决方案都涉及到除DataView之外的其他数据结构,或者涉及到使用我在这个项目中无法合并的其他库。我是否需要首先将DataView转换为其他内容并使用它来填充DataGridView?或者除了DataGridView之外,我还可以使用其他类似的方式显示信息吗 这都是本地的,我只是在谷歌上搜索了“datagridview.datasource to dataview”,

我想用我们可以假设已经包含数据的DataView来填充我的DataGridView。我一直在寻找一种方法来实现这一点,但所有的解决方案都涉及到除DataView之外的其他数据结构,或者涉及到使用我在这个项目中无法合并的其他库。我是否需要首先将DataView转换为其他内容并使用它来填充DataGridView?或者除了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);
    }
}

尝试将
DataGridView
DataSource
属性设置为您的
DataView
DataSource
可以实现由
DataView
实现的
IBindingListView
IList
接口(以及与本例相关的其他选项)

有关更多信息,请查看MSDN:


显然,我可以使用DataView的Table属性,并将其用作DataGridView的数据源。我会尝试一下,如果可行的话,把它作为解决方案。