未将对象引用设置到对象的实例.

guy晨 2016-08-25 10:20:29

private void toolStripButton1_Click(object sender, EventArgs e)
{

if (this.toolStripTextBox1.Text.Equals(""))
{
return;
}


// Linq模糊查询
IEnumerable<DataGridViewRow> enumerableList = this.dataGridView1.Rows.Cast<DataGridViewRow>();
List<DataGridViewRow> list = (from item in enumerableList
where item.Cells[0].Value.ToString().IndexOf(this.toolStripTextBox1.Text) >= 0
select item).ToList();

// 恢复之前行的背景颜色为默认的白色背景
this.dataGridView1.Rows[beforeMatchedRowIndex].DefaultCellStyle.BackColor = System.Drawing.Color.White;

if (list.Count > 0)
{
// 查找匹配行高亮显示
int matchedRowIndex = list[0].Index;
this.dataGridView1.Rows[matchedRowIndex].DefaultCellStyle.BackColor = System.Drawing.Color.Yellow;
this.beforeMatchedRowIndex = matchedRowIndex;
}

}

运行查询后系统提示 item.Cells[0].Value.ToString().IndexOf(this.toolStripTextBox1.Text) >= 0 未将对象引用设置到对象的实例.
网上说要进行赋值,请问怎么修改,谢谢指点!
...全文
197 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
正怒月神 2016-08-25
  • 打赏
  • 举报
回复
哪里为null了,自己断点一下就知道了
巴士上的邂逅 2016-08-25
  • 打赏
  • 举报
回复
为null,是因为在这一行是新建行时,Value都为null,如果不需要用户在DataGridview上直接添加行就将AllowUserToAddRows改为=false即可!
笑容天下事 2016-08-25
  • 打赏
  • 举报
回复
List<DataGridViewRow> list = (from item in enumerableList where item.Cells[0].Value.ToString().IndexOf(this.toolStripTextBox1.Text) >= 0 select item).ToList(); where item.Cells!=null && item.Cells.Count>0 && item.Cells[0].Value!=null && item.Cells[0].Value.Length>0 && item.Cells[0].Value.ToString().IndexOf(this.toolStripTextBox1.Text) >= 0
guy晨 2016-08-25
  • 打赏
  • 举报
回复
引用 4 楼 yuankaiwsl 的回复:
两种方法 1、AllowUserToAddRows改为=false 2、相应的代码改成
List<DataGridViewRow> list = (from item in enumerableList
                              where !item.IsNewRow && item.Cells[0].Value.ToString().IndexOf(this.toolStripTextBox1.Text) >= 0
                              select item).ToList();
或者
List<DataGridViewRow> list = enumerableList.Where(dr => !dr.IsNewRow && dr.Cells[0].Value.ToString().IndexOf(this.toolStripTextBox1.Text) >= 0);
谢谢
巴士上的邂逅 2016-08-25
  • 打赏
  • 举报
回复
两种方法 1、AllowUserToAddRows改为=false 2、相应的代码改成
List<DataGridViewRow> list = (from item in enumerableList
                              where !item.IsNewRow && item.Cells[0].Value.ToString().IndexOf(this.toolStripTextBox1.Text) >= 0
                              select item).ToList();
或者
List<DataGridViewRow> list = enumerableList.Where(dr => !dr.IsNewRow && dr.Cells[0].Value.ToString().IndexOf(this.toolStripTextBox1.Text) >= 0);
笑容天下事 2016-08-25
  • 打赏
  • 举报
回复
if (list!=null && list.Count > 0) //先判断是否为空在判断是否有元素 { // 查找匹配行高亮显示 int matchedRowIndex = list[0].Index; this.dataGridView1.Rows[matchedRowIndex].DefaultCellStyle.BackColor = System.Drawing.Color.Yellow; this.beforeMatchedRowIndex = matchedRowIndex; }
guy晨 2016-08-25
  • 打赏
  • 举报
回复
引用 1 楼 yuankaiwsl 的回复:
加断点,看看是哪个为null,估计是item.Cells[0].Value==null。你是DataGridview.AllowUserToAddRows是不是为true?
.AllowUserToAddRows为true。 item.Cells[0].Value==null应该怎么修改呢,请指点。
巴士上的邂逅 2016-08-25
  • 打赏
  • 举报
回复
加断点,看看是哪个为null,估计是item.Cells[0].Value==null。你是DataGridview.AllowUserToAddRows是不是为true?

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧