简体   繁体   中英

Can i create reports and print DataGridView data in c#?

I have this code in which the user needs to create Price Offers for clients, the user searches the database for products by entering the name of the product and gets all the matching results in a DataGridView and then he chooses the product he wants to include in the price Offer and adds the amount he wants to calculate the price of and the result goes to another DataGridView which i used to keep the data of all the products needed for the offer, i then need to print the offer out but I couldn't find a way to create a Report from the second DataGridView since it's not connected to a dataSet, is there a way I can do that. here is my code for the whole process which works perfect, i just need to add the reporting part.

   //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();
          }

what i need is to put SPOdataGridView2 Data into a Report.

Here ( http://csharp.net-informations.com/datagridview/csharp-datagridview-printing.htm ) you will get to know how the printing works in C#. You will get familiarity with print documents in C#. A dynamic approach is mentioned here. https://www.codeproject.com/Articles/28046/Printing-of-DataGridView

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM