Dev Gridcontrol Data export format issues

Source: Internet
Author: User

Environment: devexpress9.3,vs2008

DevExpress's Gridcontrol provides convenient data export to Excel features, which users can export in accordance with the Gridcontrol format (the stream stream method).

1, if the table has a column character type, but stored as a number, then export to excel in accordance with the Gridcontrol format will produce the "convert numbers stored as text to numbers" problem tip, you must convert these values into numbers in Excel, you can calculate and so on.

The way to do this is simply to modify the data type of this column of the Gridcontrol bound data source to a numeric type.

2, I manually processed the stored numbers, the integer using the thousand-bit display (intvalue.tostring ("###,###") method), the decimal with the thousand bits two decimal places display, less than two decimal places with 0 complement (String.Format ("{0:n}", Doublevalue) method or String.Format ("{0:n2}", Doublevalue) method. This way, the stored number is converted to a string, and if the column numeric type, it will result in an assignment error, so the column can only be a character type, and the same result of exporting to excel in Gridcontrol format will result in the "convert numbers stored as text to numbers" issue.

For this method, we can solve it by gridcontrol the data format. The following two methods can be implemented.

(1) Implementation via code

for integers:

Gridviewtest.columns[i]. Displayformat.formattype = Formattype.numeric;
Gridviewtest.columns[i]. displayformat.formatstring = "#,###";

For decimals:

Gridviewtest.columns[i]. Displayformat.formattype = Formattype.numeric;
Gridviewtest.columns[i]. displayformat.formatstring = "#,###.00";

(1) Implementation by design form

for integers:

For decimals:

The following code implementations are exported to Excel:

   1:private void Btnexport_click (object sender, EventArgs e)
   2: {
   3:     savefiledialog1.filename = "";
   4:     savefiledialog1.filter = "Excel file (. xls) |*.xls";
   5:     savefiledialog1.filename = "Test Document";
   6:     savefiledialog1.overwriteprompt = false;//existing file overwrite hint
   7:     if (Savefiledialog1.showdialog ()! = DialogResult.OK)
   8:         return;
   9:     //File overwrite hint already exists
  10:     
  11:         
  :         "Hint", Messageboxbuttons.yesno, messageboxicon.question)! = Dialogresult.yes)
  :     {
  :         if (Savefiledialog1.showdialog ()! = DialogResult.OK)
  :             return;
  :     }
  :     if (savefiledialog1.filename! = "")
  :     {
  :         Try
  :         {
  :             System.IO.FileStream fs =
  :                (System.IO.FileStream) savefiledialog1.openfile ();
  At:             This.gridControlTest.ExportToXls (FS);
  :             FS. Close ();
  :             DevExpress.XtraEditors.XtraMessageBox.Show ("Data export success!") "," prompt ");
  :         }
  :         catch (Exception ex)
  :         {
  :             if (ex. Message.contains ("being used by another process")
  :             {
  To:                 DevExpress.XtraEditors.XtraMessageBox.Show ("Data export failed!") The file is being occupied by another program! "," prompt ");
  :             }
  :             Else
  DevExpress.XtraEditors.XtraMessageBox.Show:                 ("Data export failed!") Data volume is too large, please separate statistics and re-export! "," prompt ");
  :         }
  :     }
  37:}

Dev Gridcontrol Data export format issues

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.