Search code examples
c#datagridviewdatatabledatagridsage-erp

DataGrid generating blank rows


I am populating a DataTable with some values (after defining the column names) and then using it as a data source for a Sage Grid - functionally very similar to a WinForms DataGridView.

So far, I have tried adding the data type to the DataTable columns and populating a BindingSource with the DataTable and then binding it to the Sage Grid. When viewing the contents of the DataTable whilst debugging you can see the data is there using DataSet Visualiser.

DataSet Visualiser

Creating the DataTable -

DataTable failedOrders = new DataTable();

failedOrders.Columns.Add("externalItemCodeColumn", typeof(String));
failedOrders.Columns.Add("reasonColumn", typeof(String));

foreach (String item in insufficientItemsAvailable)
{
    DataRow dataRow = failedOrders.NewRow();
    dataRow["externalItemCodeColumn"] = item;
    dataRow["reasonColumn"] = "Not enough available items in WAREHOUSE";

    failedOrders.Rows.Add(dataRow);
}

Populating the Sage Grid -

Sage.Common.Controls.GridColumn externalItemCodeColumn = new Sage.Common.Controls.GridColumn();
externalItemCodeColumn.Caption = "External Item Code";
externalItemCodeColumn.DisplayMember = "externalItemCodeColumn";

Sage.Common.Controls.GridColumn reasonColumn = new Sage.Common.Controls.GridColumn();
reasonColumn.Caption = "Reason";
reasonColumn.DisplayMember = "reasonColumn";

failedOrdersGrid.Columns.Add(externalItemCodeColumn);
failedOrdersGrid.Columns.Add(reasonColumn);

failedOrdersGrid.DataSource = FailedOrders;
//failedOrdersGrid.Refresh(); - this doesn't seem to make a difference

Please note that failedOrders is passed to another method hence the name change from failedOrders to FailedOrders.

Just to check that this behaviour is not specific to the Sage Grid, I tried populating a regular WinForms DGV with the DataTable. (Note - I disabled AutoGenerateColumns as this does not seem to be an option for the Sage Grid).

dataGridView1.AutoGenerateColumns = false;
dataGridView1.Columns.Add("externalItemCodeColumn", "External Item Code");
dataGridView1.Columns.Add("reasonColumn", "Reason");
dataGridView1.DataSource = FailedOrders;

I expect the contents of the Sage Grid to match those of the DataGrid, but instead get blank rows.

Form


Solution

  • The Sage.Common.Controls.Grid inherits from the Sage.Common.Controls.List and the DataSource property of Sage.Common.Controls.List requires on refresh that the new DataSource supports at least IList (It literally just nulls out your datasource if it isn't an IList)

    Here's an adaptation of your code to work with the Sage.Common.Controls.Grid

    I added this to the constructor of my form (where failedOrdersGrid is of type Sage.Common.Controls.Grid):

            List<Failure> failedOrders = new List<Failure>();
    
            foreach (String item in new List<string> { "1", "2", "3" })
            {
                failedOrders.Add(new Failure { externalItemCodeColumn = item, reasonColumn = "Not enough available items in WAREHOUSE" });
            }
    
            Sage.Common.Controls.GridColumn externalItemCodeColumn = new Sage.Common.Controls.GridColumn();
            externalItemCodeColumn.Caption = "External Item Code";
            externalItemCodeColumn.DisplayMember = "externalItemCodeColumn";
    
            Sage.Common.Controls.GridColumn reasonColumn = new Sage.Common.Controls.GridColumn();
            reasonColumn.Caption = "Reason";
            reasonColumn.DisplayMember = "reasonColumn";
    
            failedOrdersGrid.Columns.Add(externalItemCodeColumn);
            failedOrdersGrid.Columns.Add(reasonColumn);
    
            failedOrdersGrid.DataSource = failedOrders;
    

    And here's the failure class:

    public class Failure
    {
        public string externalItemCodeColumn { get; set; }
        public string reasonColumn { get; set; }
    }