Wijmo GridViewWidgetYou cannot export an Excel file. This blog introduces a simple method to save the Wijmo GridView control to Excel. You can use the same method on C1 WebUI GridView.
Procedure1:SetC1GridViewBind to Data Source
The first step is to bind C1GridView to the data source. For simplicity, we bind it to the c1mers table of C1Nwind. mdb.
650) this. width = 650; "style =" border-right-0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px "title =" image "border =" 0 "alt =" image "src =" http://www.bkjia.com/uploads/allimg/131228/132T42W1-0.png "width =" 896 "height =" 330 "/>
Procedure2:ExportC1GridViewToExcel
Two steps are required to export data to Excel. The first step is to save the GridView to an HTML string.
The Web control hasRenderControl ()Method to output the content of the server-side control to the specifiedHtmlTextWriterObject. If Tracing is enabled, this method also stores the Trace information of the control. ThenHtmlTextWriterOutput object toStringWriterObject.
The following method is used to create a string:
- Public Function DataGridToExcel (ByVal dgExport As C1.Web. Wijmo. Controls. C1GridView. C1GridView) As String
-
- 'Create a stringwriter
-
- Dim stringWrite As New System. IO. StringWriter ()
-
- 'Create an htmltextwriter that uses the stringwriter
-
- Dim htmlWrite As New System. Web. UI. HtmlTextWriter (stringWrite)
-
- Dim dg As C1.Web. Wijmo. Controls. C1GridView. C1GridView
-
- 'Just set the input datagrid = to the new dg grid
-
- Dg = dgExport
-
- 'Bold the header font
-
- Dg. HeaderStyle. Font. Bold = True
-
- 'If needed, the color/format is changed at the component level.
-
- Dg. HeaderStyle. ForeColor = System. Drawing. Color. Black
-
- Dg. RowStyle. ForeColor = System. Drawing. Color. Black
-
- 'Bind the modified datagrid
-
- 'Tells the datagrid to present itself to the htmltextwriter we provide.
-
- Dg. AllowSorting = False
-
- Dg. AllowPaging = False
-
- Dg. AllowCustomPaging = False
-
- 'New code
-
- Dim parent As Control = dg. Parent
-
- Parent. Controls. Remove (dg)
-
- Dg. RenderControl (htmlWrite)
-
- 'New code
-
- Parent. Controls. Add (dg)
-
- 'Output HTML
-
- Return stringWrite. ToString ()
-
- End Function
650) this. width = 650; "style =" border-right-0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px "title =" image "border =" 0 "alt =" image "src =" http://www.bkjia.com/uploads/allimg/131228/132T45021-1.png "width =" 897 "height =" 157 "/>
Next, we will callDownloadToExcelMethod To create an excel file from the saved string.
- Public Sub DownloadToExcel (ByVal content As String, ByVal response As HttpResponse)
-
- 'Clear response. object
-
- Response. Clear ()
-
- Response. Buffer = True
-
- Response. Charset = ""
-
- 'Set the response MIME type to excel.
-
- Response. ContentType = "application/vnd. ms-excel"
-
- Response. ContentEncoding = New System. Text. UTF8Encoding ()
-
- Response. Write (content)
-
- Response. End ()
-
- End Sub
650) this. width = 650; "style =" border-right-0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px "title =" image "border =" 0 "alt =" image "src =" http://www.bkjia.com/uploads/allimg/131228/132T42L6-2.png "width =" 604 "height =" 404 "/>
Implementation problems
In many cases, you may encounter some errors during export. You may receive an error message: "RegisterForEventValidation can only be called during Render ;". In this case, try the following methods:
1. You can reload the VerifyRenderingInServerForm method as described in the following article:
Http://connect.microsoft.com/VisualStudio/feedback/details/118285/rendercontrol-doesnt-work-for-gridview
Public Overrides Sub VerifyRenderingInServerForm (control As Control)
End Sub
2. To avoid the error that "RegisterForEventValidation can only be called during Render ();", disable Page. EnableEventValidation or place the RenderControl method call in a try-catch Block.
In addition, if the gridview contains a check box or a template column, you will receive the above error. Currently, the same error occurs in Microsoft's GridView. Because C1GridView inherits from Microsoft's GridView, it is a known design issue of C1GridView.
Download example
Download Wijmo. Please goStudio for ASP. NET Wijmo 2012 v1 officially released 2012.03.22 )!