我可以在c#中创建报表并打印DataGridView数据吗?

时间:2018-03-15 05:09:28

标签: c# printing datagridview report

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

   //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数据放入报告中。

1 个答案:

答案 0 :(得分:0)

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