0
votes

I have a datagridview, and I am populating it from a datatable which in turn retrieves it's data from a webservice query (against SalesForce cloud system).

Essentially we want to show the results of attempting to remove attachments from SalesForce cases which have been put on there by the users, currently we query a SalesForce XML webservice called case and we want to add the capability to also query a second new SalesForce object called credit case.

It has been working fine getting its data from an object called cases, and displaying them in a Windows Forms datagridview control.

Now we want to add another object (let's call it creditCases), so I have the query all setup, have added another task object with the fields and datatypes and so on.

Once the dataset is populated we set the data source of the datagridview using gvTaskCases.DataSource = dtCases;

But of course I now have a second datasource (with a different number of columns) that I want to add to the table.

If I do this with my new datatable (gvTaskCases.DataSource = dtcreditCases) how do I stop it replacing the data from the existing datatable?

CODE:

gvTaskCases.DataSource = null;

        dtCases.Rows.Clear();

        foreach (task_cases item in cases)
        {
            DataRow drCases = dtCases.NewRow();

            // Then add the new row to the collection.
            drCases["Case ID"] = item.c_Id;
            drCases["Case Number"] = item.c_Number;
            drCases["Case Topic"] = item.c_Topic;
            drCases["Case SubTopic"] = item.c_Subtopic;
            drCases["Account Number"] = item.c_CustomerNumber;
            drCases["Additional Info"] = item.c_AdditionalInfo;
            drCases["Closed Date"] = Convert.ToDateTime(item.c_ClosedDate).ToString("dd/MM/yyyy");
            drCases["Attachment"] = item.c_Attachment;
            drCases["Content Type"] = item.c_ContentType;
            drCases["Detach Status"] = item.c_Status;
            drCases["Document Type"] = item.c_DocumentType;
            drCases["Imaging Directory"] = item.c_ImagingDSXDirectory;
            drCases["Imaging Document"] = item.c_ImagingDocument;

            dtCases.Rows.Add(drCases);
        }

        gvTaskCases.DataSource = dtCases;


        gvTaskCases.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing;
        gvTaskCases.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;

        foreach (task_creditcases item in creditCases)
            {
            DataRow drCases = dtCases.NewRow();

            // Then add the new row to the collection.
            drCases["Case ID"] = item.c_Id;
            drCases["Case Number"] = item.c_Name; // SalesForece Credit Case Object CASE NUMBER;
            drCases["Account Number"] = item.c_Account__c;
            drCases["Closed Date"] = Convert.ToDateTime(item.c_Closed_Date__c).ToString("dd/MM/yyyy");
            drCases["Attachment"] = item.c_Imaging_Document_Attached__c;
            drCases["Detach Status"] = item.c_Status__c;
            drCases["Document Type"] = item.c_Document_Type__c;
            drCases["Imaging Directory"] = item.c_Directories__c;
            drCases["Imaging Document"] = item.c_Imaging_Document;

            dtCases.Rows.Add(drCases);
            }

        // add the new records:

        // Adjust size & hide columns that aren't needed
        gvTaskCases.Columns[0].Visible = false;
        gvTaskCases.Columns[11].Visible = false;
        gvTaskCases.Columns[12].Visible = false;
        gvTaskCases.Columns[13].Visible = false;
1
you want to show both the old data and new data in the same grid at the same time? What about the structure of your new data? - King King
well, it's very similar, just may have to add a couple of blank or empty columns in my datatable ... - Our Man in Bananas
@KingKing: I have edited my question to give more clarification, and added the code block, towards the end where I hide some columns is where I want to add the second datatable - Our Man in Bananas
I guess an alternative might be to reuse the existing datatable dtcases but I wanted to keep them seperate if possible - Our Man in Bananas
not clear what you want, if you want to keep the old DataTable, possibly you have to keep some reference to it and create a new DataTable from both the old and new DataTable, of course that will change the meaning of reuse. - King King

1 Answers

1
votes

You can use the Merge method of the DataTable.

 ((DataTable) gvTaskCases.DataSource).Merge(dtcreditCases);

Another approach is just keep your old DataTable, you have to use a little LINQ to merge the dataTables like this:

gvTaskCases.DataSource = (dtCases.Columns.Count > dtcreditCases.Columns.Count ?
                          dtCases.AsEnumerable().Concat(dtcreditCases.AsEnumerable()) :
                          dtcreditCases.AsEnumerable().Concat(dtCases.AsEnumerable())).CopyToDataTable();