2012-03-04 61 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!