这C#未将对象引用设置到对象的实例

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace 数据库
{
public partial class 工种种类 : Form
{
string connStr = "Data Source=.;Initial Catalog=db_person;User ID=sa;Password=1";
SqlConnection conn;
DataTable ds;
int flag = 0;
public 工种种类()
{
InitializeComponent();

    }

    public void gzlx_SelectionChanged(object sender, EventArgs e)
    {
        if (gzlx.CurrentRow != null)
        {

            tbxbh.Text = gzlx.CurrentRow.Cells[0].Value.ToString();
            tbxmc.Text = gzlx.CurrentRow.Cells[1].Value.ToString();
            tbxms.Text = gzlx.CurrentRow.Cells[2].Value.ToString();
        }
    }
    private void a()
    {
        buttj.Enabled = false;
        butxg.Enabled = false;
        butsc.Enabled = true;
        gbxDetailed.Enabled = false;
    }
    private void b()
    {
        buttj.Enabled = false;
        butxg.Enabled = false ;
        butsc.Enabled =false ;
        gbxDetailed.Enabled =true;           
    }

    private void 工种种类_Load_1(object sender, EventArgs e)
    {
        conn = new SqlConnection(connStr);
        string sql = "select * from tb_JobInfo";
        SqlCommand sb = new SqlCommand(sql, conn);
        a();
        ds = new DataTable();
        conn.Open();
        SqlDataReader cnm = sb.ExecuteReader(CommandBehavior.CloseConnection);
        if (cnm.HasRows)
        {
            ds.Load(cnm);
            cnm.Close();
            gzlx.DataSource = null;
            gzlx.DataSource = ds;
        }

    }

    private void buttj_Click(object sender, EventArgs e)
    {
        b();
        tbxbh.Enabled = true;
        tbxbh.Focus();
    }

    private void butxg_Click(object sender, EventArgs e)
    {
        b();
        tbxbh.Enabled = false;
        tbxmc.Focus();
    }
    //删除
    private void butsc_Click(object sender, EventArgs e)
    {
        string sql = "delete from tb_JobInfo where JobID " + gzlx.CurrentRow.Cells[0].Value;
        SqlCommand cnm = new SqlCommand(sql, conn);
        conn.Open();
        conn.Close();
    }
    //添加
    private void btnSu_Click(object sender, EventArgs e)
    {
        if (flag == 1)
        {
        string sql = "insert into tb_JobInfo(JobID,JobName,Remark) lalues({0}',{1},{2}'{3}')";
        sql = string.Format(sql, Convert.ToInt16(tbxbh.Text.Trim()), tbxmc.Text.Trim(), tbxms.Text.Trim());
        SqlCommand cnm = new SqlCommand(sql, conn);
        conn.Open();
        conn.Close();
        工种种类_Load_1(null, null);
        a();
        }
        else if (flag == 2)
        {
        string sql = "update tb_JobInfo set JobID='{1}',JobName'{2}'where Remark {0}";
        sql = string.Format(sql, Convert.ToInt32(tbxbh.Text), tbxmc.Text.Trim(), tbxms.Text.Trim());
        SqlCommand cnm = new SqlCommand(sql, conn);
        conn.Close();
        }
        工种种类_Load_1(null, null);
        a();
        tbxbh.Enabled = true;
    }

    private void btncancel_Click(object sender, EventArgs e)
    {
        a();
    }
}

}图片说明


gzlx.CurrentRow.Cells[0].Value看下这个有没有值,如果为null会报这个错,没问题的话再看下gzlx.CurrentRow.Cells[0]的值有没有问题

主要是CurrentRow,你有没有先选中一行?然后看你表格有没有内容