Use the grid of devexpress to automatically print and export

Source: Internet
Author: User

When we write a project, we will always encounter some data printing and export. It is really difficult to start thinking about it, but it still solves the problem. Let's share it with you and it will be useful to you in the future.
/// <Summary>
/// Print information
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>
Private void barbuttonitem4_itemclick (Object sender, devexpress. xtrabars. itemclickeventargs E)
{
// Specify the settings applied to a single page
Pagesetaskset_print_page = new pagesetask ();
// Automatically print
Dynamicprinthelper pH = new dynamicprinthelper ();
If (pH. isprintingavailable)
{
// Set attributes for automatic printing
Ph. pagesettings = set_print_page;
Ph. pagesettings. Landscape = true;
// Print the gridview page
Ph. showpreview (this. gridcontrol1, true );
}
/// <Summary>
/// Export
/// </Summary>
/// <Param name = "Bv"> </param>
/// <Param name = "provider"> </param>
Public static void exportto (baseview BV, iexportprovider provider)
{
// Draw the mouse pointer Image
Cursor currentcursor = cursor. Current;
Cursor. Current = cursors. waitcursor;
Baseexportlink link = BV. createexportlink (provider );
Link. exportto (true );
Cursor. Current = currentcursor;
}

/// <Summary>
/// Set a dialog box for saving files is displayed.
/// </Summary>
/// <Param name = "title"> </param>
/// <Param name = "filter"> </param>
/// <Returns> </returns>
Public static string showsavefiledialog (String title, string filter)
{
// Open the file dialog box
Savefiledialog DLG = new savefiledialog ();
// Default file name
String name = "Export File ";
// Set the title bar
DLG. Title = "export to" + title;
// Set the default file name
DLG. filename = Name;
DLG. Filter = filter;
If (DLG. showdialog () = dialogresult. OK)
{
// Returns the file name.
Return DLG. filename;

}
Return "";

}
/// <Summary>
/// Event Method for exporting data to excel
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>
Private void barbuttonitem5_itemclick (Object sender, devexpress. xtrabars. itemclickeventargs E)
{
Try
{
// Open the exported file and save the Dialog
String filename = showsavefiledialog ("Microsoft Excel Document", "Microsoft Excel | *. xls ");
// The file is not empty.
If (filename! = "")
{
// Call the export Method
Exportto (gridview1, new exportxlsprovider (filename ));
}
}
Catch (exception ex)
{
// Catch exceptions
MessageBox. Show (ex. Message. tostring ());
Return;
}

}

You can use the following method to export data, which is more convenient and concise.
/// <Summary>
/// Event Method for exporting data to excel
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>
Private void barbuttonitem export _ itemclick (Object sender, devexpress. xtrabars. itemclickeventargs E)
{
// Save dialog box
Savefiledialog SFD = new savefiledialog ();
// Default file name
String name = "fake and inferior Drug Information ";
// Set the default file name
SFD. filename = Name;
// Set the title bar
SFD. Title = "export file to" + name + "file ";
// Set the exported file format
SFD. Filter = "XLS | *. xls ";
// The save dialog box is displayed.
SFD. showdialog ();
// If the file name is not empty
If (SFD. filename! = NULL)
{
// Export the file
This. gridcontrol information display. exporttoxls (SFD. filename );
}

}< br> catch (exception ES)
{< br> MessageBox. show (es. message);
return;
}< BR >}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.