【问题标题】:DataGridView Default Error DialogDataGridView 默认错误对话框
【发布时间】:2015-04-20 08:56:13
【问题描述】:

下面的方法(GetData)给了我这个错误:

DataGridView 默认错误对话框
DataGridView 出现以下异常:
System.ArgumentException:列“anNo”不属于表 _utJM_WOExCycleItem。
在 System.Data.DataRow.CheckColumn(DataColumn 列)
在 System.Data.DataColumnPropertyDescriptor.GetValue(对象组件)
在 System.Windows.Forms.DataGridView.DataGridViewDataConnection.GetValue(Int32 boundColumnIndex, Int32 columnIndex, Int32 rowIndex)
要替换此默认对话框,请处理 DataError 事件。

我可以找出问题所在。 当表_utJM_WOExCycle 在状态“O”或“N”中没有记录时,我收到此错误。当一条记录得到acStatusON,并且程序调用GetData方法时,我得到这个错误:

字段 anNo、acWoex、acIdent、acName 重复错误(_utJM_WOExCycleItem 中的 x 条记录)。

方法:

private void GetData(DataGridView dgwm, DataGridView dgwd, BindingSource bsm, BindingSource bsd, String resurs)
{
    dgwm.DataSource = bsm;
    dgwd.DataSource = bsd;

    DataSet ds = new DataSet();
    String SQL = "";

    SQL = " SELECT anCycle, acResurs, anYear, case when dbo._ufnJM_CycleCompare(anCycle, acResurs, anYear, 'P') <> dbo._ufnJM_CycleCompare(anCycle, acResurs, anYear, 'T') then 'Sprememba' else '' end acChange, acNote " +
            " FROM _utJM_WOExCycle " +
            " WHERE acResurs = '" + resurs + "' " +
            "       AND acStatus in ('O','N') " +
            " ORDER BY anCycle asc ";

    SqlDataAdapter sqlDataAdapterMaster = new SqlDataAdapter(SQL, cn); 
    sqlDataAdapterMaster.Fill(ds, "_utJM_WOExCycle");

    SQL = " SELECT anCycle, anNo, acWoex, acIdent, acName" +
            " FROM _utJM_WOExCycleItem " +
            " WHERE acResurs = '" + resurs + "' " +
            "   and anCycle in (select anCycle from RotoP.._utJM_WOExCycle where acStatus in ('O','N') and acResurs = '" + resurs + "') " +
            " ORDER BY anCycle asc, anNo asc ";

    SqlDataAdapter sqlDataAdapterDetail = new SqlDataAdapter(SQL, cn);
    sqlDataAdapterDetail.Fill(ds, "_utJM_WOExCycleItem");

    DataRelation relation = new DataRelation("Povezava1",
        ds.Tables["_utJM_WOExCycle"].Columns["anCycle"],
        ds.Tables["_utJM_WOExCycleItem"].Columns["anCycle"]);

    ds.Relations.Add(relation);

    bsm.DataSource = ds;
    bsm.DataMember = "_utJM_WOExCycle";

    bsd.DataSource = bsm;
    bsd.DataMember = "Povezava1";

    dgwm.ReadOnly = true;
    dgwd.ReadOnly = true;

    dgwm.AutoResizeColumns();

    dgwd.AutoSizeColumnsMode =
        DataGridViewAutoSizeColumnsMode.AllCells;

    dgwm.Columns["anYear"].Visible = false;
    dgwm.Columns["acResurs"].Visible = false;
    dgwm.Columns["acChange"].DefaultCellStyle.ForeColor = Color.Green;
    dgwm.Columns["acNote"].DefaultCellStyle.ForeColor = Color.Red;

    dgwm.Columns["acNote"].HeaderText = "Opomba";
    dgwm.Columns["anCycle"].HeaderText = "Cikel";
    dgwm.Columns["acChange"].HeaderText = "";

    dgwd.Columns["anCycle"].Visible = false;

    dgwd.Columns["anNo"].HeaderText = "Pozicija";
    dgwd.Columns["acWoex"].HeaderText = "Delovni nalog";
    dgwd.Columns["acIdent"].HeaderText = "Šifra izdelka";
    dgwd.Columns["acName"].HeaderText = "Naziv izdelka";

    dgwm.SetColumnSortMode(DataGridViewColumnSortMode.NotSortable);
    dgwd.SetColumnSortMode(DataGridViewColumnSortMode.NotSortable);
}

【问题讨论】:

  • 1.不要连接您的 sql 查询。使用参数。 2. 您是否单步执行了您的代码,您能否确认您的 Dataadapter 实际上用数据表填充了您的数据集?

标签: c# sql-server datagridview


【解决方案1】:

如果我们查看您的第一个查询,您没有任何名称为 anNo 的列。

然后您将此结果集与DataGridView 控件绑定,可能您的DataGridView 控件有一个与anNo 列映射的列,该列在数据源中找不到。

在设计模式中选择DataGridView控件(与_utJM_WOExCycle绑定)并检查您定义的列,将DataPropertyName设置为anNo的列,该列是实际问题。

【讨论】:

    【解决方案2】:

    在炸毁我的整个 EntityFramework 连接并再次完成该过程后,我遇到了这个非常模糊的错误。

    我能够确定我的问题在于 SQL 服务器如何存储我的图像。不要使用类型 Image 它已被弃用,它具有 byte[] 类型并保存为 0x433...

    将您的图像存储类型更改为 varbinary(Max) 它将另存为 0xFFD...解决了问题。

    回顾:

    1. 检查实体框架连接
    2. 将图像类型保存为 Varbinary(Max)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多