Kiki
Kiki

Reputation: 770

DataGridView Default Error Dialog

Method below (GetData) gives me this error:

DataGridView Default Error Dialog
The following exception occurred in the DataGridView:
System.ArgumentException: Column 'anNo' does not belong to table _utJM_WOExCycleItem.
at System.Data.DataRow.CheckColumn(DataColumn column)
at System.Data.DataColumnPropertyDescriptor.GetValue(Object component)
at System.Windows.Forms.DataGridView.DataGridViewDataConnection.GetValue(Int32 boundColumnIndex, Int32 columnIndex, Int32 rowIndex)
To replace this default dialog please handle the DataError event.

I can figure out what is wrong. I get this error when table _utJM_WOExCycle does not have a record in status 'O' or 'N'. When one record get acStatus O or N, and program calls GetData method, I get this error:

Error is repeated for fields anNo, acWoex, acIdent, acName (x number of records in _utJM_WOExCycleItem).

Method:

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);
}

Upvotes: 0

Views: 2289

Answers (2)

Imhotep
Imhotep

Reputation: 3

I had this error which is so ambiguous after blowing up my entire EntityFramework connections and going through the process again.

I was able to find identify that my issue was with how SQL server was storing my image..don't use type Image it is deprecated it has a byte[] type and saves as 0x433...

Change your image storage type to varbinary(Max) it will save as 0xFFD... resolved the issue.

Recap:

  1. Check Entity Framework connection
  2. Save Image type as Varbinary(Max)

Upvotes: 0

sallushan
sallushan

Reputation: 1147

If we look at your first query, you don't have any column with name anNo.

And then you are binding this result set with a DataGridView control, probably your DataGridView control has a column mapped with anNo column which cannot be found in data source.

Select the DataGridView control (which is binding with _utJM_WOExCycle) in Design Mode and check the columns which you have defined, the column which has DataPropertyName set to anNo, that column is actual problem.

Upvotes: 0

Related Questions