C# 使用DataView填充DataGridView
我想用我们可以假设已经包含数据的DataView来填充我的DataGridView。我一直在寻找一种方法来实现这一点,但所有的解决方案都涉及到除DataView之外的其他数据结构,或者涉及到使用我在这个项目中无法合并的其他库。我是否需要首先将DataView转换为其他内容并使用它来填充DataGridView?或者除了DataGridView之外,我还可以使用其他类似的方式显示信息吗 这都是本地的,我只是在谷歌上搜索了“datagridview.datasource to dataview”,也许我读错了,这并不能解决您的问题,但如果是这样的话,请发表评论,我会尽力帮助您C# 使用DataView填充DataGridView,c#,.net,datagridview,dataview,C#,.net,Datagridview,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: