.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> C# >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Print datagridview with taking image of the datagridview

Posted By:sandeepms       Posted Date: January 06, 2009    Points: 25    Category: C#    URL: http://www.dotnetspark.com  
 

The below code is for complete program guidance for beginners who can learn about datagrid view from scratch. I added several events for this datagridview. For eg:- CellValueChanged, CellClick

Add dataGridView to the Windows Forms then add events named CellClick. To do this Click on fourth tab of property window Then choose a event to add double click on Event named Cell Click. Then start coding

The basic thing in Cell Related events is MessageBox.Show(e.ColumnIndex.ToString()); will display Column no of datagridview remember column number is 0 based index value. Similary this will apply to e.RowIndex values too. This code MessageBox.Show(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString()); will display the content of clicked cell.

This is a base concept in dataGridView


using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Data.SqlClient;

using System.Drawing;

using System.Drawing.Printing;

using System.Drawing.Imaging;

using System.Text;

using System.IO;

using System.Windows.Forms;

namespace hotelmanagement

{

public partial class items : Form

{

Font printFont;

string path;

int rowscount;

int rowshieght;

StreamReader sr;

Bitmap bmp;

double pce;

bool isCellValuechanged = false;

string totalamount;

string conn = "server=localhost;database=hotelmanagement;trusted_connection=true";

public items()

{

InitializeComponent();

}

private void items_Load(object sender, EventArgs e)

{

SqlConnection cnn = new SqlConnection(conn);

SqlDataAdapter da = new SqlDataAdapter("select * from items", cnn);

DataSet ds = new DataSet();

da.Fill(ds);

dataGridView1.DataSource = ds.Tables[0].DefaultView;

SqlDataAdapter da1 = new SqlDataAdapter("select * from rates", cnn);

DataSet ds1 = new DataSet();

da1.Fill(ds1);

dataGridView2.DataSource = ds1.Tables[0].DefaultView;

rowscount = dataGridView2.Rows.Count+1;

rowshieght = (rowscount * 22) + 25;

cnn.Close();

}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)

{

SqlConnection cnn = new SqlConnection(conn);

SqlCommand cmm = new SqlCommand("select * from items where type=@1", cnn);

cmm.Parameters.AddWithValue("@1",comboBox1.Text);

SqlDataAdapter da = new SqlDataAdapter(cmm);

DataSet ds = new DataSet();

da.Fill(ds);

dataGridView1.DataSource = ds.Tables[0].DefaultView;

SqlDataAdapter da1 = new SqlDataAdapter("select * from rates", cnn);

DataSet ds1 = new DataSet();

da1.Fill(ds1);

dataGridView2.DataSource = ds1.Tables[0].DefaultView;

cnn.Close();

}

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)

{

double itemid = Convert.ToDouble(dataGridView1.Rows[e.RowIndex].Cells[0].Value);

string itemname = Convert.ToString(dataGridView1.Rows[e.RowIndex].Cells[1].Value);

pce = Convert.ToDouble(dataGridView1.Rows[e.RowIndex].Cells[2].Value);

SqlConnection cnn = new SqlConnection(conn);

SqlDataAdapter da1 = new SqlDataAdapter("select * from rates where itemid='"+itemid+"'", cnn);

DataSet ds1 = new DataSet();

da1.Fill(ds1);

if (ds1.Tables[0].Rows.Count == 0)

{

SqlCommand cmm = new SqlCommand("insert into rates (itemid,itemname,price) values (@1,@2,@3)", cnn);

cmm.Parameters.AddWithValue("@1", itemid);

cmm.Parameters.AddWithValue("@2", itemname);

cmm.Parameters.AddWithValue("@3", pce);

cnn.Open();

cmm.ExecuteNonQuery();

}

SqlDataAdapter da2 = new SqlDataAdapter("select * from rates", cnn);

DataSet ds2 = new DataSet();

da2.Fill(ds2);

dataGridView2.DataSource = ds2.Tables[0].DefaultView;

}

private void dataGridView2_CellValueChanged(object sender, DataGridViewCellEventArgs e)

{

try

{

if (e.ColumnIndex == 3)

{

double itemid = Convert.ToDouble(dataGridView2.Rows[e.RowIndex].Cells[1].Value);

if (itemid.ToString() != "")

{

double price = Convert.ToDouble(dataGridView2.Rows[e.RowIndex].Cells[4].Value);

double quantity = Convert.ToDouble(dataGridView2.Rows[e.RowIndex].Cells[3].Value);

double result = price * quantity;

dataGridView2.Rows[e.RowIndex].Cells[5].Value = result.ToString();

SqlConnection cnn = new SqlConnection(conn);

SqlCommand cmm = new SqlCommand("update rates set quantity=@1,totalprice=@2 where itemid=@3", cnn);

cmm.Parameters.AddWithValue("@1", quantity);

cmm.Parameters.AddWithValue("@2", result);

cmm.Parameters.AddWithValue("@3", itemid);

cnn.Open();

cmm.ExecuteNonQuery();

cnn.Close();

SqlDataAdapter da1 = new SqlDataAdapter("select * from rates", cnn);

DataSet ds1 = new DataSet();

da1.Fill(ds1);

dataGridView2.DataSource = ds1.Tables[0].DefaultView;

SqlDataAdapter da2 = new SqlDataAdapter("SELECT SUM(totalprice) AS totalamount FROM rates", cnn);

DataSet ds2 = new DataSet();

da2.Fill(ds2);

string totalamount = ds2.Tables[0].Rows[0]["totalamount"].ToString();

int rCount = dataGridView2.Rows.Count-1;

string tamount = "Total Amount";

dataGridView2.Rows[rCount].Cells[5].Value = totalamount.ToString();

dataGridView2.Rows[rCount].Cells[3].Value = tamount.ToString();

cnn.Close();

}

else

{

MessageBox.Show("There is no items to calculate");

}

}

}

catch(Exception exc)

{

//MessageBox.Show(exc.Message);

}

}

private void items_FormClosing(object sender, FormClosingEventArgs e)

{

this.dataGridView2.CellValueChanged += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView2_CellValueChanged);

}

private void btnSubmit_Click(object sender, EventArgs e)

{

this.dataGridView2.CellValueChanged += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView2_CellValueChanged);

}

private void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e)

{

this.dataGridView2.CellValueChanged += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView2_CellValueChanged);

}

The below Code will print the dataGridView Results in printer.

private void btnBill_Click(object sender, EventArgs e)

{

Random rand = new Random();

string pname = rand.Next().ToString() + ".jpg";

SaveFileDialog sfd = new SaveFileDialog();

sfd.Filter = "Bitmap (*.bmp)|*.bmp|GIF (*.gif)|*.gif|" +

"JPEG (*.jpg)|*.jpg;*.jpeg|TIF (*.tif)|*.tif";

sfd.InitialDirectory = "C:\\";

sfd.FilterIndex = 3;

sfd.FileName = pname;

Bitmap bmp = new Bitmap(dataGridView2.Width, rowshieght, PixelFormat.Format64bppArgb);

Graphics grfx = Graphics.FromImage(bmp);

grfx.CopyFromScreen(dataGridView2.Location, new Point(0, 0), new Size(dataGridView1.Width, rowshieght), CopyPixelOperation.SourceCopy);

grfx.Save();

if (sfd.ShowDialog() == DialogResult.OK)

{

path = sfd.FileName;

bmp.Save(sfd.FileName, ImageFormat.Jpeg);

printFont = new Font("Arial", 12);

printDialog1.AllowSomePages = true;

printDialog1.ShowHelp = true;

printPreviewDialog1.Document = printDocument1;

printPreviewDialog1.ShowDialog();

if (printDialog1.ShowDialog() == DialogResult.OK)

{

sr = new StreamReader(path);

printDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(printDocument1_PrintPage);

printDocument1.Print();

//printPreviewDialog1.Document = printDocument1;

sr.Close();

}

}

}

private void printDocument1_PrintPage(object sender, PrintPageEventArgs ev)

{

bmp = new Bitmap(path);

Rectangle rect = new Rectangle(dataGridView2.Location, new Size(dataGridView2.Width, rowshieght));

ev.Graphics.DrawImage(bmp, ev.Graphics.VisibleClipBounds);

//ev.Graphics.DrawString("Bill", printFont, Brushes.Black, rect, StringFormat.GenericDefault);

if (bmp.Height > 800)

{

ev.HasMorePages = true;

}

else

{

ev.HasMorePages = false;

}

}

The below code will validate dataGridView Column numbered 4

private void dataGridView2_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)

{

double quantity;

{

try

{

if (e.ColumnIndex == 3)

{

quantity = Convert.ToDouble(e.FormattedValue);

}

}

catch

{

MessageBox.Show("You must enter a Digit in quantity Column");

}

}

}

 


 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend