You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Try-Catch处理DataGridView空引用异常并将空值设为字符串"Null"或空

解决方案:处理DataGridView空值避免NullReferenceException

当然可以用Try-Catch捕获异常,但其实还有更优雅、高效的方式来处理空值——毕竟异常处理的开销不小,而且从根源避免问题比事后捕获要更合理。

先说说你当前代码的问题:你的Catch块是空的,就算捕获到空引用异常,也不会做任何处理;而且只要某一个单元格为空,整个Try块的后续赋值都会中断,导致后面的TextBox都拿不到数据。这显然不是你想要的效果。

方法一:使用空合并运算符(推荐)

C# 6.0及以上支持?.(空条件运算符)和??(空合并运算符),可以一行代码搞定空值处理,完全避免NullReferenceException:

private void dataGridViewE_CellEnter(object sender, DataGridViewCellEventArgs e)
{
    // 如果单元格Value为空,就显示"Null",否则显示对应值
    employeetxt.Text = dataGridViewE.CurrentRow.Cells[1].Value?.ToString() ?? "Null";
    lasttxt.Text = dataGridViewE.CurrentRow.Cells[2].Value?.ToString() ?? "Null";
    firsttxt.Text = dataGridViewE.CurrentRow.Cells[3].Value?.ToString() ?? "Null";
    titletxt.Text = dataGridViewE.CurrentRow.Cells[4].Value?.ToString() ?? "Null";
    titleoftxt.Text = dataGridViewE.CurrentRow.Cells[5].Value?.ToString() ?? "Null";
    birthtxt.Text = dataGridViewE.CurrentRow.Cells[6].Value?.ToString() ?? "Null";
    hiretxt.Text = dataGridViewE.CurrentRow.Cells[7].Value?.ToString() ?? "Null";
    addresstxt.Text = dataGridViewE.CurrentRow.Cells[8].Value?.ToString() ?? "Null";
    citytxt.Text = dataGridViewE.CurrentRow.Cells[9].Value?.ToString() ?? "Null";
    regiontxt.Text = dataGridViewE.CurrentRow.Cells[10].Value?.ToString() ?? "Null";
    postaltxt.Text = dataGridViewE.CurrentRow.Cells[11].Value?.ToString() ?? "Null";
    countrytxt.Text = dataGridViewE.CurrentRow.Cells[12].Value?.ToString() ?? "Null";
    hometxt.Text = dataGridViewE.CurrentRow.Cells[13].Value?.ToString() ?? "Null";
    extensiontxt.Text = dataGridViewE.CurrentRow.Cells[14].Value?.ToString() ?? "Null";
    phototxt.Text = dataGridViewE.CurrentRow.Cells[15].Value?.ToString() ?? "Null";
    notestxt.Text = dataGridViewE.CurrentRow.Cells[16].Value?.ToString() ?? "Null";
    reportstxt.Text = dataGridViewE.CurrentRow.Cells[17].Value?.ToString() ?? "Null";
    photopathtxt.Text = dataGridViewE.CurrentRow.Cells[18].Value?.ToString() ?? "Null";
}
  • ?.的作用是:如果Value不为null,就调用ToString();如果为null,直接返回null,不会触发空引用异常。
  • ??的作用是:如果前面的结果是null,就替换成后面的"Null"字符串(你也可以换成空字符串"")。

方法二:封装辅助方法

如果你的项目版本比较旧,不支持空条件运算符,可以写一个简单的辅助方法来处理:

// 辅助方法:安全获取单元格文本
private string GetSafeCellText(DataGridViewCell cell)
{
    // 可以根据需求改成返回空字符串""
    return cell.Value == null ? "Null" : cell.Value.ToString();
}

// 调用辅助方法赋值
private void dataGridViewE_CellEnter(object sender, DataGridViewCellEventArgs e)
{
    employeetxt.Text = GetSafeCellText(dataGridViewE.CurrentRow.Cells[1]);
    lasttxt.Text = GetSafeCellText(dataGridViewE.CurrentRow.Cells[2]);
    // ... 其他TextBox同理
}

方法三:使用Try-Catch(不推荐,但满足你的需求)

如果你一定要用Try-Catch,那至少要在Catch块里做处理,而且最好是逐个单元格捕获(避免一个空值影响所有赋值):

private void dataGridViewE_CellEnter(object sender, DataGridViewCellEventArgs e)
{
    // 逐个处理单元格,避免一个空值导致所有赋值失败
    try
    {
        employeetxt.Text = dataGridViewE.CurrentRow.Cells[1].Value.ToString();
    }
    catch (NullReferenceException)
    {
        employeetxt.Text = "Null";
    }

    try
    {
        lasttxt.Text = dataGridViewE.CurrentRow.Cells[2].Value.ToString();
    }
    catch (NullReferenceException)
    {
        lasttxt.Text = "Null";
    }

    // ... 其他TextBox同理
}

不过这种方式代码冗余度很高,而且异常处理的性能不如前面两种方法,所以更推荐前两种方案。

内容的提问来源于stack exchange,提问作者Jason_27

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 17:37:49