我对这一切都有点陌生,所以我会尽量具体一点..我正在尝试创建一个在另一个表单中显示两个日期的按钮。因此,我写了以下内容:
DataView dv = new DataView(dataComercioDataSet.Comex);
dv.Sort = "Id";
int ixe = dv.Find(idTextBox.Text);
DateTime embarque = Convert.ToDateTime(dv[ixe]["FechaEmbarque"]);
otherForm.fechaEmbarqueDateTimePicker.Value = embarque;
DateTime vencimiento = Convert.ToDateTime(dv[ixe]["FechaVencimiento"]);
otherForm.fechaVencimientoDateTimePicker.Value = vencimiento;
otherForm.idBox1.Text = dv[ixe]["Id"].ToString();
this.comexTableAdapter.FillBy3(this.dataComercioDataSet.Comex, c41TextBox.Text);现在,当我单击按钮时,它捕捉到一个异常,表明它是一个DBNull对象。因此,我决定通过添加以下内容来测试它:
if (dv.Count == 1)
{
MessageBox.Show("1");
}
if (dv.Count == 0) ;
{
MessageBox.Show("0");
}它同时显示了这两种情况!因为异常声明它是DBNull,所以我认为dv.find必须返回0,所以我计算如下:
if (ixe == 0)
{
ixe = 1;
DateTime embarque = Convert.ToDateTime(dv[ixe]["FechaEmbarque"]);
otherForm.fechaEmbarqueDateTimePicker.Value = embarque;
DateTime vencimiento = Convert.ToDateTime(dv[ixe]["FechaVencimiento"]);
otherForm.fechaVencimientoDateTimePicker.Value = vencimiento;
otherForm.idBox1.Text = dv[ixe]["Id"].ToString();
this.comexTableAdapter.FillBy3(this.dataComercioDataSet.Comex, c41TextBox.Text);
}但是当我这样做的时候,例外的是索引1要么是负的,要么比行数高(它是西班牙语的,我不知道这是不是实际的翻译),我想我不太明白DataView.Find()实际上是如何索引结果的,我的意思是,行1=1还是0?
提前感谢!
发布于 2015-06-05 20:47:05
设法解决了这个问题,不知何故改变了
DataView dv = new DataView(dataComercioDataSet.Comex);至
DataView dv = new DataView();
dv = dataComercioDataSet.Comex.AsDataView();解决了这个问题,虽然不确定为什么.
https://stackoverflow.com/questions/30650719
复制相似问题