2014-08-30 62 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; 
     } 
    } 

希望帮助!

相关问题