using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace WindowsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“sanfuyibiaoDataSet.lyxstart”中。您可以根据需要移动或移除它。
this.lyxstartTableAdapter.Fill(this.sanfuyibiaoDataSet.lyxstart);
label2.Text = dataGridView1[1, 0].Value.ToString();
label3.Text = dataGridView1[1, 3].Value.ToString();
} private void button1_Click(object sender, EventArgs e)
{
bool a = false;
for (int x = 0; x < dataGridView1.RowCount; x++)
{
if (textBox1.Text==dataGridView1[1,x].Value.ToString().Trim())
{
a = true;
break;
}
}
if (a)
{
label1.Text = "值相同!";
}
else
{
label1.Text = "值不同!";
}
}
}
}请高手看看这句 if (textBox1.Text==dataGridView1[1,x].Value.ToString().Trim())错误提示是 未将对象引用设置到对象的实例
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace WindowsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“sanfuyibiaoDataSet.lyxstart”中。您可以根据需要移动或移除它。
this.lyxstartTableAdapter.Fill(this.sanfuyibiaoDataSet.lyxstart);
label2.Text = dataGridView1[1, 0].Value.ToString();
label3.Text = dataGridView1[1, 3].Value.ToString();
} private void button1_Click(object sender, EventArgs e)
{
bool a = false;
for (int x = 0; x < dataGridView1.RowCount; x++)
{
if (textBox1.Text==dataGridView1[1,x].Value.ToString().Trim())
{
a = true;
break;
}
}
if (a)
{
label1.Text = "值相同!";
}
else
{
label1.Text = "值不同!";
}
}
}
}请高手看看这句 if (textBox1.Text==dataGridView1[1,x].Value.ToString().Trim())错误提示是 未将对象引用设置到对象的实例
解决方案 »
- 我在windowsform窗体应用程序中可以控制DOS命令的输入和执行吗?
- 【GDI+】急救~画图程序的坐标转换问题?
- LIST如何按照集合外值排序?
- 从第二窗体回到第一窗体
- 如何通过ToString()把时间2007年6月2日转化成<a href="/a.aspx?y=2007">2007</a>年6月2日
- Winform 的Datagrid中如何增加一个按钮列?
- 有没有把EMF图片转化成AutoCAD的DXF或DWG格式的控件?
- 多线创建PictureBox,并将每个图片加到一个Panel中 的问题
- VS 2013 开发C# 时候的问题!
- 强烈建议csdn提供更好的编辑器
- 栏目权限表如何控制
- asp.net的安全性问题。
if (textBox1.Text==dataGridView1[1][x].Value.ToString().Trim())
你Debug下 看看dataGridView1[1,x].Value是不是为Null
判断
if(!string.IsEmptyOrEmpty(dataGridView1[1,x].Value))
{if (textBox1.Text==dataGridView1[1,x].Value.ToString().Trim())
{
a = true;
break;
}}
试试这个
要先和DBNULL对比
if(dataGridView1[1,x].Value!=null)
{
if (textBox1.Text==dataGridView1[1,x].Value.ToString().Trim())
}先做null判断
奇怪了,怎么那么多人眼睛干什么去了,这个明显是个低级错误,第一个是行号,第二个才是列号,楼主弄反了,那些不懂程序的人不要乱指点,免得误人.什么"dataGridView1[1,x].Value"可能是null,你这么认定之前也不想想,Value是dataGridView1[row,cum]的固有属性,是通过get访问器返回的,怎么可能是空呢?当人家微软吃干饭的,会返回空值?
{
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if (dataGridView1.Rows[i].Cells["EmailTypeName"].Value.ToString() == "未阅读")
{
for (int j = 0; j < dataGridView1.Rows[i].Cells.Count; j++)
{
dataGridView1.Rows[i].Cells[j].Style.BackColor = Color.Red;
}
}
}
}
别的不看,单看这句,这句什么意思呢?意思就是dataGridView1有RowCount行,然后一行行遍历,楼主是这个意思吧?这里变量x就是行号了,没错吧?好,我们再看看System.Windows.Forms.DataGrid类型,你的dataGridView1是这里类型的吧?帮助里面明显提示GridView[int rowIndex,int columnIndex],rowIndex在这里就是行号,也就是变量x.而楼主"if (textBox1.Text==dataGridView1[1,x].Value.ToString().Trim())"把行号错误的当作列号使用,当x的值超过列数时,dataGridView1[1,x]返回的就是null,了然后再引用就会出现经典提示错误!