2012-03-04 60 views
0

我有2个数据网格视图(ValidationRun是主,结果是详细信息)。我将它绑定到我知道包含数据的数据集 - 我可以在调试时看到它。但是,我没有获得网格中的数据。Datagridview不显示数据

任何想法?

/class level objects 
private DataGridView dgvValidationRun = new DataGridView(); 
private BindingSource bsValidationRun = new BindingSource(); 
private DataGridView dgvResults = new DataGridView(); 
private BindingSource bsResults = new BindingSource(); 

private void Form1_Load(object sender, EventArgs e)  
    {  

     // Bind the DataGridView controls to the BindingSource  
     // components and load the data from the database.  
     dgvValidationRun.DataSource = bsValidationRun;  
     dgvResults.DataSource = bsResults;  
     GetData();  

     // Resize the master DataGridView columns to fit the newly loaded data.  
     dgvValidationRun.AutoResizeColumns();  

     // Configure the details DataGridView so that its columns automatically  
     // adjust their widths when the data changes.  
     dgvResults.AutoSizeColumnsMode =  
      DataGridViewAutoSizeColumnsMode.AllCells;  

    } 

private void GetData()  
    {  
     try  
     {  
      SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["XMLValidate.Properties.Settings.ValidationResultsConnectionString"].ConnectionString);  

      // Create a DataSet.  
      DataSet data = new DataSet();  
      data.Locale = System.Globalization.CultureInfo.InvariantCulture;  

      // Add data from the Customers table to the DataSet.  
      SqlDataAdapter daValidationRun = new SqlDataAdapter("select * from ValidationRun", connection);  
      daValidationRun.Fill(data, "ValidationRun");  

      // Add data from the Orders table to the DataSet.  
      SqlDataAdapter daResults = new SqlDataAdapter("select * from Result", connection);  
      daResults.Fill(data, "Result");  

      // Establish a relationship between the two tables.  
      DataRelation relRunResult = new DataRelation("RunResult",  
       data.Tables["ValidationRun"].Columns["RunId"],  
       data.Tables["Result"].Columns["RunId"]);  
      data.Relations.Add(relRunResult);  

      // Bind the run data connector to the Run table.  
      bsValidationRun.DataSource = data;  
      bsValidationRun.DataMember = "ValidationRun";  

      // Bind the results data connector to the results data connector,  
      // using the DataRelation name to filter the information in the  
      // details table based on the current row in the master table.  
      bsResults.DataSource = bsValidationRun;  
      bsResults.DataMember = "RunResult";  

      dgvValidationRun.AutoGenerateColumns = true;  
      dgvValidationRun.Refresh();  
     }  
     catch (Exception ex)  
     {  
      int i = 1;  
     }  
    } 
+0

是dgvValidationRun添加到您的表单控件? – nawfal 2012-03-04 08:38:30

+0

我怀疑是非常微不足道的东西,请删除你的catch,让我们看看是否有一些异常发生。 – Steve 2012-03-04 09:25:17

+0

非常感谢nawfal,那就是问题所在。我有几个数据网格,我通过设计器添加到表单中,但忘记给它们与代码隐藏名称相同的名称 - doh! – 2012-03-04 15:16:41

回答

0

非常感谢nawfal,那就是问题所在。我有几个数据网格,我通过设计器添加到表单中,但忘记给它们与代码隐藏名称相同的名称 - doh!