Stumped on trying to get gridview cell contents

Posted on 2011-10-20
Medium Priority
Last Modified: 2012-05-12
Boy am I stumped on this one.

I am dynamically populating a GridView like this:
Where mdt is a DataTable and dr is a datarow -

dr = mdt.NewRow()
'fill in the dr cols
gvMoveDetails.DataSource = mdt

This works fine for the columns that contain characters, but does not seem to work for numerics.
Example (col definitions between dr and gvMoveDetails are the same):
I can see that dr(11) has "2" in it before the databind is executed.
After the databind is executed, that same column in the gridview has spaces instead of a "2":
gvMoveDetails.Rows(0).Cells(11).Text = ""

fyi...The value is in the cell itself and not within a control that lives in the cell.
Another key point....The cells I am trying to extract from the gridview has visible=false.  

Any ideas what I  might be doing wrong?
Question by:ToolTimeGang
  • 4
  • 3

Author Comment

ID: 37002977
Ok.  Nevermind. It was the the visibility=false on the gridview column.  Now to figure a work around...I'll keep this open until I get that part figured out.  Then I will post it.

But if anyone has a slick solution for this in the meantime, can you please share?

Thank you!
LVL 38

Expert Comment

by:Tom Beck
ID: 37003323
If there are columns you want to make invisible, it's best accomplished in the RowCreated event like this:

Protected Sub GridView1_RowCreated(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated
            e.Row.Cells(1).Attributes.Add("style", "display:none")
    End Sub

Author Comment

ID: 37003435
Thank you tommyBoy (love that movie too!).
I ended up coding this:
If e.Row.RowType = DataControlRowType.DataRow OrElse
            e.Row.RowType = DataControlRowType.Header Then
            e.Row.Cells(Cols.SubType).Visible = False
End if

And it is working.
But would the "RowCreated" event be a better place?  If so, why is it better than RowDataBound?
I know that with RowCreated, the databinding hasn't taken place yet.
With RowDataBound, it has.
Thx again...Michele.

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

LVL 38

Accepted Solution

Tom Beck earned 2000 total points
ID: 37004303
I think RowCreated is the more logical place because that's where the html controls that display the data are created. It just feels right to me to add any attributes I need to the html controls as they are created rather than during RowDataBound which is after the fact. But, as you have seen, making changes to the html controls will work in either event. Of course, it doesn't work the other way. You cannot manipulate the data in RowCreated. In the RowCreated event, this one line affects the whole column including header: e.Row.Cells(1).Attributes.Add("style", "display:none"). No need for the two DataControlRowTypes in the conditional. And of course, you could have changed my one line to: e.Row.Cells(1).Attributes.Add("style", "visibility:hidden") if that's the style you wanted.

I don't believe I've ever seen the entire movie but I can picture the actor that plays the lead. BTW, I don't look like him. I'm 6' 4", 190.

Author Comment

ID: 37006364
Great!  Thanks for that explanation.  Makes more sense for me now.

Favorite line "That's gonna leave a mark!".   Now you will have to see the whole thing to appreciate that!

Thanks again tommyBoy!

Author Closing Comment

ID: 37006371
Great explanation.
LVL 38

Expert Comment

by:Tom Beck
ID: 37006551
That line was Tommy Boy? I thought it was Ed Grimley.

Thanks for the points.

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Loops Section Overview
Suggested Courses
Course of the Month17 days, 6 hours left to enroll

864 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