2014-08-30 63 views
-1

這是我的代碼的NullReferenceException是未處理顯示在datagridview的設置欄

connection.Open(); 
     try 
     { 
      adpSup.SelectCommand = new SqlCommand("SELECT Supplier_Supplier AS 'Supplier', Supplier_TP AS 'Telephone', Supplier_EMail AS 'E-Mail', Supplier_Address AS 'Address' FROM Supplier", connection); 
      dsSup.Clear(); 
      adpSup.Fill(dsSup, "tblSupplier"); 
      dgSupplier.DataSource = dsSup.Tables["tblSupplier"]; 
      dgSupplier.Columns["Telephone"].Width = 70; 

      foreach (DataGridViewColumn col in dgSupplier.Columns) 
      { 
       col.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter; 
       col.HeaderCell.Style.Font = new Font("Arial", 12F, FontStyle.Bold, GraphicsUnit.Pixel); 
      } 
     } 
     catch (SqlException ex) 
     { 
      MessageBox.Show(ex.Message, "Report", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Error); 
     } 
     finally 
     { 
      connection.Close(); 
     } 

的寬度時,當我運行這段代碼它顯示「類型‘System.NullReferenceException’未處理的異常發生在System.Windows。 Forms.dll其他信息:對象引用未設置爲對象的實例。「 我不知道什麼是錯誤的,請幫我

回答

0

更換你的Catch語句來此:

catch (Exception ex) 
    { 
     MessageBox.Show(ex.ToString(), "Report", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Error); 
    } 

,所以你會看到的行號發生錯誤

+0

仍然會出現問題。在這條線上顯示的問題 'dgSupplier.Columns [「Telephone」]。Width = 70;' – kavi 2014-08-31 00:50:25

-1

難道你解決了這個問題? 當我使用DataGridView時,我也遇到了這個問題。 最後,我通過「刪除」解決了它。

,所以我想,該塊在你的代碼

dgSupplier.DataSource = dsSup.Tables["tblSupplier"]; 
     dgSupplier.Columns["Telephone"].Width = 70; 

     foreach (DataGridViewColumn col in dgSupplier.Columns) 
     { 
      col.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter; 
      col.HeaderCell.Style.Font = new Font("Arial", 12F, FontStyle.Bold, GraphicsUnit.Pixel); 
     } 

應該由委託運行。

我這是怎麼計算出來的我的代碼:

 //prepare data in other thread: 
      String line; 
      String[] split = null; 
      DataTable table = new DataTable(); 
      DataRow row = null; 
      StreamReader sr = new StreamReader(pCsvPath, Encoding.Default); 
      line = sr.ReadLine(); 
      split = line.Split(','); 
      foreach (String colname in split) 
      { 
       table.Columns.Add(colname, System.Type.GetType("System.String")); 
      } 
      //fill the data to the datatable 
      int j = 0; 
      while ((line = sr.ReadLine()) != null) 
      { 
       j = 0; 
       row = table.NewRow(); 
       split = line.Split(','); 
       foreach (String colname in split) 
       { 
        row[j] = colname; 
        j++; 
       } 
       table.Rows.Add(row); 
      } 
      sr.Close(); 

      //use the delegate 
      parent.showDataview(table.DefaultView); 

遵循的是委託碼主線程你

private delegate void ShowDatagridView(DataView dataView); 
    public void showDataview(DataView dataView) 
    { 
     if (this.InvokeRequired) 
     { 
      ShowDatagridView show = new ShowDatagridView(showDataview); 
      this.Invoke(show, new object[] { dataView }); 
     } 
     else 
     { 
      pmGridview.DataSource = dataView; 
     } 
    } 

希望幫助!

相關問題