Monday 29 July 2013

specific

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.OleDb;
using System.Configuration;
using System.Drawing.Printing;
namespace Milk
{
    public partial class Specific_Supplier : Form
    {
        public Specific_Supplier()
        {
            InitializeComponent();
        }
        OleDbConnection con = new OleDbConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString);
        OleDbCommand cmd;
        OleDbDataAdapter da;
        DataTable dt;
        OleDbDataReader dr;
        public double close;
        private void Specific_Supplier_Load(object sender, EventArgs e)
        {
            label1.Text = Supplier_Details.me.ToString();

            grid();
            grid1();
            double amt = (Convert.ToDouble(label7.Text) - Convert.ToDouble(label9.Text));
            label11.Text = amt.ToString();
            close = Convert.ToDouble(label11.Text);
        }
        public void grid()
        {
            try
            {
                da = new OleDbDataAdapter("select S_Date,Supplier_Qty,Total_Amount From suppurchage where Supplier_Name= '" + label1.Text + "'", con);
                dt = new DataTable();
                da.Fill(dt);
                dataGridView1.DataSource = dt;
                double no = 0;
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    no += Convert.ToDouble(dt.Rows[i][2].ToString());
                }
                label7.Text = Math.Round(no, 2).ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        public void grid1()
        {
            try
            {
                da = new OleDbDataAdapter("select Payment_Date,Amount From spayment where Supplier_Name= '" + label1.Text + "'", con);
                dt = new DataTable();
                da.Fill(dt);
                dataGridView2.DataSource = dt;
                double no2 = 0;
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    no2 += Convert.ToDouble(dt.Rows[i][1].ToString());
                }
                label9.Text = Math.Round(no2, 2).ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        double inwardquan;
        double outwardquan;

        private void button1_Click(object sender, EventArgs e)
        {
            dateTimePicker1.Text = DateTime.Parse(dateTimePicker1.Text).ToString();
            double date1 = dateTimePicker1.Value.ToOADate();
            double date2 = dateTimePicker2.Value.ToOADate();
            double date3 = DateTime.Now.Date.ToOADate();

            label3.Text = (opening1(date1) - opening2(date1)).ToString();
            label5.Text = (opening1(date2) - opening2(date2)).ToString();
            gridshow3(date1, date2);
            gridshow4(date1, date2);
            double nos = Convert.ToDouble(label7.Text) - Convert.ToDouble(label9.Text);
            label11.Text = Math.Round(nos, 2).ToString();
            //if (Convert.ToDouble(label11.Text) > 0)
            //{
            //    label12.Visible = true;
            //}
            //else
            //{
            //    label10.Visible = true;
            //}
        }
        private double opening1(double date)
        {

            try
            {
                string str = "select SUM(Total_Amount) as total from suppurchage where Supplier_Name='" + label1.Text + "' and S_Date<" + date + "";
                cmd = new OleDbCommand(str, con);
                da = new OleDbDataAdapter(str, con);
                dt = new DataTable();
                da.Fill(dt);

                con.Open();
                dr = cmd.ExecuteReader();
                if (dr.Read())
                {
                    inwardquan = Convert.ToDouble(dr[0].ToString());
                }

                con.Close();

            }
            catch (Exception ex)
            {
                con.Close();
                inwardquan = 0;
            }
            return inwardquan;

        }


        private double opening2(double date)
        {

            try
            {
                string str = "select SUM(Amount) as total from spayment where supplier_Name='" + label1.Text + "' and Payment_Date<" + date + "";
                cmd = new OleDbCommand(str, con);
                con.Open();
                dr = cmd.ExecuteReader();
                if (dr.Read())
                {
                    outwardquan = Convert.ToDouble(dr[0].ToString());
                    con.Close();
                }

            }
            catch (Exception ex)
            {
                con.Close();
                outwardquan = 0;
            }
            return outwardquan;
        }
        protected void gridshow3(double d1, double d2)
        {

            try
            {
                string str = "select S_Date,Supplier_Qty,Total_Amount from suppurchage where Supplier_Name='" + label1.Text + "' and S_Date between " + d1 + " and " + d2 + "";
                da = new OleDbDataAdapter(str, con);
                dt = new DataTable();
                da.Fill(dt);
                dataGridView1.DataSource = dt;
                //if (dt.Rows.Count > 0)
                //{
                //    dt3 = Convert.ToDateTime(dt.Rows[0][0].ToString());
                //}
                double no = 0;
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    no += Convert.ToDouble(dt.Rows[i][2].ToString());
                }
                label7.Text = Math.Round(no, 2).ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        protected void gridshow4(double d1, double d2)
        {

            try
            {
                string str = "select Payment_Date,Amount from spayment where Supplier_Name='" + label1.Text + "' and Payment_Date between " + d1 + " and " + d2 + "";
                da = new OleDbDataAdapter(str, con);
                dt = new DataTable();
                da.Fill(dt);
                dataGridView2.DataSource = dt;
                double no = 0;
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    no += Convert.ToDouble(dt.Rows[i][1].ToString());
                }
                label9.Text = Math.Round(no, 2).ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        public int i;
        public int k;
        public int f;
        public int s;
        private void button3_Click(object sender, EventArgs e)
        {
            f = dataGridView1.Rows.Count - 1;
            s = dataGridView2.Rows.Count - 1;
            i = (f >= s) ? f : s;
            k = 0;
            while (k < i)
            {
                PrintDocument doc = new PrintDocument();
                doc.PrintPage += new PrintPageEventHandler(doc_PrintPage);
                PrintDialog dlgSettings = new PrintDialog();
                dlgSettings.Document = doc;
                doc.Print();
            }
        }
        void doc_PrintPage(object sender, PrintPageEventArgs e)
        {
            try
            {
                Font font1 = new Font("Arial", 18);
                Font font = new Font("Arial", 10);
                //float x = e.MarginBounds.Left;
                //float lineSide=font.
                float y = e.MarginBounds.Top;
                float lineHeight = font.GetHeight(e.Graphics);

                //Header
                e.Graphics.DrawString(label1.Text, font1, Brushes.Black, float.Parse("50"), float.Parse("0"));
                y += lineHeight;
                e.Graphics.DrawString("Date:- " + dateTimePicker1.Value.ToString("dd-MM-yyyy") + " To " + dateTimePicker2.Value.ToString("dd-MM-yyyy"), font, Brushes.Black, float.Parse("550"), float.Parse("10"));
                y += lineHeight;
                e.Graphics.DrawString("PURCHASE", font, Brushes.Black, float.Parse("100"), float.Parse("30"));
                y += lineHeight;
                e.Graphics.DrawString("PAYMENT ", font, Brushes.Black, float.Parse("500"), float.Parse("30"));
                y += lineHeight;

                e.Graphics.DrawString("----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------" +
                    "-", font, Brushes.Black, float.Parse("00"), float.Parse("45"));
                y += lineHeight;
                //Header
                e.Graphics.DrawString("DATE", font, Brushes.Black, float.Parse("40"), float.Parse("60"));
                y += lineHeight;
                e.Graphics.DrawString("QUANTITY", font, Brushes.Black, float.Parse("200"), float.Parse("60"));
                y += lineHeight;
                e.Graphics.DrawString("AMOUNT", font, Brushes.Black, float.Parse("360"), float.Parse("60"));
                y += lineHeight;
                e.Graphics.DrawString("DATE", font, Brushes.Black, float.Parse("520"), float.Parse("60"));
                y += lineHeight;
                e.Graphics.DrawString("AMOUNT", font, Brushes.Black, float.Parse("680"), float.Parse("60"));
                y += lineHeight;

                //HERE I NEED TO PUT DATA GRID VIEW WHICH MAY VERY NUMBER OF ROWS EVERY TIME    
                //Body
                e.Graphics.DrawString("---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------", font, Brushes.Black, float.Parse("00"), float.Parse("70"));
                y += lineHeight;

                int j = 80;
                for (int l = k; l < i; l++)
                {
                    if (l < f)
                    {
                        e.Graphics.DrawString(Convert.ToDateTime(dataGridView1.Rows[l].Cells[0].Value).ToString("dd-MM-yy"), font, Brushes.Black, float.Parse("40"), float.Parse(j.ToString()));
                        y += lineHeight;
                        e.Graphics.DrawString(dataGridView1.Rows[l].Cells[1].Value.ToString(), font, Brushes.Black, float.Parse("200"), float.Parse(j.ToString()));
                        y += lineHeight;
                        e.Graphics.DrawString(dataGridView1.Rows[l].Cells[2].Value.ToString(), font, Brushes.Black, float.Parse("360"), float.Parse(j.ToString()));
                        y += lineHeight;
                        e.Graphics.DrawString("|", font, Brushes.Black, float.Parse("450"), float.Parse(j.ToString()));
                        y += lineHeight;
                        int a = j + 12;
                        e.Graphics.DrawString("|", font, Brushes.Black, float.Parse("450"), float.Parse(a.ToString()));
                        y += lineHeight;
                    }

                    if (l < s)
                    {
                        e.Graphics.DrawString("|", font, Brushes.Black, float.Parse("450"), float.Parse(j.ToString()));
                        y += lineHeight;
                        int a = j + 12;
                        e.Graphics.DrawString("|", font, Brushes.Black, float.Parse("450"), float.Parse(a.ToString()));
                        y += lineHeight;
                        e.Graphics.DrawString(Convert.ToDateTime(dataGridView2.Rows[l].Cells[0].Value).ToString("dd-MM-yy"), font, Brushes.Black, float.Parse("520"), float.Parse(j.ToString()));
                        y += lineHeight;
                        e.Graphics.DrawString(dataGridView2.Rows[l].Cells[1].Value.ToString(), font, Brushes.Black, float.Parse("680"), float.Parse(j.ToString()));
                        y += lineHeight;
                    }
                    j = j + 20;
                    k++;
                    if (j > 1140)
                    {
                        break;
                    }
                }
                j = j + 10;
                e.Graphics.DrawString("----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------", font, Brushes.Black, float.Parse("00"), float.Parse(j.ToString()));
                y += lineHeight;
                j = j + 20;
                e.Graphics.DrawString("OPENING :-", font, Brushes.Black, float.Parse("50"), float.Parse(j.ToString()));
                y += lineHeight;
                e.Graphics.DrawString(label3.Text, font, Brushes.Black, float.Parse("170"), float.Parse(j.ToString()));
                y += lineHeight;
                e.Graphics.DrawString("CLOSING :-", font, Brushes.Black, float.Parse("400"), float.Parse(j.ToString()));
                y += lineHeight;
                e.Graphics.DrawString(label5.Text, font, Brushes.Black, float.Parse("510"), float.Parse(j.ToString()));
                y += lineHeight;
                j = j + 30;
                e.Graphics.DrawString("BALANCE :-   " + label11.Text, font, Brushes.Black, float.Parse("280"), float.Parse(j.ToString()));
                y += lineHeight;


            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }
    }
}

Printing extending the page

 public int i;
        public int k;
        public int f;
        public int s;
        private void button4_Click(object sender, EventArgs e)
        {
            f = dataGridView1.Rows.Count - 1;
            s = dataGridView2.Rows.Count - 1;
            i = (f >= s) ? f : s;
            k = 0;
            while (k < i)
            {
                PrintDocument doc = new PrintDocument();
                doc.PrintPage += new PrintPageEventHandler(doc_PrintPage);
                PrintDialog dlgSettings = new PrintDialog();
                dlgSettings.Document = doc;
                doc.Print();
            }
        }
        void doc_PrintPage(object sender, PrintPageEventArgs e)
        {
            try
            {
                Font font1 = new Font("Arial", 15);
                Font font = new Font("Arial", 10);
                Font font3 = new Font("Arial", 8);
                Font font4 = new Font("Arial", 9);
                Font font2 = new Font("Arial", 13);
                //float x = e.MarginBounds.Left;
                //float lineSide=font.
                float y = e.MarginBounds.Top;
                float lineHeight = font.GetHeight(e.Graphics);

                //Header
                e.Graphics.DrawString("JAI MATA DI", font3, Brushes.Black, float.Parse("132"), float.Parse("1"));
                e.Graphics.DrawString("HARI SWEETS", font2, Brushes.Black, float.Parse("100"), float.Parse("15"));
                e.Graphics.DrawString("Arya Samaj Road, Jacubpura,Gurgaon-122001(Hr.)", font3, Brushes.Black, float.Parse("45"), float.Parse("35"));
                e.Graphics.DrawString("Ph. 2332108, 4063808", font3, Brushes.Black, float.Parse("105"), float.Parse("49"));
                e.Graphics.DrawString("Name   " + textBox7.Text, font3, Brushes.Black, float.Parse("20"), float.Parse("70"));
                e.Graphics.DrawString("S.No.  " + textBox18.Text, font3, Brushes.Black, float.Parse("160"), float.Parse("70"));
                e.Graphics.DrawString("Month  " + comboBox2.Text, font3, Brushes.Black, float.Parse("230"), float.Parse("70"));
                e.Graphics.DrawString("           ..................................        .........              ...................", font3, Brushes.Black, float.Parse("20"), float.Parse("73"));
                //e.Graphics.DrawString(textBox7.Text, font1, Brushes.Black, float.Parse("50"), float.Parse("0"));
                //y += lineHeight;
                //e.Graphics.DrawString("Date:- " + dateTimePicker1.Value.ToString("dd-MM-yyyy") + " To " + dateTimePicker2.Value.ToString("dd-MM-yyyy"), font, Brushes.Black, float.Parse("550"), float.Parse("10"));

                //y += lineHeight;
                //e.Graphics.DrawString("Sell", font, Brushes.Black, float.Parse("100"), float.Parse("30"));
                //y += lineHeight;
                //e.Graphics.DrawString("PAYMENT ", font, Brushes.Black, float.Parse("500"), float.Parse("30"));
                //y += lineHeight;

                e.Graphics.DrawString("-----------------------------------------------------------------" +
                    "-", font, Brushes.Black, float.Parse("14"), float.Parse("80"));
                int ij = 86;
                while (ij < 120)
                {
                    e.Graphics.DrawString("| ", font, Brushes.Black, float.Parse("14"), float.Parse(ij.ToString()));
                    y += lineHeight;
                    e.Graphics.DrawString("| ", font, Brushes.Black, float.Parse("80"), float.Parse(ij.ToString()));
                    y += lineHeight;
                    e.Graphics.DrawString("| ", font, Brushes.Black, float.Parse("150"), float.Parse(ij.ToString()));
                    y += lineHeight;
                    e.Graphics.DrawString("| ", font, Brushes.Black, float.Parse("230"), float.Parse(ij.ToString()));
                    y += lineHeight;
                    e.Graphics.DrawString("| ", font, Brushes.Black, float.Parse("320"), float.Parse(ij.ToString()));
                    y += lineHeight;
                    ij = ij + 10;
                    e.Graphics.DrawString("| ", font, Brushes.Black, float.Parse("14"), float.Parse(ij.ToString()));
                    y += lineHeight;
                    e.Graphics.DrawString("| ", font, Brushes.Black, float.Parse("80"), float.Parse(ij.ToString()));
                    y += lineHeight;
                    e.Graphics.DrawString("| ", font, Brushes.Black, float.Parse("150"), float.Parse(ij.ToString()));
                    y += lineHeight;
                    e.Graphics.DrawString("| ", font, Brushes.Black, float.Parse("230"), float.Parse(ij.ToString()));
                    y += lineHeight;
                    e.Graphics.DrawString("| ", font, Brushes.Black, float.Parse("320"), float.Parse(ij.ToString()));
                    y += lineHeight;
                    ij = ij + 10;
                }


                y += lineHeight;
                //Header
                e.Graphics.DrawString("Date", font3, Brushes.Black, float.Parse("20"), float.Parse("92"));
                y += lineHeight;
                e.Graphics.DrawString("Morning", font3, Brushes.Black, float.Parse("100"), float.Parse("92"));
                y += lineHeight;
                e.Graphics.DrawString("Evening", font3, Brushes.Black, float.Parse("180"), float.Parse("92"));
                y += lineHeight;
                e.Graphics.DrawString("Amount", font3, Brushes.Black, float.Parse("260"), float.Parse("92"));
                y += lineHeight;
                //e.Graphics.DrawString("AMOUNT", font3, Brushes.Black, float.Parse("680"), float.Parse("90"));
                //y += lineHeight;

                //HERE I NEED TO PUT DATA GRID VIEW WHICH MAY VERY NUMBER OF ROWS EVERY TIME    
                //Body
                e.Graphics.DrawString("------------------------------------------------------------------ ", font, Brushes.Black, float.Parse("15"), float.Parse("100"));
                y += lineHeight;

                int j = 115;
                for (int l = k; l < i; l++)
                {
                    if (l < f)
                    {
                        //e.Graphics.DrawString(dataGridView1.Rows[l].Cells[0].Value.ToString(), font, Brushes.Black, float.Parse("40"), float.Parse(j.ToString()));
                        //y += lineHeight;
                        //e.Graphics.DrawString(Convert.ToDateTime(dataGridView1.Rows[l].Cells[1].Value).ToString(), font, Brushes.Black, float.Parse("40"), float.Parse(j.ToString()));
                        //y += lineHeight;
                        //e.Graphics.DrawString(dataGridView1.Rows[l].Cells[2].Value.ToString(), font, Brushes.Black, float.Parse("360"), float.Parse(j.ToString()));
                        //y += lineHeight;
                        //e.Graphics.DrawString(dataGridView1.Rows[l].Cells[3].Value.ToString(), font, Brushes.Black, float.Parse("520"), float.Parse(j.ToString()));
                        //y += lineHeight;
                        //e.Graphics.DrawString(dataGridView1.Rows[l].Cells[4].Value.ToString(), font, Brushes.Black, float.Parse("680"), float.Parse(j.ToString()));
                        //y += lineHeight;
                        //e.Graphics.DrawString(dataGridView1.Rows[l].Cells[2].Value.ToString(), font, Brushes.Black, float.Parse("360"), float.Parse(j.ToString()));
                        //y += lineHeight;

                    }

                    if (l < s)
                    {

                        //e.Graphics.DrawString(dataGridView2.Rows[l].Cells[0].Value.ToString(), font, Brushes.Black, float.Parse("40"), float.Parse(j.ToString()));
                        //y += lineHeight;
                        e.Graphics.DrawString("| ", font, Brushes.Black, float.Parse("14"), float.Parse(j.ToString()));
                        y += lineHeight;
                        e.Graphics.DrawString("| ", font, Brushes.Black, float.Parse("80"), float.Parse(j.ToString()));
                        y += lineHeight;
                        e.Graphics.DrawString("| ", font, Brushes.Black, float.Parse("150"), float.Parse(j.ToString()));
                        y += lineHeight;
                        e.Graphics.DrawString("| ", font, Brushes.Black, float.Parse("230"), float.Parse(j.ToString()));
                        y += lineHeight;
                        e.Graphics.DrawString("| ", font, Brushes.Black, float.Parse("320"), float.Parse(j.ToString()));
                        y += lineHeight;
                        j = j + 10;
                        e.Graphics.DrawString("| ", font, Brushes.Black, float.Parse("14"), float.Parse(j.ToString()));
                        y += lineHeight;
                        e.Graphics.DrawString("| ", font, Brushes.Black, float.Parse("80"), float.Parse(j.ToString()));
                        y += lineHeight;
                        e.Graphics.DrawString("| ", font, Brushes.Black, float.Parse("150"), float.Parse(j.ToString()));
                        y += lineHeight;
                        e.Graphics.DrawString("| ", font, Brushes.Black, float.Parse("230"), float.Parse(j.ToString()));
                        y += lineHeight;
                        e.Graphics.DrawString("| ", font, Brushes.Black, float.Parse("320"), float.Parse(j.ToString()));
                        y += lineHeight;


                        j = j - 10;
                        e.Graphics.DrawString(Convert.ToDateTime(dataGridView2.Rows[l].Cells[1].Value).ToString("dd-MM-yy"), font4, Brushes.Black, float.Parse("20"), float.Parse(j.ToString()));
                        y += lineHeight;
                        e.Graphics.DrawString(dataGridView2.Rows[l].Cells[2].Value.ToString(), font4, Brushes.Black, float.Parse("100"), float.Parse(j.ToString()));
                        y += lineHeight;
                        e.Graphics.DrawString(dataGridView2.Rows[l].Cells[3].Value.ToString(), font4, Brushes.Black, float.Parse("180"), float.Parse(j.ToString()));
                        y += lineHeight;
                        e.Graphics.DrawString(dataGridView2.Rows[l].Cells[4].Value.ToString(), font4, Brushes.Black, float.Parse("260"), float.Parse(j.ToString()));
                        y += lineHeight;
                    }
                    j = j + 20;
                    k++;
                    if (j > 1140)
                    {
                        break;
                    }
                }
                j = j + 10;
                e.Graphics.DrawString("------------------------------------------------------------------ ", font, Brushes.Black, float.Parse("15"), float.Parse(j.ToString()));
                y += lineHeight;
                j = j - 7;
                e.Graphics.DrawString("| ", font, Brushes.Black, float.Parse("14"), float.Parse(j.ToString()));
                y += lineHeight;
                e.Graphics.DrawString("| ", font, Brushes.Black, float.Parse("80"), float.Parse(j.ToString()));
                y += lineHeight;
                e.Graphics.DrawString("| ", font, Brushes.Black, float.Parse("150"), float.Parse(j.ToString()));
                y += lineHeight;
                e.Graphics.DrawString("| ", font, Brushes.Black, float.Parse("230"), float.Parse(j.ToString()));
                y += lineHeight;
                e.Graphics.DrawString("| ", font, Brushes.Black, float.Parse("320"), float.Parse(j.ToString()));
                y += lineHeight;
                j = j + 7;


                j = j + 15;
                e.Graphics.DrawString("Total Quantity : " + textBox8.Text, font4, Brushes.Black, float.Parse("20"), float.Parse(j.ToString()));
                y += lineHeight;

                e.Graphics.DrawString("Current Amount : " + textBox9.Text, font4, Brushes.Black, float.Parse("160"), float.Parse(j.ToString()));
                y += lineHeight;

                j = j + 20;
                e.Graphics.DrawString("Last Amount   : " + textBox17.Text, font4, Brushes.Black, float.Parse("20"), float.Parse(j.ToString()));
                y += lineHeight;

                e.Graphics.DrawString("Total Amount     : " + textBox16.Text, font4, Brushes.Black, float.Parse("160"), float.Parse(j.ToString()));
                y += lineHeight;
                //e.Graphics.DrawString(, font, Brushes.Black, float.Parse("510"), float.Parse(j.ToString()));
                //y += lineHeight;
                //j = j + 30;
                //e.Graphics.DrawString("BALANCE :-   " + label11.Text, font, Brushes.Black, float.Parse("280"), float.Parse(j.ToString()));
                //y += lineHeight;


            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }
       

Print Using Selecting Printer

Step1: Associate control to Print document

private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
           e.Graphics.DrawString(richTextBox1.Text, richTextBox1.Font, Brushes.Black, 100, 20);
           e.Graphics.PageUnit = GraphicsUnit.Inch;        
}

Step2:  write code for printing and print preview.

private void btnPrint_Click(object sender, EventArgs e)
{
            //PrintDialog associate with PrintDocument;
            printDialog1.Document = printDocument1;

            if (printDialog1.ShowDialog()==DialogResult.OK)
            {
                printDocument1.Print();
            }
}

private void btnPrintPreview_Click(object sender, EventArgs e)
{
            //Associate PrintPreviewDialog with PrintDocument.
            printPreviewDialog1.Document = printDocument1;
 
            // Show PrintPreview Dialog
            printPreviewDialog1.ShowDialog();
}

Edit in Datagridview

try
            {
                int id = Convert.ToInt32(dataGridView1.CurrentRow.Index.ToString());
                cid = Convert.ToInt32(dataGridView1.Rows[id].Cells[0].Value.ToString());
                cname = dataGridView1.Rows[id].Cells[1].Value.ToString();
                cadd = dataGridView1.Rows[id].Cells[2].Value.ToString();
                cont = dataGridView1.Rows[id].Cells[3].Value.ToString();
                Edit_Customer ob = new Edit_Customer();
                ob.Show();
                this.Close();
               
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

delete in gridview

try
            {
                int id = Convert.ToInt32(dataGridView1.CurrentRow.Index.ToString());

                cid =Convert.ToInt32( dataGridView1.Rows[id].Cells[0].Value.ToString());
                OleDbCommand cmd = new OleDbCommand("delete * from customer where id="+cid+"", con);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
                gridshow();
               

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

validation

cmd = new OleDbCommand("update valid set date1="+ dateTimePicker1.Value.ToOADate() +" where id='"+ 1 +"'",con);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            da = new OleDbDataAdapter("select * from valid", con);
            dt = new DataTable();
            da.Fill(dt);
            DateTime dd = Convert.ToDateTime(dt.Rows[0][1].ToString());
            DateTime dd1 = Convert.ToDateTime(dt.Rows[0][2].ToString());
            TimeSpan dd2 = dd1 - dd;
            int dd3 = Convert.ToInt32(dd2.TotalDays);
            if (dd3 >= 0)
            {
                label1.Text = "You Have " + dd3 + " Days Left";
            }
            else
            {
                label1.Text = "Your Valid Time is Expired";
                menuStrip1.Visible = false;
            }

Backup

string fileName = "Milk.accdb";
            string sourcePath = @"D:\DatabaseMilk";
            string targetPath = @"D:\";


            // Use Path class to manipulate file and directory paths.
            string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
            string destFile = System.IO.Path.Combine(targetPath, fileName);

            // To copy a folder's contents to a new location:
            // Create a new target folder, if necessary.
            if (!System.IO.Directory.Exists(targetPath))
            {
                System.IO.Directory.CreateDirectory(targetPath);
            }

            // To copy a file to another location and
            // overwrite the destination file if it already exists.
            System.IO.File.Copy(sourceFile, destFile, true);

            // To copy all the files in one directory to another directory.
            // Get the files in the source folder. (To recursively iterate through
            // all subfolders under the current directory, see
            // "How to: Iterate Through a Directory Tree.")
            // Note: Check for target path was performed previously
            //       in this code example.
            if (System.IO.Directory.Exists(sourcePath))
            {
                string[] files = System.IO.Directory.GetFiles(sourcePath);

                // Copy the files and overwrite destination files if they already exist.
                foreach (string s in files)
                {
                    // Use static Path methods to extract only the file name from the path.
                    fileName = System.IO.Path.GetFileName(s);
                    destFile = System.IO.Path.Combine(targetPath, fileName);
                    System.IO.File.Copy(s, destFile, true);

                }
                label1.Visible = true;
            }
            else
            {

            }

Notification

 //dateTimePicker1.Text = DateTime.Parse(dateTimePicker1.Text).ToString();
            da = new OleDbDataAdapter("select distinct(Customer_Name) from cpayment",con);
            dt = new DataTable();
            da.Fill(dt);
           // dataGridView1.DataSource = dt;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string st = dt.Rows[i][0].ToString();
                da = new OleDbDataAdapter("select Max(Payment_Date) from cpayment where Customer_Name='" + dt.Rows[i][0] + "'", con);
                dt = new DataTable();
                da.Fill(dt);
                //dataGridView1.DataSource = dt;
                // MessageBox.Show("" + dt.Rows[0][0].ToString());
                DateTime dd = Convert.ToDateTime(dt.Rows[0][0]);
                DateTime dd1 = Convert.ToDateTime(DateTime.Now.ToShortDateString());
                TimeSpan dd2 = dd1 - dd;
                int dd3 = Convert.ToInt32(dd2.TotalDays);
                if (dd3 >= 40)
                {

                    da = new OleDbDataAdapter("select * from cpayment where Payment_Date=" + dd.ToOADate() + " and Customer_Name='" + st + "'", con);
                    dt = new DataTable();
                    da.Fill(dt);
                   // dataGridView1.DataSource = dt;
                    DateTime mt = Convert.ToDateTime(dt.Rows[0][3].ToString());
                    listView1.Items.Add(""+dt.Rows[0][1].ToString()+  "");
                    listView2.Items.Add("" + mt.ToShortDateString() + "");
                    listView3.Items.Add(""+ dt.Rows[0][4].ToString()+ "");
                }
                else
                {
                    // MessageBox.Show("No");
                }

                da = new OleDbDataAdapter("select distinct(Customer_Name) from cpayment", con);
                dt = new DataTable();
                da.Fill(dt);
            }