[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

making individual gridview header columns invisible

Posted on 2011-10-16
18
Medium Priority
?
332 Views
Last Modified: 2012-05-12
The attached code does what I want with my autogenerated grid (hides the first column) EXCEPT
for the header row.   I cannot seem to find how that is done, so everything in the following rows appears to move to the left.

How do I also make the header column invisible?

Thanks!
Public Sub FormatGrid(ByRef dbgrid As GridView,
                          ByVal str_format As String)

        Dim introw As Integer
        Dim introwcnt As Integer

        Try
            introwcnt = dbgrid.Rows.Count

            For introw = 0 To dbgrid.Rows.Count - 1

                dbgrid.Rows(introw).Cells(Me.ColumnsByNumber_RECORDID).Visible = False


            Next




        Catch ex As Exception

            str_format = "stop here"

        End Try




    End Sub

Open in new window

0
Comment
Question by:codefinger
  • 6
  • 5
  • 4
  • +1
17 Comments
 
LVL 38

Assisted Solution

by:Tom Beck
Tom Beck earned 1000 total points
ID: 36977294
The auto-generated header produces white space ( ) so you will continue to have the small column showing.

Instead, add a RowCreated event for the GridView and code it something like this:

Protected Sub dbgrid_RowCreated(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles dbgrid.RowCreated
        If e.Row.Cells.Count > 3 Then
            e.Row.Cells(4).Attributes.Add("style", "display:none")
        End If
    End Sub

Adjust the index and conditional so you hide the correct column.
0
 
LVL 12

Expert Comment

by:Ramkisan Jagtap
ID: 36977840
if you want to hide whole column then you can write following line after binding the gridview

dbgrid.Columns(Me.ColumnsByNumber_RECORDID).Visible = False

Hope this will help you. :)
0
 

Author Comment

by:codefinger
ID: 36978336
ramkisan:

Auto-generated grids have no columns that can be accessed by code.  Try it out and check in the debugger....columns.count will ALWAYS be zero, so the code you provided causes an "out of range" exception.

Unless I am missing something?

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 12

Expert Comment

by:Ramkisan Jagtap
ID: 36978367
@codefinger: Sorry for wrong suggestion!! You are right it will not work.
Try to add following code on RowDataBound Event of gridview

Protected Sub dbgrid_RowDataBound(sender As Object, e As GridViewRowEventArgs)

      If e.Row.RowType = DataControlRowType.Header Then
            e.Row.Cells(Me.ColumnsByNumber_RECORDID).Visible = False
      End If
End Sub
0
 

Author Comment

by:codefinger
ID: 36978389
tommyboy:

 Your solution works, but before I accept and assign points, I was wondering, this code runs after the grid loads so I don't have to guess what cells have what number:
 
Public Sub NameColumns()

        Dim cnt As Integer


        Dim str_columnId As String = Nothing

        For cnt = 1 To Me.GridViewRegistrants.Rows(0).Cells.Count - 1

            str_columnId = DirectCast(Me.GridViewRegistrants.Rows(0).Cells(cnt), System.Web.UI.WebControls.DataControlFieldCell).ContainingField.ToString

            Select Case str_columnId.ToUpper

                Case "RECORDID"

                    Me.ColumnsByNumber_RECORDID = cnt

                Case "FIRSTNAME"

                    Me.ColumnsByNumber_FIRSTNAME = cnt

                Case "LASTNAME"

                    Me.ColumnsByNumber_LASTNAME = cnt

                Case "CLINICNO"

                    Me.ColumnsByNumber_CLINICNO = cnt

                Case "ADDRESS1"
                    Me.ColumnsByNumber_ADDRESS1 = cnt

                Case "ADDRESS2"
                    Me.ColumnsByNumber_ADDRESS2 = cnt

                Case "CITY"
                    Me.ColumnsByNumber_CITY = cnt

                Case "STATE"
                    Me.ColumnsByNumber_STATE = cnt

                Case "ZIP"
                    Me.ColumnsByNumber_ZIP = cnt

                Case "PHONE_NUMBER"
                    Me.ColumnsByNumber_PHONE_NUMBER = cnt

                Case "SSN"
                    Me.ColumnsByNumber_SSN = cnt

                Case "EMAIL"
                    Me.ColumnsByNumber_EMAIL = cnt

                Case "GENDER"
                    Me.ColumnsByNumber_GENDER = cnt

                Case "DOB"
                    Me.ColumnsByNumber_DOB = cnt

                Case "STATUS"
                    Me.ColumnsByNumber_STATUS = cnt

                Case "STATUS_UPDATED_ON"
                    Me.ColumnsByNumber_STATUS_UPDATED_ON = cnt

                Case "STATUS_UPDATED_BY"
                    Me.ColumnsByNumber_STATUS_UPDATED_BY = cnt

                Case "EPIC_ACT_CODE"
                    Me.ColumnsByNumber_EPIC_ACT_CODE = cnt

                Case "NOTES"
                    Me.ColumnsByNumber_NOTES = cnt

                Case "DENIAL_EMAIL_SENT"
                    Me.ColumnsByNumber_DENIAL_EMAIL_SENT = cnt

                Case "REJECTED_EMAIL_SENT"
                    Me.ColumnsByNumber_REJECTED_EMAIL_SENT = cnt

            End Select
        Next

    End Sub

Open in new window


Is there a way I can accomplish the same thing before the row_created event to avoid all the trial and error and counting its going to take me to figure out which numbers to use for the cell indexes?  (I tried the databound event of the grid, no dice.)

Probably impossible, but it never hurts to ask!

Thanks!

0
 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 1000 total points
ID: 36978480
You can try something like below


Protected Sub GridView1_RowCreated(sender As Object, e As GridViewRowEventArgs)
	'Just set the Column Name that you wish to hide based on your requirements
	For Each cell As TableCell In e.Row.Cells
		Dim field As BoundField = DirectCast(DirectCast(cell, DataControlFieldCell).ContainingField, BoundField)
		If field.DataField = "ColumnName" Then
			field.Visible = False
		End If
	Next
End Sub

Open in new window

0
 
LVL 38

Expert Comment

by:Tom Beck
ID: 36978892
There is a System.Web.UI.WebControls.DataControlHeaderCell class. Perhaps you need to manipulate that as well as the System.Web.UI.WebControls.DataControlFieldCell to hide the cell using your method.

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.datacontrolfieldheadercell.aspx

0
 

Author Comment

by:codefinger
ID: 36979353
CodeCruiser:

Thought you had something there, but turns out that approach somehow makes every single cell in every single row invisible.



0
 

Accepted Solution

by:
codefinger earned 0 total points
ID: 36979587
CodeCruiser and TommyBoy  -- thanks!

I was able to combine the best of your suggestions and the code in this comment does exactly what I want it to do...
Private Sub GridViewRegistrants_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridViewRegistrants.RowCreated

        Dim cnt As Integer

        Dim str_columnId As String = Nothing

        For cnt = 1 To e.Row.Cells.Count - 1
            str_columnId = DirectCast(e.Row.Cells(cnt), System.Web.UI.WebControls.DataControlFieldCell).ContainingField.ToString
            Select Case str_columnId.ToUpper
                Case "RECORDID"
                    Me.ColumnsByNumber_RECORDID = cnt
                Case "FIRSTNAME"
                    Me.ColumnsByNumber_FIRSTNAME = cnt
                Case "LASTNAME"
                    Me.ColumnsByNumber_LASTNAME = cnt
                Case "CLINICNO"
                    Me.ColumnsByNumber_CLINICNO = cnt
                Case "ADDRESS1"
                    Me.ColumnsByNumber_ADDRESS1 = cnt
                Case "ADDRESS2"
                    Me.ColumnsByNumber_ADDRESS2 = cnt
                Case "CITY"
                    Me.ColumnsByNumber_CITY = cnt
                Case "STATE"
                    Me.ColumnsByNumber_STATE = cnt
                Case "ZIP"
                    Me.ColumnsByNumber_ZIP = cnt
                Case "PHONE_NUMBER"
                    Me.ColumnsByNumber_PHONE_NUMBER = cnt
                Case "SSN"
                    Me.ColumnsByNumber_SSN = cnt
                Case "EMAIL"
                    Me.ColumnsByNumber_EMAIL = cnt
                Case "GENDER"
                    Me.ColumnsByNumber_GENDER = cnt
                Case "DOB"
                    Me.ColumnsByNumber_DOB = cnt
                Case "STATUS"
                    Me.ColumnsByNumber_STATUS = cnt
                Case "STATUS_UPDATED_ON"
                    Me.ColumnsByNumber_STATUS_UPDATED_ON = cnt
                Case "STATUS_UPDATED_BY"
                    Me.ColumnsByNumber_STATUS_UPDATED_BY = cnt
                Case "EPIC_ACT_CODE"
                    Me.ColumnsByNumber_EPIC_ACT_CODE = cnt
                Case "NOTES"
                    Me.ColumnsByNumber_NOTES = cnt
                Case "DENIAL_EMAIL_SENT"
                    Me.ColumnsByNumber_DENIAL_EMAIL_SENT = cnt
                Case "REJECTED_EMAIL_SENT"
                    Me.ColumnsByNumber_REJECTED_EMAIL_SENT = cnt
            End Select
        Next

        ' Now hide the columns we don't want to see:
        e.Row.Cells(Me.ColumnsByNumber_RECORDID).Visible = False




    End Sub

Open in new window

0
 

Author Closing Comment

by:codefinger
ID: 37010515
Used suggestions from TommyBoy and CodeCruiser for my solution.  solution is my final comment...
0
 
LVL 38

Expert Comment

by:Tom Beck
ID: 36979771
@codefinger,

First of all, if you used a combination of our suggestions then split the points.

Secondly, you were too quick to close the question. I have a solution that works with your original code. Instead of looping through all the rows to make the Me.ColumnsByNumber_RECORDID column invisible in each row, all you had to do was apply the correct column number to the GridView at the end.

Me.GridViewRegistrants.Columns(Me.ColumnsByNumber_RECORDID).Visible = False

No need to use the RowCreated event.

Now, are you going to hit the request attention or am I going to object to you closing the question without awarding points?
0
 
LVL 38

Expert Comment

by:Tom Beck
ID: 36986950
@SouthMod,

I know, I messed up, thanks for pointing that out. At least I caught it before hitting the 'Object' button. There should be some kind of "Recall my comment" button on this forum.

My apologies to @codefinger.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 36988234
@tommyBoy

> I have a solution that works with your original code.

It appears that you did not notice http:#36977840 and http:#36978336
0
 
LVL 38

Expert Comment

by:Tom Beck
ID: 36988967
@CodeCruiser,

Ok, I missed that too. I guess I was asleep at the wheel for the entire question.

But why are you objecting to the closing of the question?
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 36995277
I think the proposed acceptance of the comments is appropriate as http:#36979587 shows the final code and you and me are getting the points. What's wrong with that?
0
 
LVL 38

Expert Comment

by:Tom Beck
ID: 36997592
Nothing wrong with that. Just wondering why your comment comes through as an "Objection Posted" rather than just a comment. Does it always work like that?
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 37002595
Yours comes the same way to me :-) something wrong with EE emailing stuff.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month20 days, 10 hours left to enroll

868 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