How to Insert Data in Database and Print the Grid View Data in Windows Forms Application using C#
This is very important Application for any Web Developer.In this Application You can Easily insert data in SQL Database and Print the Grid view data in Windows form application.You can easily make many Application as shown below:
There are some steps please follow this one by one.You can download whole Application and code from the end of the tutorials.
Step1:- First Create a Table in database . Here I have created a 'RAM' table in 'Master' System Database.
See it :-
Step2:- Create a Widows Forms Application. Go through this, File->New Project->Select Widows Forms Application->Select Visual c# (From Left Window)->Choose Application Name(Here my application is'text')->Click OK->Form1 will open.
See it:-
See here:->
See Here:->
Note-> some code are not shown in the image
- Library Book Submission Application.
- School Fee Submission Application.
- Web Application. etc
There are some steps please follow this one by one.You can download whole Application and code from the end of the tutorials.
Step1:- First Create a Table in database . Here I have created a 'RAM' table in 'Master' System Database.
See it :-
See it:-
Step3:- Design this type of interface on Form1.cs(Design) Which is given as below.
Step4:- Add New Form from Solution Explorer. Drag and Drop Grid View control and Button from Toolbox on Form2.cs(Design).After that Drag and Drop' PrintPreviewDialog1' and 'PrintDocument1' from Toolbox as shown below:
Step5:- Go to the Property of print Document1 and Set Document Name=Document. Which is as shown below:-
Step6:- Now go back From1 and double click on 'Insert' Button , 'Show Grid View Data' Button and written the code(Form1.cs) ,which are as given below:-
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
| 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 test { public partial class Form1 : Form { public Form1() { InitializeComponent(); } SqlDataAdapter da; DataSet ds; SqlConnection con; private void button1_Click( object sender, EventArgs e) { con = new SqlConnection( "data source=RAMASHANKER-PC;Integrated Security=Yes;Database=master" ); da = new SqlDataAdapter( "insert into RAM(STUDENT,CLASS,SEX)values('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "')" , con); ds = new DataSet(); da.Fill(ds); MessageBox.Show( "Registration has been successful" ); } private void button2_Click_2( object sender, EventArgs e) { Form2 f2 = new Form2(); f2.Show(); } } } |
See here:->
Step7:-> Go again Form2 .cs(Design) Double click on Preview Button and write the following codes (Form2.cs),which are given below:-
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
| using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; namespace test { public partial class Form2 : Form { public Form2() { InitializeComponent(); } int i = 0; SqlDataAdapter da; DataSet ds; SqlConnection con; private void button1_Click( object sender, EventArgs e) { printPreviewDialog1.ShowDialog(); //Fore direct printout enable the below code //printDocument1.Print(); } private void Form2_Load( object sender, EventArgs e) { con = new SqlConnection( "data source=RAMASHANKER-PC;Integrated Security=Yes;Database=master" ); da = new SqlDataAdapter( "select*from RAM" , con); ds = new DataSet(); da.Fill(ds); dataGridView1.DataSource = ds.Tables[0]; } private void printDocument1_PrintPage( object sender, System.Drawing.Printing.PrintPageEventArgs e) { int width = 0; int height = 0; StringFormat str = new StringFormat(); str.Alignment = StringAlignment.Near; str.LineAlignment = StringAlignment.Center; str.Trimming = StringTrimming.EllipsisCharacter; Pen p = new Pen(Color.Black, 2.5f); #region Draw Column 1 e.Graphics.FillRectangle(Brushes.LightGray, new Rectangle(100, 100, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height)); e.Graphics.DrawRectangle(Pens.Black, 100, 100, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height); e.Graphics.DrawString(dataGridView1.Columns[0].HeaderText, dataGridView1.Font, Brushes.Black, new RectangleF(100, 100, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height), str); #endregion #region Draw column 2 e.Graphics.FillRectangle(Brushes.LightGray, new Rectangle(100 + dataGridView1.Columns[0].Width, 100, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height)); e.Graphics.DrawRectangle(Pens.Black, 100 + dataGridView1.Columns[0].Width, 100, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height); e.Graphics.DrawString(dataGridView1.Columns[1].HeaderText, dataGridView1.Font, Brushes.Black, new RectangleF(100 + dataGridView1.Columns[0].Width, 100, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height), str); //Drawcolumn 3 e.Graphics.FillRectangle(Brushes.LightGray, new Rectangle(100 +100+ dataGridView1.Columns[1].Width, 100, dataGridView1.Columns[1].Width, dataGridView1.Rows[0].Height)); e.Graphics.DrawRectangle(Pens.Black, 100 +100+ dataGridView1.Columns[1].Width, 100, dataGridView1.Columns[1].Width, dataGridView1.Rows[0].Height); e.Graphics.DrawString(dataGridView1.Columns[2].HeaderText, dataGridView1.Font, Brushes.Black, new RectangleF(100 + dataGridView1.Columns[1].Width, 100, dataGridView1.Columns[0].Width, dataGridView1.Rows[1].Height), str); width = 100 + dataGridView1.Columns[0].Width; height = 100; //variable i is declared at class level to preserve the value of i if e.hasmorepages is true while (i < dataGridView1.Rows.Count) { if (height > e.MarginBounds.Height) { height = 100; width = 100; e.HasMorePages = true ; return ; } height += dataGridView1.Rows[i].Height; e.Graphics.DrawRectangle(Pens.Black, 100, height, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height); e.Graphics.DrawString(dataGridView1.Rows[i].Cells[0].FormattedValue.ToString(), dataGridView1.Font, Brushes.Black, new RectangleF(100, height, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height), str); e.Graphics.DrawRectangle(Pens.Black, 100 + dataGridView1.Columns[0].Width, height, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height); e.Graphics.DrawString(dataGridView1.Rows[i].Cells[1].FormattedValue.ToString(), dataGridView1.Font, Brushes.Black, new RectangleF(100 + dataGridView1.Columns[0].Width, height, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height), str); e.Graphics.DrawRectangle(Pens.Black, 200 + dataGridView1.Columns[0].Width, height, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height); e.Graphics.DrawString(dataGridView1.Rows[i].Cells[2].FormattedValue.ToString(), dataGridView1.Font, Brushes.Black, new RectangleF(200 + dataGridView1.Columns[0].Width, height, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height), str); width += dataGridView1.Columns[0].Width; i++; } #endregion } } } |
Note-> some code are not shown in the image
Step8:-> Run the project(click F5).You can easily Insert Data in database and print the Grid View data as shown below.
See here:-
Now click on Preview Button ,then You will see a Preview window, Now you can easily print the grid view data. If want to print the grid view data on click the Preview button then you can enable and disable some codes ,which are shown below:
codes are:-
printDocument1.Print(); --> disable the Comment .
printPreviewDialog1.ShowDialog(); --> Comment it.
See it:->Preview window
Click below for download whole Application :-
in this case all columns are of same size,i want to increase size of specific column,plzzzzzzzz help
ReplyDelete