Possible for datagrid

albertkhor

Well-known member
Joined
Jan 12, 2006
Messages
150
Programming Experience
Beginner
Possible for datagrid (solve)

just want to ask is there possible to set the datagrid header?
below is my code, but it cannot work! pls help me to check, thx!

VB.NET:
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] ExcelConn()[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] DSexcel [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Data.DataSet
[/SIZE][COLOR=seagreen][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2][COLOR=#000000] ExcelCommand [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#000000] System.Data.OleDb.OleDbDataAdapter[/COLOR]
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2][COLOR=black] ExcelConnection[/COLOR] [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [COLOR=black]System.Data.OleDb.OleDbConnection[/COLOR][/SIZE]
[SIZE=2][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2][COLOR=#000000] dtImport [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#000000] DataTable = [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Nothing
[/COLOR][/SIZE][/SIZE][/COLOR][COLOR=seagreen]'HDR=No is to let the engine know no header; IMEX=1 is to let all the data convert to string[/COLOR]
[SIZE=2]ExcelConnection = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] System.Data.OleDb.OleDbConnection( _
"provider=Microsoft.Jet.OLEDB.4.0; " & _
"data source=" & OpenFileDialog1.FileName & " ; " & _
"Extended Properties=""Excel 8.0;HDR=No;IMEX=1""")
ExcelCommand = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", ExcelConnection)
DSexcel = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] System.Data.DataSet
ExcelCommand.Fill(DSexcel)
dtImport = DSexcel.Tables(0)
ExcelConnection.Close()
[COLOR=blue]End Sub[/COLOR]
[/SIZE]
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] [SIZE=2]LoadHeader()
[/SIZE][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] aryColumnHeader [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] ArrayList
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] ts [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] DataGridTableStyle
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] colName [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE]
 
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#008000]'to get the value which row on DataTable will become datagrid header
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] rowNum [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#0000ff]CInt[/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2]NumbericUpDown.Value)
[/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] i [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = 0 [/SIZE][SIZE=2][COLOR=#0000ff]To[/COLOR][/SIZE][SIZE=2] DataTable.Columns.Count - 1
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] column [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] DataGridTextBoxColumn = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] DataGridTextBoxColumn[/SIZE]
[SIZE=2][COLOR=seagreen]'default datagrid column header is F1, F2, ... etc[/COLOR]
column.MappingName = "F" & i + 1
colName = dtImport.Rows(rowNum - 1).Item(i).ToString
[COLOR=seagreen]'check whether colName have value or not [/COLOR]
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] colName = "" [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]colName = "Column" & i + 1
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2]col.HeaderText = colName
aryColumnHeader.Add(colName)
AddComboboxItem(colName)
AutoMappping(colName, i + 1)
ts.GridColumnStyles.Add(col)
[/SIZE][SIZE=2][COLOR=#0000ff]Next[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff][COLOR=seagreen][SIZE=2][COLOR=#008000]'selected row to become header name will be delete!
[/COLOR][/SIZE][/COLOR][/COLOR][/SIZE][SIZE=2]dtImport.Rows.RemoveAt(rowNum - 1)
 
[/SIZE][SIZE=2][COLOR=#0000ff][COLOR=blue]Me[/COLOR][/COLOR][/SIZE][SIZE=2][COLOR=blue].[COLOR=black]DataGrid1.TableStyles.Add(ts)[/COLOR][/COLOR][/SIZE]
[COLOR=blue]Me[/COLOR][SIZE=2].DataGrid1.DataSource = dtImport[/SIZE]
[SIZE=2][COLOR=blue]End Sub[/COLOR]
[/SIZE]
 
Last edited:
Back
Top