设置列宽度时,未处理数据网格视图上显示的Nullreferenceexception

本文关键字:视图 网格 显示 Nullreferenceexception 数据网 数据 置列宽 未处理 | 更新日期: 2023-09-27 18:20:01

这是我的代码

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.Windows.Forms.dll中发生类型为System.NullReferenceException的未处理异常。附加信息:对象引用未设置为对象的实例。"我不知道错误是什么,请帮助我

设置列宽度时,未处理数据网格视图上显示的Nullreferenceexception

将Catch语句替换为:

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

因此,您将看到发生错误的行号

您解决了这个问题吗?我在使用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;
        }
    }

希望你能得到帮助!