如何从网格视图中仅打印指定的行

  • 本文关键字:打印 网格 视图 c#
  • 更新时间 :
  • 英文 :


欢迎我有一个带有发票的销售屏幕,该发票显示在带有Extra Robert报告的网格视图中我只想当我使用选择框选择一些行时,只有所选的行会打印在一页上。我尝试过这个代码,但它会在单独的报告中打印每一行我只想在一页上打印选定的行。谢谢

for (int i = 0; i <= DgvSale.Rows.Count - 1; i++)
{
if (Convert.ToBoolean(DgvSale.Rows[i].Cells[8].Value) == true)
{
tblRpt1 = db.readData("SELECT [Order_ID] as 'رقم الفاتورة',talb_ID as 'طلب',[Cust_Name] as 'اسم العميل',Products.Pro_Name as 'المنتج',Products.Group_ID as 'قسم',Products_Group.Group_Name as 'اسم القسم',[Sales_Detalis].[Qty] as 'الكمية',[Price] as 'السعر',[User_Name] as 'الكاشير',[Date] as 'التاريخ',[Unit] as 'الوحدة',[Sales_Detalis].Tax_Value as 'الضريبة',Price_Tax as 'السعر بعد الضريبة',[typetalab] as 'نوع الطلب',[priceservic] as 'خدمة',[shiftname] as 'شيفت',notes as 'ملاحظات',shiftnum as 'شيفت رقم',Print_Group.Print_Name,Print_Group.Name_Group  FROM [dbo].[Sales_Detalis] , Products,Products_Group,Print_Group where Products.Pro_ID = Sales_Detalis.Pro_ID and Products.Pro_ID= " + row1.Cells[0].Value + " and Order_ID = " + id + "  and Products.Group_ID = Products_Group.Group_ID and Print_Group.Name_Group=Products.Group_ID  ORDER BY Order_ID ASC", "");
}
}
if (Properties.Settings.Default.SalePrintKind == "8CM")
{
devOrderSales2 rpt = new devOrderSales2() { DataSource = tblRpt1, DataMember = "OrderSales2" };
rpt.DataSource = tblRpt1;
rpt.Parameters["ID1"].Value = id;
rpt.PrinterName = Properties.Settings.Default.PrinterNameSteker;
rpt.xrTableCell8.Visible=true;
if (Properties.Settings.Default.saleprintauto == "0")
{
rpt.Print();
}
else if (Properties.Settings.Default.saleprintauto == "1")
{
rpt.ShowPreviewDialog();
}
}

我建议使用所选记录动态创建WHERE条件:

string sql = @"SELECT [Order_ID] as 'رقم الفاتورة',talb_ID as 'طلب',[Cust_Name] as 'اسم العميل',Products.Pro_Name as 'المنتج',Products.Group_ID as 'قسم',Products_Group.Group_Name as 'اسم القسم',[Sales_Detalis].[Qty] as 'الكمية',[Price] as 'السعر',[User_Name] as 'الكاشير',[Date] as 'التاريخ',[Unit] as 'الوحدة',[Sales_Detalis].Tax_Value as 'الضريبة',Price_Tax as 'السعر بعد الضريبة',[typetalab] as 'نوع الطلب',[priceservic] as 'خدمة',[shiftname] as 'شيفت',notes as 'ملاحظات',shiftnum as 'شيفت رقم',Print_Group.Print_Name,Print_Group.Name_Group
FROM
[dbo].[Sales_Detalis]
INNER JOIN Products
ON Products.Pro_ID = Sales_Detalis.Pro_ID
INNER JOIN Products_Group
ON Products.Group_ID = Products_Group.Group_ID
INNER JOIN Print_Group
ON Print_Group.Name_Group=Products.Group_ID
WHERE
Products.Pro_ID IN ({0}) AND Order_ID = {1}
ORDER BY Order_ID ASC";
var productIds = new List<int>();
for (int i = 0; i <= DgvSale.Rows.Count - 1; i++) {
if (Convert.ToBoolean(DgvSale.Rows[i].Cells[8].Value) == true) {
productIds.Add(Convert.ToInt32(DgvSale.Rows[i].Cells[0].Value));
}
}
if (Properties.Settings.Default.SalePrintKind == "8CM" && productIds.Count > 0) {
string commandText = String.Format(sql, String.Join(", ", productIds), id);
tblRpt1 = db.readData(commandText, "");
var rpt = new devOrderSales2 {
DataSource = tblRpt1,
DataMember = "OrderSales2",
PrinterName = Properties.Settings.Default.PrinterNameSteker
};
rpt.xrTableCell8.Visible = true;
rpt.Parameters["ID1"].Value = id;
if (Properties.Settings.Default.saleprintauto == "0") {
rpt.Print();
} else if (Properties.Settings.Default.saleprintauto == "1") {
rpt.ShowPreviewDialog();
}
}

此外,您应该使用现代的JOIN语法,而不是WHERE子句中过时的联接条件。

请注意,我已经设置了一个SQL字符串,其中{0}{1}作为占位符,稍后将用String.Format方法替换。我假设产品ID的类型为int。由此产生的WHERE条件如下所示:

WHERE
Products.Pro_ID IN (12, 30, 55, 68) AND Order_ID = 17

@"引入的C#逐字逐句字符串允许在字符串中包含换行符,并使SQL文本更具可读性。

row1总是指向同一行。在循环中使用DgvSale.Rows[i]动态获取行。

一种方法是首先从行中收集所有相关的ID,并且只进行1db查询,例如:

var toprint_ids = new List<long>();
for (int i = 0; i <= DgvSale.Rows.Count - 1; i++)
{
if (Convert.ToBoolean(DgvSale.Rows[i].Cells[8].Value) == true)
{
var itm_id = Convert.ToInt64(row1.Cells[0].Value);
ids.Add(itm_id);
}
}
if (toprint_ids.Count == 0)
return;
tblRpt1 = db.readData("SELECT [Order_ID] as 'رقم الفاتورة',talb_ID as 'طلب',[Cust_Name] as 'اسم العميل',Products.Pro_Name as 'المنتج',Products.Group_ID as 'قسم',Products_Group.Group_Name as 'اسم القسم',[Sales_Detalis].[Qty] as 'الكمية',[Price] as 'السعر',[User_Name] as 'الكاشير',[Date] as 'التاريخ',[Unit] as 'الوحدة',[Sales_Detalis].Tax_Value as 'الضريبة',Price_Tax as 'السعر بعد الضريبة',[typetalab] as 'نوع الطلب',[priceservic] as 'خدمة',[shiftname] as 'شيفت',notes as 'ملاحظات',shiftnum as 'شيفت رقم',Print_Group.Print_Name,Print_Group.Name_Group  FROM [dbo].[Sales_Detalis] , Products,Products_Group,Print_Group where Products.Pro_ID = Sales_Detalis.Pro_ID and Products.Pro_ID in (" + string.Join(",", toprint_ids) + ") and Order_ID = " + id + "  and Products.Group_ID = Products_Group.Group_ID and Print_Group.Name_Group=Products.Group_ID  ORDER BY Order_ID ASC", "");

if (Properties.Settings.Default.SalePrintKind == "8CM")
{
devOrderSales2 rpt = new devOrderSales2() { DataSource = tblRpt1, DataMember = "OrderSales2" };
rpt.DataSource = tblRpt1;
rpt.Parameters["ID1"].Value = id;
rpt.PrinterName = Properties.Settings.Default.PrinterNameSteker;
rpt.xrTableCell8.Visible=true;
if (Properties.Settings.Default.saleprintauto == "0")
{
rpt.Print();
}
else if (Properties.Settings.Default.saleprintauto == "1")
{
rpt.ShowPreviewDialog();
}
}

请注意查询中的更改:in (" + string.Join(",", toprint_ids) + ")此外,如果您的行(例如Guids)具有非数字ID,则需要将列表更改为List<string>,并将查询更改为类似in ('" + string.Join("','", toprint_ids) + "')

最新更新