Populate a DataGridView with a DataView
本文关键字:DataView with DataGridView Populate | 更新日期: 2023-09-27 18:06:28
我想用我们可以假设已经包含数据的DataView填充我的DataGridView。我一直在寻找一种方法来做到这一点,但是所有的解决方案都涉及到其他数据结构,而不是DataView,或者涉及到使用我无法在这个项目中包含的其他库。我是否需要首先将数据视图转换为其他东西并使用它来填充数据视图?或者还有其他东西我可以使用比DataGridView以类似的方式显示信息?
尝试将DataGridView
的DataSource
属性设置为DataView
。DataSource
可以实现由DataView
实现的IBindingListView
或IList
接口(与其他与本案例无关的选项)。
更多信息请查看MSDN:
- 显示Windows窗体数据视图控件
- Windows窗体中的数据显示模式
- DataGridView。DataSource属性 <
- DataView类/gh>
如何:将数据视图对象绑定到Windows Forms数据视图控件
这都是原生的,我只是谷歌了一下"datagridview"。也许我读错了,这并不能解决你的问题,但如果是的话,请评论,我会尝试帮助
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);
}
}