[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

VB.NET Hide/Delete column in gridview

Posted on 2014-01-09
8
Medium Priority
?
1,620 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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…
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…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

649 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