Stumped on trying to get gridview cell contents

Posted on 2011-10-20
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

    Author Comment

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

    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.
    LVL 38

    Accepted Solution

    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

    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

    Great explanation.
    LVL 38

    Expert Comment

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

    Thanks for the points.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    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…
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    728 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

    20 Experts available now in Live!

    Get 1:1 Help Now