我有一个从工具箱中删除的datagridview(VS2015)。我需要根据“销售订单”列选择相同的行,然后发送电子邮件:
因此;如果我根据唯一标识符(行项目)选择一行,我想查找具有相同销售订单编号的所有行,并通过电子邮件发送这些行中的信息。如您所见,可能有一行没有匹配的销售订单,也可能有几个。我想要的是关于代码的建议,以选择多个(相似)行和关于如何在电子邮件中添加所有信息的建议。我有一些代码片段可以做一个选择:
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;
正如您所看到的,我正在写标签(这样用户可以验证信息。然后我将其添加到电子邮件(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 = ";work@somewhere.com";
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);
}
我剥离了我认为没有必要的东西 - 请随意提出任何问题,因为我不知道如何澄清我需要什么。感谢您通常的快速回复和帮助
答案 0 :(得分:0)
通过直接查询(而不是写入datagridview,我能够得到我需要的东西。希望这段代码示例可以帮助其他人:
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"];
}
然后我只是在我的电子邮件代码中插入变量