2016-07-23 45 views
0

我導入Excel文件GridControl的DevExpress我有沒有問題導入Excel文件網格控制,但是當我將列添加到我的GridControl然後嘗試導入我的excel文件,它將覆蓋那些列,有麻煩的DevExpress GridControl和導入Excel文件

我也試過用CustomColumnUnBoundData事件GridView的,它似乎工作,並添加未綁定列時,它不會覆蓋列,但在點擊按鈕創建兩個c由於某些原因,列名爲[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 

我道歉了很長的帖子我只是不我不知道是什麼觸發了這個事件,所以我不得不徹底解釋它。謝謝

+0

Excel中的列和列的名稱是否相同? –

回答

0

如果您的網格在設置其datasouce時沒有列,網格將自動生成GridColumns。您可以將GridView.OptionsBehavior.AutoPopulateColumns設置爲false以避免此行爲。