2011-08-23 153 views
1

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

+0

显然,我可以只使用数据视图表属性,并以此作为DataGridView的DataSource。我会尝试一下,并将其作为解决方案,如果它的工作。 – Boumbles

回答

1

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

这是所有原生的,我只是用Google搜索“datagridview.datasource到数据视图”也许我错读,这不能解决您的问题,但评论,如果这样,我将尽力帮助

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); 
    } 
}