DevExpress GridControl 和导入 Excel 文件时遇到问题
Posted
技术标签:
【中文标题】DevExpress GridControl 和导入 Excel 文件时遇到问题【英文标题】:Having trouble With DevExpress GridControl and importing Excel file 【发布时间】:2016-07-23 16:35:06 【问题描述】:我正在将一个 excel 文件导入到 DevExpress 中的 GridControl 我将 excel 文件导入到网格控件没有问题,但是当我向我的 GridControl 添加列时strong>GridControl 然后尝试导入我的 excel 文件,它会覆盖那些列,
我也尝试使用 GridView 中的 CustomColumnUnBoundData 事件,它似乎可以工作,并且在添加未绑定列时它不会覆盖列但是当单击按钮时为某些名为 [NAME] 的原因创建两列,并查询行值两次。
VB 示例:
Private Sub SimpleButton2_Click(sender As System.Object, e As System.EventArgs) Handles SimpleButton2.Click, GridView1.DataSourceChanged
OFD.ImportMe(GridControl1, TextEdit1)
view = GridControl1.MainView
'Dim Columns As GridColumn
GridColumn1 = GridView1.Columns.AddField("NAME")
GridColumn1.VisibleIndex = view.Columns.Count
End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub GridView1_CustomUnboundColumnData(sender As Object, e As DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs) Handles GridView1.CustomUnboundColumnData
Dim Fields() As String = "NAME", "DESCRIPTION", "STOCK", "ORDERED"
GridColumn1.VisibleIndex = GridView1.Columns.Count - 1
For i As Integer = 0 To GridView1.Columns.Count
If e.Column.FieldName = Fields(0) AndAlso e.IsGetData Then
e.Value = e.ListSourceRowIndex
End If
' OFD.Listing(e.ListSourceRowIndex)
Next
End Sub
我在不同的类中建立了连接:
VB 类:
Public Function openMe(ByVal path As TextEdit)
Dim ofd As New OpenFileDialog
ofd.Filter = "Excel Files (*.xls)|*.xls|All Files (*.)|*.*"
If ofd.ShowDialog() = DialogResult.OK Then
path.Text = ofd.FileName
End If
Return 0
End Function
Public Function ImportMe(ByVal gc As GridControl, ByVal file As TextEdit)
Dim Connect As OleDbConnection
Dim Command As OleDbDataAdapter
Dim con As String
con = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + file.Text + ";Extended Properties=Excel 8.0"
Connect = New OleDbConnection(con)
Command = New OleDbDataAdapter("Select * from [Sheet1$]", Connect)
DD = New DataSet()
Command.Fill(DD)
gc.DataSource = DD.Tables(0)
Connect.Close()
Return DD
End Function
Public Function Listing(ByVal sender As Object, ByVal listSource As Integer)
Dim DR As DataRow = DD.Tables("NAME").Rows(listSource)
Return DR
End Function
我为这篇长文道歉,我只是不知道是什么触发了这个事件,所以我不得不彻底解释它。谢谢
【问题讨论】:
你的列名和 Excel 中的列名一样吗? 【参考方案1】:如果您的网格在设置数据源时没有列,则网格将自动生成 GridColumns。您可以将 GridView.OptionsBehavior.AutoPopulateColumns 设置为 false 以避免这种行为。
【讨论】:
以上是关于DevExpress GridControl 和导入 Excel 文件时遇到问题的主要内容,如果未能解决你的问题,请参考以下文章
Devexpress GridControl 设置combobox下拉框
如何在GridControl中显示图片列?控件DevExpress.XtraGrid.GridControl中显示图片列。