Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;


namespace Petrol_Billing_System1
{
    public partial class frmDisplayCreditSales : Form
    {
        public frmDisplayCreditSales()
        {
            InitializeComponent();
        }

        SqlConnection con = new SqlConnection(Properties.Settings.Default.dbconnection);
        DataSet ds = new DataSet();
       

        private void btnNew_Click(object sender, EventArgs e)
        {
            frmCreditSalesDetails frm = new frmCreditSalesDetails();
            frm.Show();
        }

        private void frmDisplayCreditSales_Load(object sender, EventArgs e)
        {
            dtpdisCreditSale.Value = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
            dtpdisCreditSale.CustomFormat = "dd MMMM,yyyy";
            dtpdisCreditSale.Format = DateTimePickerFormat.Custom;

            dtpFromDate.CustomFormat = "dd MMMM,yyyy";
            dtpFromDate.Format = DateTimePickerFormat.Custom;

            dtpToDate.CustomFormat = "dd MMMM,yyyy";
            dtpToDate.Format = DateTimePickerFormat.Custom;

            SqlDataAdapter daitems = new SqlDataAdapter("Select ItemID,ItemName from Items", con);
            daitems.Fill(ds, "items");

            SqlDataAdapter dacust = new SqlDataAdapter("Select CustomerID,CustomerName from Customers", con);
            dacust.Fill(ds,"cust");
                            
            SqlDataAdapter dadt = new SqlDataAdapter("select s.SalesID,c.CustomerID,chd.SalesID,chd.ItemID,ch.ChallanID,ch.Date,s.FromDate,s.ToDate,chd.ItemName,chd.Description,chd.Rate,chd.Qty,(chd.Rate*chd.Qty) as Total from Challans ch inner join ChallanDetails chd on chd.ChallanID=ch.ChallanID inner join  Sales s on s.CustomerID=ch.CustomerID inner join  Customers c on c.CustomerID=s.CustomerID", con);
            dadt.Fill(ds, "dt");

            bndSoDisCrSale.DataSource = ds;
            bndSoDisCrSale.DataMember = "dt";

            cmbCustomerName.DisplayMember = "CustomerName";
            cmbCustomerName.ValueMember = "CustomerID";
            cmbCustomerName.DataSource = ds.Tables["cust"];

           
           txtInvoiceNo.DataBindings.Add("Text", bndSoDisCrSale, "SalesID");
           dtpdisCreditSale.DataBindings.Add("Value", bndSoDisCrSale, "Date");
           cmbCustomerName.DataBindings.Add("SelectedValue", bndSoDisCrSale,"CustomerID");
           dtpFromDate.DataBindings.Add("Value", bndSoDisCrSale, "FromDate");
           dtpToDate.DataBindings.Add("Value", bndSoDisCrSale, "ToDate");


           string sql = string.Format("select ch.Date,ch.ChallanID,chd.ItemName,chd.Description,chd.Rate,chd.Qty,(chd.Rate*chd.Qty) as Total  from ChallanDetails chd inner join challans ch on  ch.ChallanID=chd.ChallanID where chd.SalesID='{0}'", txtInvoiceNo.Text);
           SqlDataAdapter da = new SqlDataAdapter(sql, con);
           da.Fill(ds, "gd");

           grddiscrsale.DataSource = ds;
           grddiscrsale.DataMember = "gd";

          
          ds.Tables["gd"].RowChanged += new DataRowChangeEventHandler(frmDisplayCreditSales_RowChanged);
          
          int total = 0;
          for (int i = 0; i <= grddiscrsale.Rows.Count - 2; i++)
          {
              total += int.Parse(grddiscrsale.Rows[i].Cells["Total"].Value.ToString());
          }

          txtTotalPrice.Text = total.ToString();
 

        }

                    
         void frmDisplayCreditSales_RowChanged(object sender, DataRowChangeEventArgs e)
        {
            decimal Total = 0;

            foreach (DataRow row in ds.Tables["gd"].Rows)
            {

                Total += decimal.Parse(row["Total"].ToString());
            }
            txtTotalPrice.Text = Total.ToString();
        }

        
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            string sql = string.Format("Select SalesID from ChallanDetails where SalesID='{0}'",txtInvoiceNo.Text);
            SqlCommand cmd = new SqlCommand(sql, con);
            SqlDataReader sdr;
            con.Open();
            sdr = cmd.ExecuteReader();
            sdr.Read();
            string salesID = sdr["SalesID"].ToString();
            sdr.Close();
            con.Close();


            frmEditCreditSalesDetails frm= new frmEditCreditSalesDetails(txtInvoiceNo.Text,this);
            frm.Show();
        }

        private void bndnavdiscrsale_RefreshItems(object sender, EventArgs e)
        {
            int total = 0;
            for (int i = 0; i <= grddiscrsale.Rows.Count - 2; i++)
            {
                total += int.Parse(grddiscrsale.Rows[i].Cells["Total"].Value.ToString());

            }

          txtTotalPrice.Text = total.ToString();    
     
          
       }
Posted
Comments
BulletVictim 30-Jan-14 2:37am    
A bit of a description of what is going on here?

Something you can try.
In future please only show the code of where the problem is occurring.
bool filled = false;
 private void btnNew_Click(object sender, EventArgs e)
        {
            frmCreditSalesDetails frm = new frmCreditSalesDetails();
            frm.Show();
        }
 
        private void frmDisplayCreditSales_Load(object sender, EventArgs e)
        {
            if(filled == false)
            {
              DataGridFill();
            }
        }
        
public static void DataGridFill()
{
dtpdisCreditSale.Value = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
            dtpdisCreditSale.CustomFormat = "dd MMMM,yyyy";
            dtpdisCreditSale.Format = DateTimePickerFormat.Custom;
 
            dtpFromDate.CustomFormat = "dd MMMM,yyyy";
            dtpFromDate.Format = DateTimePickerFormat.Custom;
 
            dtpToDate.CustomFormat = "dd MMMM,yyyy";
            dtpToDate.Format = DateTimePickerFormat.Custom;
 
            SqlDataAdapter daitems = new SqlDataAdapter("Select ItemID,ItemName from Items", con);
            daitems.Fill(ds, "items");
 
            SqlDataAdapter dacust = new SqlDataAdapter("Select CustomerID,CustomerName from Customers", con);
            dacust.Fill(ds,"cust");
                            
            SqlDataAdapter dadt = new SqlDataAdapter("select s.SalesID,c.CustomerID,chd.SalesID,chd.ItemID,ch.ChallanID,ch.Date,s.FromDate,s.ToDate,chd.ItemName,chd.Description,chd.Rate,chd.Qty,(chd.Rate*chd.Qty) as Total from Challans ch inner join ChallanDetails chd on chd.ChallanID=ch.ChallanID inner join  Sales s on s.CustomerID=ch.CustomerID inner join  Customers c on c.CustomerID=s.CustomerID", con);
            dadt.Fill(ds, "dt");
 
            bndSoDisCrSale.DataSource = ds;
            bndSoDisCrSale.DataMember = "dt";
 
            cmbCustomerName.DisplayMember = "CustomerName";
            cmbCustomerName.ValueMember = "CustomerID";
            cmbCustomerName.DataSource = ds.Tables["cust"];
 
           
           txtInvoiceNo.DataBindings.Add("Text", bndSoDisCrSale, "SalesID");
           dtpdisCreditSale.DataBindings.Add("Value", bndSoDisCrSale, "Date");
           cmbCustomerName.DataBindings.Add("SelectedValue", bndSoDisCrSale,"CustomerID");
           dtpFromDate.DataBindings.Add("Value", bndSoDisCrSale, "FromDate");
           dtpToDate.DataBindings.Add("Value", bndSoDisCrSale, "ToDate");
 

           string sql = string.Format("select ch.Date,ch.ChallanID,chd.ItemName,chd.Description,chd.Rate,chd.Qty,(chd.Rate*chd.Qty) as Total  from ChallanDetails chd inner join challans ch on  ch.ChallanID=chd.ChallanID where chd.SalesID='{0}'", txtInvoiceNo.Text);
           SqlDataAdapter da = new SqlDataAdapter(sql, con);
           da.Fill(ds, "gd");
 
           grddiscrsale.DataSource = ds;
           grddiscrsale.DataMember = "gd";
 
          
          ds.Tables["gd"].RowChanged += new DataRowChangeEventHandler(frmDisplayCreditSales_RowChanged);
          
          int total = 0;
          for (int i = 0; i <= grddiscrsale.Rows.Count - 2; i++)
          {
              total += int.Parse(grddiscrsale.Rows[i].Cells["Total"].Value.ToString());
          }
 
          txtTotalPrice.Text = total.ToString();
 filled = true;
}
 
Share this answer
 
Hi,

U Can Bind the Data in the Page Load

private void frmDisplayCreditSales_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
DataGridFill();
}
}


Public Void DataGridFill()
{

Bind Data to DataGrid Here....
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900