Avatar of schwientekd
schwientekd
Flag for United States of America asked on

VB.NET Hide/Delete column in gridview

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
ASP.NETVisual Basic.NET.NET Programming

Avatar of undefined
Last Comment
schwientekd

8/22/2022 - Mon
Luis Pérez

I think it would be easier if you directly don't add the column to the dataset, so it will be never be shown in the grid.

So, instead of doing SELECT * to fill your dataset, select only the columns you want to show in the grid.

Hope that helps.
schwientekd

ASKER
The CSV file does not have column names so I'm not sure how I would do that.
Luis Pérez

And how about to remove the column from the DataTable?

MyCommand.Fill(DtSet)
DtSet.Tables(0).Columns.RemoveAt(2) 'Remove the undesired column
gvCensus.DataSource = DtSet.Tables(0)

Hope that helps.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
schwientekd

ASKER
OK, that does work exactly like I asked so I will definitely give you credit for the answer and I very much appreciate the quick responses.  I do however have another problem now.  In this gridview there are a columns for first name and last name.  I was using another sub to combine the first/last names and then want to delete the last name column.  The code you provided deletes the column before I can do this.  Is there a way to remove the column in another sub or should I open a new question.

Again, thank you for you help.
Luis Pérez

Please post your complete code so I can see how are you doing the mixing of names.
schwientekd

ASKER
Here is the sub I am using to combine names.  I just call it after the code block you have already seen (after the line MyConnection.Close).

    Protected Sub MergeName()

        For Each row As GridViewRow In gvCensus.Rows

            'Combine column 1 and 2 into column 1
            Dim lastName As String = row.Cells(1).Text
            Dim firstName As String = row.Cells(2).Text
            row.Cells(1).Text = firstName & " " & lastName

        Next

    End Sub
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
Luis Pérez

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
schwientekd

ASKER
That did it.  Thank you!