Solved

VB.NET Hide/Delete column in gridview

Posted on 2014-01-09
8
1,448 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
  • 4
  • 4
8 Comments
 
LVL 25

Expert Comment

by:Luis Pérez
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:schwientekd
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 25

Expert Comment

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

Author Comment

by:schwientekd
Comment Utility
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
Comment Utility
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
Comment Utility
That did it.  Thank you!
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now