0
votes

I have a datagridview that is dropped in from the toolbox (VS2015). I need to select identical rows based on the Sales Order column and send an email:Datagridview with similar columns

So; if I select a row based on the unique identifier (Line Item), I would like to find all rows that have the same Sales Order number and email the information in those rows. As you can see, there can be one Row with no matching Sales Order or there can be several. What I sould like is a suggestion on code to select the multiple (similar) rows and suggestion on how to add all that information in email. I have some code snippets to do one single selection:

 private void salesOrdersDataGridView_CellContentClick(object sender, 
 DataGridViewCellEventArgs e)
    {

        if (e.RowIndex > -1)
        {
            DataGridViewRow row = salesOrdersDataGridView.Rows[e.RowIndex];
            string cpn = "";
            cpn = salesOrdersDataGridView.Rows[e.RowIndex].Cells["cpn"].Value.ToString();
            lblCpn.Text = cpn;
            lblCpn.Visible = true;
            string ordnum = "";
            ordnum = salesOrdersDataGridView.Rows[e.RowIndex].Cells["SalesOrder"].Value.ToString();
            lblOrder.Text = ordnum;
            lblOrder.Visible = true;
            string pnum = "";
            pnum = salesOrdersDataGridView.Rows[e.RowIndex].Cells["partno"].Value.ToString();
            lblPN.Text = pnum;
            lblPN.Visible = true;
            lblPN.ForeColor = Color.FromArgb(0, 192, 0);
            string desc = "";
            desc = salesOrdersDataGridView.Rows[e.RowIndex].Cells["descript"].Value.ToString();
            lblDesc.Text = desc;
            lblDesc.Visible = true;
            string quant = "";
            quant = salesOrdersDataGridView.Rows[e.RowIndex].Cells["qty"].Value.ToString();
            lblQty.Text = quant;
            lblQty.Visible = true;
            string cpo = "";
            cpo = salesOrdersDataGridView.Rows[e.RowIndex].Cells["cpo"].Value.ToString();
            lblPO.Text = cpo;
            lblPO.Visible = true;
            string rev = "";
            rev = salesOrdersDataGridView.Rows[e.RowIndex].Cells["crev"].Value.ToString();
            lblRev.Text = rev;
            lblRev.Visible = true;
            lblRev.ForeColor = Color.FromArgb(0, 192, 0);
            DateTime shp = DateTime.Parse(salesOrdersDataGridView.Rows[e.RowIndex].Cells["shipDate"].Value.ToString());
            lblshp.Text = shp.ToString("MM/dd/yyyy");
            lblshp.Visible = true;
            string eml = "";
            eml = salesOrdersDataGridView.Rows[e.RowIndex].Cells["email"].Value.ToString();
            lblEmail.Text = eml;
            lblEmail.Visible = true;

As you can see, I am writing to labels (this is so the user can verify the information. I then add that to email (Outlook):

 private void btnAck_Click(object sender, EventArgs e)
    {
  try
        {


            //Must add Outlook Reference Object Library


            Outlook.Application _app = new Outlook.Application();
            Outlook.MailItem mail = (Outlook.MailItem)_app.CreateItem(Outlook.OlItemType.olMailItem);

            mail.To = lblEmail.Text;
            mail.BCC = ";[email protected]";
            mail.Subject = "Order Acknowledgement for your PO " + lblPO.Text;
            mail.BodyFormat = Outlook.OlBodyFormat.olFormatHTML;
            mail.HTMLBody = "<HTML><BODY>";
            mail.HTMLBody += "Thank you for your order!" + "<br />" + "<br />" + "Touchstone Order Number: " + lblOrder.Text +
            "<br />" + " Part Number: " + lblPN.Text + "<br />" + "Description:" + lblDesc.Text + "<br />" + "Customer Part Number: " + "  " +
            lblCpn.Text + "-" + " Revision: " + lblRev.Text + "<br />" + "Expected Ship Date: " + lblshp.Text +
            "<br />" + "Quantity: " + lblQty.Text + "<br />" + "<br />" + "Regards," + "<br />" +
            "<br />" + "Customer Service" + "<br />" + "Touchstone Technology, Inc." + "<br />" + "address." +
            "<br />" + "more address" + "<br />" + "Phone: phone number";

            mail.HTMLBody += "<p>Web: <a href='http://www.web'>web</a></p></body></html>";
            mail.Display(true);
           // mail.Send();

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

I stripped out what I though was not necessary - please feel free to ask any questions as I am not sure how to clarify what I need. Thanks for the usual quick responses and help

1
Please use Interpolated strings. (in c#6 it's much clearer to read) - SᴇM

1 Answers

0
votes

I was able to get what I needed by just doing a direct query (not writing to a datagridview. Hope this code sample helps someone else:

 string ord = "";
        string pn = "";
        string des = "";
        string cpn = "";
        string rev = "";
        string esd = "";
        string qty = "";
        string eml = "";
        string sbj = "";

        string str = @"Data Source=my data source;Initial Catalog=my table;Integrated Security=True";
        SqlConnection scn;
        SqlDataAdapter da;
        DataSet ds;


        salesOrdersTableAdapter.SO(_TST_TWIDataSet.SalesOrders);


        scn = new SqlConnection(str);
        da = new SqlDataAdapter("SELECT DISTINCT DATEADD (dd, DATEDIFF(dd,0,ShipDate),0) AS ShipDate,RTRIM(SalesOrder) AS [Sales Order], RTRIM(PartNum) AS [Part Number]," +
            "RTRIM(Description) AS Description,RTRIM(DueQty) AS Quantity,RTRIM(CustPartNum) AS[Customer Part No], RTRIM(CustPo) AS[Customer PO], " +
                                 "RTRIM(CustRev) AS[Customer Rev], RTRIM(email) AS [Email] " +
                                 "FROM SalesOrders WHERE Ack <> 'Y'AND SalesOrder =" + MyGlobals.ord, scn);
        ds = new DataSet();da.Fill(ds, "SalesOrders");
        foreach(DataRow Row in ds.Tables["SalesOrders"].Rows)
        {
            ord = ord + " Order Number "+ Row["Sales Order"];
            pn = pn + " Part Number: " + Row["Part Number"];
            des = des + "Description: " + Row["Description"];
            cpn = cpn + "Customer Part Number: " + Row["Customer Part No"];
            rev = rev + "Customer Revision: " + Row["Customer Rev"];
            DateTime dte = DateTime.Now;               
            esd = esd + "Expected Ship Date: " + dte.ToShortDateString();
            qty = qty + "Quantity: " + Row["Quantity"];
            eml = eml +  Row["Email"];
            sbj = sbj + "Order Acknowledgement for your PO " + Row["Customer PO"];
        }

Then I just plug the variables in my email code