The data table is displayed. Using the datagridview control, you can display and edit tables from different data sources. It is very simple and intuitive to bind data to the DataGridView control, in most cases, you only need to set the DataSource attribute. When binding to a database source that contains multiple lists or tables, you only need to set the DataMember attribute to the bound list or table string. The data room charging system used the data table display multiple times and exported it as an Excel table. For the first time, the data room charging system used the VB version. The Excel export method is as follows:
Open VB-project-Application-check Microsoft Excel 14.0 Objexts 2.6 Library
The Code is as follows:
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHByZSBjbGFzcz0 = "brush: java;"> Private Sub CmdExport_Click () Dim j As Integer Dim xlApp As Excel. application Dim xlBook As Excel. workbook Dim xlSheet As Excel. worksheet Set xlApp = CreateObject ("Excel. application ") 'instantiate the object xlApp. visible = True Set xlBook = xlApp. workbooks. add Set xlSheet = xlBook. worksheets (1) For I = 0 To myflexgrid. rows-1 For j = 0 To myflexgrid. cols-1 myflexgrid. row = I myflexgrid. col = j xlSheet. cells (I + 1, j + 1) = Trim (myflexgrid. text) Next End SubThe second version of the IDC charging system VB. NET version, how does she export the Exce table nie? What is the same as how to export the Excel table with VB, and what is the beauty of VB? The following is a one-to-one introduction:
First, we need to reference
There is another detail problem. We need to change the attribute AllowUserToAddRow to False. The difference between True and False will be explained later:
The programming code is as follows:
Public Function dirivExcel (ByVal dgv As DataGridView) As Boolean Dim myExcel As New Microsoft. office. interop. excel. application 'create an Excel connection to myExcel. application. workbooks. add (True) myExcel. visible = True Dim I, j, k As Integer 'defines the variable For k = 0 To dgv. columnCount-1 'add the header myExcel. cells (1, k + 1) = dgv. columns (k ). headerText Next k For I = 0 To dgv. rowCount-1 'add the data in the control To the table through a loop. For j = 0 To dgv. columnCount-1 'because the first row is the header, when adding data, add myExcel from the second row. cells (I + 2, j + 1) = dgv (j, I ). value Next Return True 'after adding, Return true End Function
Now let's take a look at the running results:
When the attribute AllowUserToAddRow is changed to True, the following error occurs:
Because the cell value is empty, when you perform the. ToString () operation on a control, the System. NullReferenceException will occur. If toString is removed, no error is returned. In addition, you can perform the Tostring () operation to determine whether or not the value is null, the above problem is solved based on the code modification. In addition, the control also has an attribute control to determine whether to automatically add blank lines. The fee system for the VB. NET data center is not complete. It is to be continued ......