【发布时间】:2015-08-19 11:17:55
【问题描述】:
我对这一切有点陌生,所以我会尽可能具体.. 我正在尝试创建一个以另一种形式显示两个日期的按钮。所以我写了这个:
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 ?
提前致谢!
【问题讨论】:
-
你会发现在 C# 中,集合是 0-indexed,这意味着第一项是索引 0。
标签: c# database winforms visual-studio-2013 dataview