【问题标题】:DataView.Count() returns more than one valueDataView.Count() 返回多个值
【发布时间】: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


【解决方案1】:

设法解决了这个问题,以某种方式改变

DataView dv = new DataView(dataComercioDataSet.Comex);

 DataView dv = new DataView();
          dv = dataComercioDataSet.Comex.AsDataView();

解决了这个问题,但不知道为什么......

【讨论】:

    猜你喜欢
    • 2010-09-29
    • 1970-01-01
    • 2021-11-30
    • 2018-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多