Load XML File Data in DataGridView & Print DataGridView Data

Let us look at the actual form:

Now let us see how to load data into the DataGridView from the XML file.
Use the following namespace for using XML:
  1. using System.Xml;  
Use the following namespace for using the DataSet to load the data into the DataSet from the XML file & then from the DataSet to DataGridView.
  1. using System.Data;  
Now create the object of the DataSet. 
  1. DataSet myDataSet = new DataSet(); 
When the user clicks on the Load XML button then it will Read the data from the XML file & then store that data into the DataSet.
  1. myDataSet.ReadXml(@"C:\Users\Indus_User\Desktop\books.xml");  
Now we have to give that DataSet data to the DataGridView using the DataSource.
  1. myDataGridView.DataSource = myDataSet.Tables[0].DefaultView;  
The original Load XML button code: 
  1. private void btnLoad_Click(object sender, EventArgs e)  
  2. {             
  3.     myDataSet.ReadXml(@"C:\Users\Indus_User\Desktop\books.xml");  
  4.     myDataGridView.DataSource = myDataSet.Tables[0].DefaultView;  
  5. } 
See the following Image:

OK. After this very small & easy code it will load the data from the XML file into the DataGridView.
Now come to print that loaded data into the DataGridView.
Use the following namespace to use the bitmap class.
  1. using System.Drawing;  
Now create the object of the PrintDocument. 
  1. System.Drawing.Printing.PrintDocument myPrintDocument = new System.Drawing.Printing.PrintDocument(); 
Create the Event of the PrintPage Of the PrintDocument.
  1. myPrintDocument.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(myPrintDocument_PrintPage);  
Now print the data. 
  1. myPrintDocument.Print(); 
The original Print Button Code: 
  1. private void btnPrint_Click(object sender, EventArgs e)  
  2. {  
  3.     myPrintDocument.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(myPrintDocument_PrintPage);  
  4.     myPrintDocument.Print();  
  5. }  
Let us see how to print the DataGridView.
First of all we have to calculate the Width & Height of the records of the DataGridView.
Find the width : 
  1. int myWidth = 0;  
  2. for (int i = 0; i < myDataGridView.Columns.Count; i++)  
  3. {  
  4.     myWidth += myDataGridView.Columns[i].Width;   
  5. }  
From the above code it will calculate the width of each column & then we will get the whole width.
Find the Height : 
  1. int myHeight = 0;  
  2. myHeight=Convert.ToInt32(myDataGridView.Rows[0].Height * (myDataGridView.Rows.Count));  
Now create the object of the Bitmap of calculated Width & Height.
  1. Bitmap myBitmap = new Bitmap(myWidth, myHeight);  
Now use the DrawToBitmap function to convert the DataGridView into a bitmap image using the calculated Width & Height.
  1. myDataGridView.DrawToBitmap(myBitmap, new Rectangle(0, 0, myWidth, myHeight));  
Finally Draw that bitmap image.
  1. e.Graphics.DrawImage(myBitmap, 0, 0);  
The whole code of the PrintPageEventHandler method: 
  1. private void myPrintDocument_PrintPage(System.Object sender, System.Drawing.Printing.PrintPageEventArgs e)  
  2. {  
  3.     int myWidth = 0;  
  4.     int myHeight = 0;  
  5.     myHeight=Convert.ToInt32(myDataGridView.Rows[0].Height * (myDataGridView.Rows.Count));  
  7.     for (int i = 0; i < myDataGridView.Columns.Count; i++)  
  8.     {  
  9.         myWidth += myDataGridView.Columns[i].Width;  
  10.     }  
  12.     Bitmap myBitmap = new Bitmap(myWidth, myHeight);  
  13.     myDataGridView.DrawToBitmap(myBitmap, new Rectangle(0, 0, myWidth, myHeight));  
  14.     e.Graphics.DrawImage(myBitmap, 0, 0);  
  15. }  
See the following image which is the print of the DataGridView:

Hope this is easy to understand for you.

Similar Articles