I've got a page that imports a CSV file into a gridview called gvCensus. I can't figure out how to hide (or preferably delete) a column. In a separate sub I try to run gvCensus.Columns.RemoveAt(2) and it tells me the index is out of range. At runtime the column names appear as F0, F1, F2, etc. but I cannot reference them by that name either. Here is my code.
Protected Sub btnLoadCensus_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLoadCensus.Click
Dim DirInfo As New DirectoryInfo(Server.MapPath("UploadedFiles"))
If DirInfo.Exists = False Then
'If folder is not there, Create one.
Directory.CreateDirectory(Server.MapPath("UploadedFiles"))
End If
If btnBrowse.PostedFile IsNot Nothing Then
btnBrowse.PostedFile.SaveAs(Server.MapPath(ResolveUrl("~/UploadedFiles/" & btnBrowse.FileName)))
End If
fileSavedPath = Server.MapPath(ResolveUrl("~/UploadedFiles/" & btnBrowse.FileName))
lblSelectError.Visible = False
stFilePathAndName = btnBrowse.PostedFile.FileName.ToString()
fileName = btnBrowse.FileName
filePath = Path.GetDirectoryName(fileSavedPath)
'Try
Dim MyConnection As System.Data.OleDb.OleDbConnection
Dim DtSet As System.Data.DataSet
Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
MyConnection = New System.Data.OleDb.OleDbConnection _
("provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & filePath & "; Extended Properties=""Text;HDR=No;FMT=Delimited""")
'("provider=Microsoft.Jet.OLEDB.4.0; Data Source='" & stFilePathAndName & "'; Extended Properties=Excel 8.0;")
MyCommand = New System.Data.OleDb.OleDbDataAdapter("select * from [" & fileName & "]", MyConnection)
'("select * from [Sheet1$]", MyConnection)
MyCommand.TableMappings.Add("Table", "TestTable")
DtSet = New System.Data.DataSet
MyCommand.Fill(DtSet)
gvCensus.DataSource = DtSet.Tables(0)
gvCensus.DataBind()
MyConnection.Close()
End Sub
So, instead of doing SELECT * to fill your dataset, select only the columns you want to show in the grid.
Hope that helps.