【问题标题】:Can i create reports and print DataGridView data in c#?我可以在 C# 中创建报告并打印 DataGridView 数据吗?
【发布时间】:2018-08-23 19:46:12
【问题描述】:

我有这段代码,用户需要在其中为客户创建价格优惠,用户通过输入产品名称在数据库中搜索产品,并在 DataGridView 中获取所有匹配结果,然后选择他想要的产品包含在报价中并添加他想要计算价格的金额,结果转到另一个 DataGridView,我用来保存报价所需的所有产品的数据,然后我需要打印报价但我找不到从第二个 DataGridView 创建报告的方法,因为它没有连接到数据集,有没有办法可以做到这一点。这是我的整个过程的完美代码,我只需要添加报告部分。

   //here i created a Function to retrieve data from Sql Table into DataGridView
     private void Display_SPOdata()
          {
              con.Open();
              SqlCommand cmd = con.CreateCommand();
              cmd.CommandType = CommandType.Text;
              cmd.CommandText = "Select Prod_Name, Date_of_Prod, Date_of_Exp, Unit_Type, Max_Unit_Sell_Price, Min_Unit_Sell_Price, Total_Amount from [Product_Amount] Where Prod_Name = N'"+SellPOproductNameTB.Text+"' and Total_Amount <>'0'";
              cmd.ExecuteNonQuery();
              DataTable dt = new DataTable();
              SqlDataAdapter da = new SqlDataAdapter(cmd);
              da.Fill(dt);
              SellPOdatagrid1.DataSource = dt;
              con.Close();

          }
    //here i called the previous function to run when the user presses the search button
          private void button62_Click(object sender, EventArgs e)
          {
              Display_SPOdata();
          }

private void SellPOdatagrid1_CellClick(object sender, DataGridViewCellEventArgs e)
          {//here i assigned Varubles to the Keep the entities i need when the cell is clicked  to send to the next dataGradeView
              indexRow = e.RowIndex;


              DataGridViewRow row = SellPOdatagrid1.Rows[indexRow];

              productname = row.Cells[0].Value.ToString();
              dateofprod = row.Cells[1].Value.ToString();
              dateofexp = row.Cells[2].Value.ToString();
              unittype = row.Cells[3].Value.ToString();
              minsp = row.Cells[5].Value.ToString();

          }    
//here the chosen product goes to the Second DataGridView after entering the Quantity to make the calculations
          private void SellPOaddBTN_Click(object sender, EventArgs e)
          {
              int totalPrice = 0;
              int row1 = 0;
              SellPOdataGrid2.Rows.Add();
              row1 = SellPOdataGrid2.Rows.Count - 2;
              SellPOdataGrid2["SPO_ProductName", row1].Value = productname;
              SellPOdataGrid2["SPO_DateOfProduction",row1].Value = dateofprod;
                  SellPOdataGrid2["SPO_DateOfExpiry",row1].Value = dateofexp;
                  SellPOdataGrid2["SPO_UnitType",row1].Value = unittype;
                  SellPOdataGrid2["SPO_Price",row1].Value = minsp;
                  SellPOdataGrid2["SPO_Quantity",row1].Value = SellPOquantityTB.Text;
                  SellPOdataGrid2["SPO_TotalPrice", row1].Value = Convert.ToInt32(SellPOquantityTB.Text) * Convert.ToInt32(minsp);
                  foreach (DataGridViewRow row2 in SellPOdataGrid2.Rows)
                  {
                      totalPrice += Convert.ToInt32(row2.Cells["SPO_TotalPrice"].Value);
                  }
                  SellPOtotalPriceTB.Text = totalPrice.ToString();
          }

我需要将 SPOdataGridView2 数据放入报告中。

【问题讨论】:

  • 您对数据组织的描述很好;执行此操作的代码也很好,但是,就数据的“打印”而言,您尝试过什么?我猜你可能想仔细阅读PrintDocument Class 作为一个可能的开始。
  • 非常感谢。我没有做任何关于打印的事情,因为我不知道如何开始,因为我找到的所有答案都不适合我的代码。
  • 尝试一下。我对打印文档类的评论是一个好的开始。
  • PrintDocument 类并不难。如果有多个页面,就会出现大多数问题。此时忘记多页,专注于打印一页。因此,如果不付出一些努力,就无法为您编写代码。再次......尝试一些东西并发布没有按预期工作的内容。
  • 好的,我试试看结果告诉你

标签: c# printing datagridview report


【解决方案1】:

在这里 (http://csharp.net-informations.com/datagridview/csharp-datagridview-printing.htm),您将了解 C# 中的打印工作原理。您将熟悉 C# 中的打印文档。这里提到了一种动态方法。 https://www.codeproject.com/Articles/28046/Printing-of-DataGridView

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-27
    相关资源
    最近更新 更多