Search code examples
c#visual-studioobjectlistview

My PrintListView adds my Data, but does not add it all onto 1 row


I am writing a program that involves an PrintListView, I have gotten the Data to show, however each part of the Data is on a different Row. The Image marked as [1] below contains my PrintListView, which shows the Data on each different row, I want this Data to be on 1 row only. I am taking a DataSet, which I then turn into a DataTable. I then add the rows of each DataTable to a List, I have checked this in debug, and it only shows up with 3 values, which is great; but they are not on the same row. The Programming Language is C#.

https://i.sstatic.net/XAvEq.png [1]

If you could help, that would be handy.

Many Thanks.

 public ObjectListView CreateOLVForSingleColumns(DataSet ds)
    {
        var olv = new ObjectListView();
        olv.ShowGroups = false;
        var colHeaders = new List<ColumnHeader>();
        var objectList = new List<DataRow>();

        //Setting the Column Names of the OLV
        var totalOrders = new OLVColumn {AspectName = "Total_Orders", Text = "Total Orders", Width = 250};
        var totalOrderValue = new OLVColumn {AspectName = "Total_Order_Value", Text = "Total Order Value", Width = 250};
        var totalOrdersNotInvoiced = new OLVColumn {AspectName = "Total_Value_Of_Orders_Not_Invoiced", Text = "Total Order Value Not Invoiced", Width = 250};
        colHeaders.AddRange(new ColumnHeader[] {totalOrders, totalOrderValue, totalOrdersNotInvoiced});
        olv.Columns.AddRange(colHeaders.ToArray());


        var dt = ds.Tables["Total Amount of Orders"];
        var dt2 = ds.Tables["Total Value of Orders"];
        var dt3 = ds.Tables["Total Value of Orders Not Invoiced"];

        var dt4 = new DataTable();

        dt4.Merge(dt);
        dt4.Merge(dt2);
        dt4.Merge(dt3);

        for (int i = 1; i <= dt4.Rows.Count; i++)
        {
            var value = dt4.Rows[0];
            var value2 = dt4.Rows[1];
            var value3 = dt4.Rows[2];

            olv.AddObjects(new DataRow[] { value, value2, value3 });

        }

Solution

  • I think your problem is with DataTable.Merge not with ObjectListView.

    I would suggest changing your data source so it all comes back in one table (e.g. by using a JOIN if your reading SQL). That said you can accomplish what you want with something like this:

    var dt4 = new DataTable();
    
    //add all the columns
    foreach (DataColumn c in 
        dt.Columns.Cast<DataColumn>()
            .Union(dt2.Columns.Cast<DataColumn>())
            .Union(dt3.Columns.Cast<DataColumn>()))
    {
        dt4.Columns.Add(c.ColumnName, c.DataType);
    }
    if(dt.Rows.Count != dt2.Rows.Count|| dt.Rows.Count != dt3.Rows.Count)
        throw new Exception("DataTables had different row counts");
    
    for (int i = 0; i < dt.Rows.Count; i++)
    {
        var row = dt4.Rows.Add();
    
        foreach (DataColumn c in dt.Columns)
            if (dt.Rows[i][c] != DBNull.Value)
                row[c.ColumnName] = dt.Rows[i][c];
    
        foreach (DataColumn c in dt2.Columns)
            if (dt2.Rows[i][c] != DBNull.Value)
                row[c.ColumnName] = dt2.Rows[i][c];
    
        foreach (DataColumn c in dt3.Columns)
            if (dt3.Rows[i][c] != DBNull.Value)
                row[c.ColumnName] = dt3.Rows[i][c];
    }
    

    If you run that code with the setup code:

    var ds = new DataSet();
    
    var dt = ds.Tables["Total Amount of Orders"] ?? new DataTable();
    dt.Columns.Add("A");
    dt.Rows.Add("A1");
    dt.Rows.Add("A2");
    
    var dt2 = ds.Tables["Total Value of Orders"]?? new DataTable();
    dt2.Columns.Add("B");
    dt2.Rows.Add("B1");
    dt2.Rows.Add("B2");
    
    var dt3 = ds.Tables["Total Value of Orders Not Invoiced"]?? new DataTable();
    dt3.Columns.Add("C");
    dt3.Rows.Add("C1");
    dt3.Rows.Add("C2");
    

    You get

    enter image description here

    If you use Merge then you get

    enter image description here