Solved

VB.NET Hide/Delete column in gridview

Posted on 2014-01-09
8
1,554 Views
Last Modified: 2014-01-09
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
0
Comment
Question by:schwientekd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 25

Expert Comment

by:Luis Pérez
ID: 39768296
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.
0
 

Author Comment

by:schwientekd
ID: 39768323
The CSV file does not have column names so I'm not sure how I would do that.
0
 
LVL 25

Expert Comment

by:Luis Pérez
ID: 39768363
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.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:schwientekd
ID: 39768435
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.
0
 
LVL 25

Expert Comment

by:Luis Pérez
ID: 39768445
Please post your complete code so I can see how are you doing the mixing of names.
0
 

Author Comment

by:schwientekd
ID: 39768469
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
0
 
LVL 25

Accepted Solution

by:
Luis Pérez earned 500 total points
ID: 39768523
Then you can do this:

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

Open in new window


And modify your MergeNames sub:

Protected Sub MergeName(ByVal ds As DataSet)
    For Each row As DataRow In ds.Tables(0).Rows
        Dim lastName As String = row.Item(1)
        Dim firstName As String = row.Item(2)
        row.Item(1) = firstName & " " & lastName
    Next
End Sub

Open in new window


Hope that helps.
0
 

Author Comment

by:schwientekd
ID: 39768559
That did it.  Thank you!
0

Featured Post

Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question