Programming for Everybody: datagridview:print
Showing posts with label datagridview:print. Show all posts
Showing posts with label datagridview:print. Show all posts

insert update delete search and print in sql server databse using C# [with Source code]


 using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

using System.Data.SqlClient;

namespace Insert_update_delete_search_and_print_in_sql

{

    public partial class Form2 : Form

    {

        public Form2()

        {

            InitializeComponent();

        }

        SqlConnection conn = new SqlConnection("Data source=.;initial catalog=database3;integrated security=true");

        private void Form2_Load(object sender, EventArgs e)

        {

            bind_data();

        }

        private void bind_data()

        {

            SqlCommand cmd1 = new SqlCommand("Select id,fname As firstname,lname As Lastname,sum from Table1", conn);

            SqlDataAdapter da = new SqlDataAdapter();

            da.SelectCommand = cmd1;

            DataTable dt = new DataTable();

            dt.Clear();

            da.Fill(dt);

            dataGridView1.DataSource = dt;

            dataGridView1.ColumnHeadersDefaultCellStyle.Font = new Font("Tahoma", 12, FontStyle.Bold);

            dataGridView1.DefaultCellStyle.Font = new Font("arial", 12);

        }


        private void button1_Click(object sender, EventArgs e)

        {

            SqlCommand cmd2 = new SqlCommand("Insert into Table1(id,fname,lname,sum)Values(@id,@firstname,@lastname,@sum)", conn);

            cmd2.Parameters.AddWithValue("id", textBox1.Text);

            cmd2.Parameters.AddWithValue("firstname", textBox2.Text);

            cmd2.Parameters.AddWithValue("lastname", textBox3.Text);

            cmd2.Parameters.AddWithValue("sum", textBox4.Text);

            conn.Open();

            cmd2.ExecuteNonQuery();

            conn.Close();

            bind_data();


        }


        private void button2_Click(object sender, EventArgs e)

        {

            textBox1.Text = "";

            textBox2.Text = "";

            textBox3.Text = "";

            textBox4.Text = "";


        }


        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)

        {

            int index;

            index = e.RowIndex;

            DataGridViewRow selectedrow = dataGridView1.Rows[index];

            textBox1.Text = selectedrow.Cells[0].Value.ToString();

            textBox2.Text = selectedrow.Cells[1].Value.ToString();

            textBox3.Text = selectedrow.Cells[2].Value.ToString();

            textBox4.Text = selectedrow.Cells[3].Value.ToString();



        }


        private void button3_Click(object sender, EventArgs e)

        {

            SqlCommand cmd3 = new SqlCommand("Update Table1 Set fname=@firstname,lname=@lastname,sum=@sum where id=@id", conn);


            

            cmd3.Parameters.AddWithValue("firstname", textBox2.Text);

            cmd3.Parameters.AddWithValue("lastname", textBox3.Text);

            cmd3.Parameters.AddWithValue("sum", textBox4.Text);

            cmd3.Parameters.AddWithValue("id", textBox1.Text);

            conn.Open();

            cmd3.ExecuteNonQuery();

            conn.Close();

            bind_data();

        }


        private void button4_Click(object sender, EventArgs e)

        {

            SqlCommand cmd4 = new SqlCommand("Delete from Table1 where id=@id", conn);

            cmd4.Parameters.AddWithValue("id", textBox1.Text);

            conn.Open();

            cmd4.ExecuteNonQuery();

            conn.Close();

            bind_data();

        }


        private void button5_Click(object sender, EventArgs e)

        {

            SqlCommand cmd1 = new SqlCommand("Select id,fname As firstname,lname As Lastname,sum from Table1 where fname Like @firstname+'%'", conn);

            cmd1.Parameters.AddWithValue("firstname", textBox5.Text);

            SqlDataAdapter da = new SqlDataAdapter();

            da.SelectCommand = cmd1;

            DataTable dt = new DataTable();

            dt.Clear();

            da.Fill(dt);

            dataGridView1.DataSource = dt;

            dataGridView1.ColumnHeadersDefaultCellStyle.Font = new Font("Tahoma", 12, FontStyle.Bold);

            dataGridView1.DefaultCellStyle.Font = new Font("arial", 12);

        }


        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)

        {

            Bitmap imagebmp = new Bitmap(dataGridView1.Width, dataGridView1.Height);

            dataGridView1.DrawToBitmap(imagebmp, new Rectangle(0, 0, dataGridView1.Width, dataGridView1.Height));

            e.Graphics.DrawImage(imagebmp, 120, 20);

        }


        private void button6_Click(object sender, EventArgs e)

        {

            printPreviewDialog1.Document = printDocument1;

            printPreviewDialog1.PrintPreviewControl.Zoom = 1;

            printPreviewDialog1.ShowDialog();

        }

    }

}

Visual Basic.NET: insert, update, delete and search using access database and print datagridview

 



Imports System.Data.OleDb

Public Class Form3

    Dim conn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=F:\listview.accdb")

    Private Sub bind_data()

        Dim cmd1 As New OleDbCommand("Select * from table1", conn)

        Dim da As New OleDbDataAdapter

        da.SelectCommand = cmd1

        Dim table1 As New DataTable

        table1.Clear()

        da.Fill(table1)

        DataGridView1.DataSource = table1


    End Sub


    Private Sub Form3_Load(sender As Object, e As EventArgs) Handles Me.Load

        bind_data()

    End Sub


    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim strsql As String

        strsql = "Insert into table1(id,firstname,lastname,sum1)Values(@id,@firstname,@lastname,@sum1)"

        Dim cmd2 As New OleDbCommand(strsql, conn)

        cmd2.Parameters.AddWithValue("@id", TextBox1.Text)

        cmd2.Parameters.AddWithValue("@firstname", TextBox2.Text)

        cmd2.Parameters.AddWithValue("@lastname", TextBox3.Text)

        cmd2.Parameters.AddWithValue("@sum1", TextBox4.Text)

        conn.Open()

        cmd2.ExecuteNonQuery()

        conn.Close()

        bind_data()



    End Sub


    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

        TextBox1.Text = ""

        TextBox2.Text = ""

        TextBox3.Text = ""

        TextBox4.Text = ""


    End Sub


    Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick

        Dim index As Integer

        index = e.RowIndex

        Dim selectedrow As DataGridViewRow = DataGridView1.Rows(index)

        TextBox1.Text = selectedrow.Cells(0).Value.ToString

        TextBox2.Text = selectedrow.Cells(1).Value.ToString

        TextBox3.Text = selectedrow.Cells(2).Value.ToString

        TextBox4.Text = selectedrow.Cells(3).Value.ToString


    End Sub


    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click

        Dim cmd4 As New OleDbCommand("Update table1 set firstname='" & TextBox2.Text & "',lastname='" & TextBox3.Text & "',sum1=" & TextBox4.Text & " where id=" & TextBox1.Text & "", conn)

        conn.Open()

        cmd4.ExecuteNonQuery()

        conn.Close()

        bind_data()

    End Sub


    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click

        Dim cmd5 As New OleDbCommand("delete from table1 where id=@id", conn)

        cmd5.Parameters.AddWithValue("@id", TextBox1.Text)

        conn.Open()

        cmd5.ExecuteNonQuery()

        conn.Close()

        bind_data()

    End Sub


    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click

        PrintPreviewDialog1.Document = PrintDocument1

        PrintPreviewDialog1.PrintPreviewControl.Zoom = 1

        PrintPreviewDialog1.ShowDialog()


    End Sub


    Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage

        Dim imagebmp As New Bitmap(Me.DataGridView1.Width, Me.DataGridView1.Height)

        DataGridView1.DrawToBitmap(imagebmp, New Rectangle(0, 0, Me.DataGridView1.Width, Me.DataGridView1.Height))

        e.Graphics.DrawImage(imagebmp, 120, 20)

    End Sub


    Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click

        Dim cmd1 As New OleDbCommand("Select * from table1 where firstname like '%' +@parm1+ '%' ", conn)

        cmd1.Parameters.AddWithValue("@parm1", TextBox5.Text)

        Dim da As New OleDbDataAdapter

        da.SelectCommand = cmd1

        Dim table1 As New DataTable

        table1.Clear()

        da.Fill(table1)

        DataGridView1.DataSource = table1

    End Sub

End Class