?
Solved

VB.NET Hide/Delete column in gridview

Posted on 2014-01-09
8
Medium Priority
?
1,579 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
Application Discovery Service in AWS

In the era of the cloud, customers migrating away from their existing on-premise infrastructure. This requires lots of planning, strategies, and effort to identify their existing resources and determine how best to migrate.  Datacenter migrations happen in four phases -

 

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 2000 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

Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

771 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