locked
Multiple row selection & Send Email Code ? Need littel Help RRS feed

  • Question

  • Dear All,

    I managed to insert a code which will enable me to select multiple rows (Artikel from the great Yann Duran), i also have a code which enables me to send a mail for each selected row (only one row).

    But now since I was able to select more rows, I need to to customize my "Send mail" code to send mail to all selected rows. I guess I will need a loop here but I am not sure.

    My code for multiy row selection :

            private const string _CONTROL = "vw_CustLedgerEntry" // This is my Data Grid;
            private DataGrid _ItemsList = null;
            private int _SelectedRowsCount = 0;
    
            partial void Part_2_CustomerItemDetail_InitializeDataWorkspace(List<IDataService> saveChangesTo)
            {
                // Write your code here.
                this.FindControl(_CONTROL).ControlAvailable += Orders_ControlAvailable;
            }
            private void Orders_ControlAvailable(object sender, ControlAvailableEventArgs e)
            {
                _ItemsList = e.Control as DataGrid;
                //if the cast failed, just leave, there's nothing more we can do here 
                if (_ItemsList == null)
                {
                    return;
                }
                //set the property on the grid that allows multiple selection 
                _ItemsList.SelectionMode = DataGridSelectionMode.Extended;
                _ItemsList.SelectionChanged += new SelectionChangedEventHandler(_ItemsList_SelectionChanged);
            }
            private void _ItemsList_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                switch (_ItemsList == null)
                {
                    case true:
                        _SelectedRowsCount = 0;
    
                        break;
                    case false:
                        _SelectedRowsCount = _ItemsList.SelectedItems.Count;
                        break;
                }
            }

    This is my code for sending a mail for one selected row:

     partial void EMail_Execute()                // EMAIL BUTTON FOR OPEN ENTRIES
            {
    
                if (vw_CustLedgerEntry.SelectedItem.Report_Type == null)
                {
                    // throw new ArgumentNullException();
                    this.ShowMessageBox("EMail Can't be sent,PDF generation is not possible for this Entry");
                    this.Application.ShowPart_2_CustomerItemDetail(this.vw_CustomerItem.Costomer_No_, this.vw_CustomerItem.Company);
                }
                else// if  (vw_CustLedgerEntry.SelectedItem.Send_Mail== true) 
                    //do
                    {
                        vw_CustLedgerEntryItem1 entryItem = this.vw_CustLedgerEntry.SelectedItem;
                        InvSendbyMailRequestBody reqBody = new InvSendbyMailRequestBody(
                              entryItem.Document_No_
                            , entryItem.Report_Type
                            , "DynNavHRS"
                            , ""
                            , this.Application.User.Name.Replace(@"HRS\", "") + "@hrs.com" // HRS001
                            , "Document"
                            , false
                            , false
                            , this.vw_CustomerItem.ISO_Code                         // Change 7.8.2014 Bug in Email body text sprache
                            , this.vw_CustomerItem.Salesperson_E_mail               // Change 7.8.2014 Bug in Send E-mail
                            , entryItem.Customer_No_.ToString()
                            , false
                            , ""
                            , ""
                            , "XYZ"
                            , false);
                        InvSendbyMailRequest req = new InvSendbyMailRequest(reqBody);
                        HRSReportServiceSoapClient wsHRS = new HRSReportServiceSoapClient();
                        //            wsHRS.InvSendbyMailCompleted += new EventHandler<InvSendbyMailCompletedEventArgs>(wsHRS_InvGetPDFCompleted);
                        wsHRS.InvSendbyMailAsync(req);
    
                        this.ShowMessageBox("Your email was successfully sent");
                    }
    
                   // while (vw_CustLedgerEntry.SelectedItem.Send_Mail == true);
    
    
            }

    Thaks a lot for your help.

    Zayed






    Thursday, March 12, 2015 7:41 AM

Answers

  • Should be like:

        //loop through the selected rows  
                //we're casting each selected row as a DemoItem 
                //so we get access to all the properties of the entity that the row represents 
                foreach (var item in _ItemsList.SelectedItems)
                { 
    

    C# reference is here

    Dave


    Dave Baker | AIDE for LightSwitch | Xpert360 blog | twitter : @xpert360 | Xpert360 website | Opinions are my own. For better forums, remember to mark posts as helpful/answer.

    • Marked as answer by Consultant_606 Thursday, March 12, 2015 10:41 AM
    Thursday, March 12, 2015 10:30 AM

All replies

  • I tried the following but I donot know if its correct and Iam facing a problem with the "Foreach" function.
            private const string _CONTROL = "vw_CustLedgerEntry" // This is my Data Grid;
            private DataGrid _ItemsList = null;
            private int _SelectedRowsCount = 0;
    
            partial void Part_2_CustomerItemDetail_InitializeDataWorkspace(List<IDataService> saveChangesTo)
            {
                // Write your code here.
                this.FindControl(_CONTROL).ControlAvailable += Orders_ControlAvailable;
            }
            private void Orders_ControlAvailable(object sender, ControlAvailableEventArgs e)
            {
                _ItemsList = e.Control as DataGrid;
                //if the cast failed, just leave, there's nothing more we can do here 
                if (_ItemsList == null)
                {
                    return;
                }
                //set the property on the grid that allows multiple selection 
                _ItemsList.SelectionMode = DataGridSelectionMode.Extended;
                _ItemsList.SelectionChanged += new SelectionChangedEventHandler(_ItemsList_SelectionChanged);
            }
    
            
            private void _ItemsList_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                switch (_ItemsList == null)
                {
                    case true:
                        _SelectedRowsCount = 0;
    
                        break;
                    case false:
                        _SelectedRowsCount = _ItemsList.SelectedItems.Count;
                        break;
                }
            }
    
            partial void Send_Multiple_Mail_CanExecute(ref bool result)
            {
                //only enable rows have actually been selected 
                result = (_SelectedRowsCount > 0);
    
            }
    
            partial void Send_Multiple_Mail_Execute()
            {
                 
                 if (_ItemsList == null)    { return; } 
                 //StringBuilder names = new StringBuilder(); 
    
                //loop through the selected rows  
                //we're casting each selected row as a DemoItem 
                //so we get access to all the properties of the entity that the row represents 
                foreach (_ItemsList.SelectedItems)
                { 
                        vw_CustLedgerEntryItem1 entryItem = this.vw_CustLedgerEntry.SelectedItem;
                        InvSendbyMailRequestBody reqBody = new InvSendbyMailRequestBody(
                              entryItem.Document_No_
                            , entryItem.Report_Type
                            , "DynNavXYZ"
                            , ""
                            , this.Application.User.Name.Replace(@"HRS\", "") + "@hrs.com" // HRS001
                            , "Document"
                            , false
                            , false
                            , this.vw_CustomerItem.ISO_Code                         // Change 7.8.2014 Bug in Email body text sprache
                            , this.vw_CustomerItem.Salesperson_E_mail               // Change 7.8.2014 Bug in Send E-mail
                            , entryItem.Customer_No_.ToString()
                            , false
                            , ""
                            , ""
                            , "XYZ"
                            , false);
                        InvSendbyMailRequest req = new InvSendbyMailRequest(reqBody);
                        XYZReportServiceSoapClient wsHRS = new HRSReportServiceSoapClient();
                        //            wsHRS.InvSendbyMailCompleted += new EventHandler<InvSendbyMailCompletedEventArgs>(wsHRS_InvGetPDFCompleted);
                        wsHRS.InvSendbyMailAsync(req);
    
                        this.ShowMessageBox("Your email was successfully sent");
                } 
     
    
            }



    Thursday, March 12, 2015 8:46 AM
  • Should be like:

        //loop through the selected rows  
                //we're casting each selected row as a DemoItem 
                //so we get access to all the properties of the entity that the row represents 
                foreach (var item in _ItemsList.SelectedItems)
                { 
    

    C# reference is here

    Dave


    Dave Baker | AIDE for LightSwitch | Xpert360 blog | twitter : @xpert360 | Xpert360 website | Opinions are my own. For better forums, remember to mark posts as helpful/answer.

    • Marked as answer by Consultant_606 Thursday, March 12, 2015 10:41 AM
    Thursday, March 12, 2015 10:30 AM
  • Thanks,

    one more question, for example after I send the E-mail I want to write the Email Status in a new table. Like Sender mail, time, date and so one.

    How can I do this from the Code ? I guess I can create a new table in DB ? but how can I accsess it and write in it a new line from C#? 

    Thanks a lot for your Help,

    Zayed


    Thursday, March 12, 2015 10:43 AM
  • Hi,

    There a couple of ways but this may be the most suitable:

    save only changes of a one entity

    That example uses a separate DataWorkSpace to create new entity objects and save them.

    Dave


    Dave Baker | AIDE for LightSwitch | Xpert360 blog | twitter : @xpert360 | Xpert360 website | Opinions are my own. For better forums, remember to mark posts as helpful/answer.

    Thursday, March 12, 2015 10:50 AM
  • Hi Dave,

    I just wanted to record all information related to the Emails that I sent? isn't
    the best way to do this is to create a table and record my data in it

    with Dataworkspace it seems like the user here are trying to save changes to the
     ame entity? but what is happing with me is that entity itself it’s not changing. I just want to record general info regarding all mails which I sent. Do you agree :) ? 

    Thanks

    Mohamed  



    Thursday, March 12, 2015 11:14 AM
  • That sample shows you how to use a new DataWorkspace to add or change a row (entity) from code.

    Which is what you will do to log the email details to your new table.

    Cheers

    Dave


    Dave Baker | AIDE for LightSwitch | Xpert360 blog | twitter : @xpert360 | Xpert360 website | Opinions are my own. For better forums, remember to mark posts as helpful/answer.

    Thursday, March 12, 2015 2:42 PM
  • Hi Dave,

    I have a nother question regarding sending the mails. would you take a look at this question.

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/4e1daeaf-7a37-4aad-8a0b-6278335640f9/attaching-all-pdfs-in-one-mail-having-only-one-body?forum=lightswitch

    its just that I want to have only one mail for all my attachments,  and according to my loops it send a mail for each selected line. 

    Thanks Dave,

    Mohamed

    Wednesday, March 18, 2015 10:10 AM