Reader Level:
ARTICLE

Load XML File Data in DataGridView & Print DataGridView Data

Posted by Ghanashyam Nayak Articles | Printing in C# February 09, 2012
Using this article you can understand how to load the XML file's record into the DataGridView & then you can print that DataGridView.
  • 1
  • 0
  • 13919
Download Files:
 

Let us look at the actual form:

1.png

Now let us see how to load data into the DataGridView from the XML file.

Use the following namespace for using XML:

 

 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.
 

 using System.Data;

Now create the object of the DataSet.
 

 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.
 

 myDataSet.ReadXml(@"C:\Users\Indus_User\Desktop\books.xml");

Now we have to give that DataSet data to the DataGridView using the DataSource.
 

 myDataGridView.DataSource = myDataSet.Tables[0].DefaultView;

The original Load XML button code:
 

 private void btnLoad_Click(object sender, EventArgs e)
{
            myDataSet.ReadXml(@"C:\Users\Indus_User\Desktop\books.xml");
            myDataGridView.DataSource = myDataSet.Tables[0].DefaultView;
}

See the following Image:

2.png

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 name space for use the bitmap class.
 

 using System.Drawing;

Now create the object of the PrintDocument.
 

 System.Drawing.Printing.PrintDocument myPrintDocument = new System.Drawing.Printing.PrintDocument();

Create the Event of the PrintPage Of the PrintDocument.
 

 myPrintDocument.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(myPrintDocument_PrintPage);

Now print the data.
 

 myPrintDocument.Print();

The original Print Button Code:
 

private void btnPrint_Click(object sender, EventArgs e)
{
            myPrintDocument.PrintPage += 
new System.Drawing.Printing.PrintPageEventHandler(myPrintDocument_PrintPage);
            myPrintDocument.Print();
}

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 :
 

int myWidth = 0;

for (int i = 0; i < myDataGridView.Columns.Count; i++)
{

         myWidth += myDataGridView.Columns[i].Width; 
}

From the above code it will calculate the width of each column & then we will get the whole width.

Find the Height :
 

int myHeight = 0;

myHeight=Convert.ToInt32(myDataGridView.Rows[0].Height * (myDataGridView.Rows.Count));

Now create the object of the Bitmap of calculated Width & Height.
 

 Bitmap myBitmap = new Bitmap(myWidth, myHeight);

Now use the DrawToBitmap function to convert the DataGridView into a bitmap image using the calculated Width & Height.
 

 myDataGridView.DrawToBitmap(myBitmap, new Rectangle(0, 0, myWidth, myHeight));

Finally Draw that bitmap image.
 

 e.Graphics.DrawImage(myBitmap, 0, 0);

The whole code of the PrintPageEventHandler method:
 

private void myPrintDocument_PrintPage(System.Object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
            
int myWidth = 0;
            
int myHeight = 0;
            myHeight=Convert.ToInt32(myDataGridView.Rows[0].Height * (myDataGridView.Rows.Count));

            for (int i = 0; i < myDataGridView.Columns.Count; i++)
            {
                myWidth += myDataGridView.Columns[i].Width;
            }

            Bitmap myBitmap = new Bitmap(myWidth, myHeight);
            myDataGridView.DrawToBitmap(myBitmap, 
new Rectangle(0, 0, myWidth, myHeight));
            e.Graphics.DrawImage(myBitmap, 0, 0);
}

See the following image which is the print of the DataGridView:

3.gif

Hope this is easy to understand for you.

COMMENT USING

Trending up