Solved

Gridview label text changing from dataset population

Posted on 2010-08-20
6
684 Views
Last Modified: 2012-08-13
I have a gridview that displays a lable from a database field.  The gridview is populated by a dataset but there is a value that will be returned occasionally that I don't want to display - I want to catch it in an if statement and manually change it to something else.  I don't know how to find that specific record as it comes up and target that row of the gridview to find that label and change the value - hopefully that makes sense and someone will know how to do that.
0
Comment
Question by:rondre
[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
  • 2
6 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 33489557
You need to look at handling the RowDataBound event of the GridView. You can then access the e.Row.DataItem property of the event args to interrogate the data associated with the current row and override its value if you need to.
0
 

Author Comment

by:rondre
ID: 33513689
Do you have a site link that has an example of this?  I will play around with this and see if I can fiugre it out myself as well, but an example would be helpful.

thanks.
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 33513997
This is a quick example of the kind of thing you would need to do (obviously you need to change the text values and also the column index to match your own data):


        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            // check we are dealing with a data row
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                // retrieve the data associated with the row
                System.Data.DataRowView row = e.Row.DataItem as System.Data.DataRowView;
                if (row != null)
                {
                    // check the value of the second field 
                    if (row[1].ToString() == "Wibble")
                    {
                        // we don't like it so change the value displayed to something else 
                        e.Row.Cells[1].Text = "Something Else";
                    }
                }
            }
        }

Open in new window

0
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 

Author Comment

by:rondre
ID: 33516899
That example definitely helps - I've been able to use response.write to find the rows I need - however the problem I'm running into now is the line:

e.Row.Cells[1].Text = ...

If I write to the screen the cell[0] there is nothing there, anything higher than 0 I get and error:

Specified argument was out of the range of valid values.
Parameter name: index

I'm not sure how to modify that value now that I've found it - can you help me further?

Thanks
0
 

Author Comment

by:rondre
ID: 33517203
k - I have the code setups as attached, but the problem now is it's replacing the whole output with just the "All Members" portion - is there a way to just hit the one label value to change?


protected void gvShit_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        // check we are dealing with a data row
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            // retrieve the data associated with the row
            System.Data.DataRowView row = e.Row.DataItem as System.Data.DataRowView;
            if (row != null)
            {
                // check the value of the second field 
                if (row[2].ToString() == "")
                {
                    Response.Write(row[0] + " " + row[1] + " " + row[2] + " " + row[3] + "<br />");
                    Response.Write("<br />cells:" + e.Row.Cells[0].Text + " after cell<br />");
                    // we don't like it so change the value displayed to something else 
                    e.Row.Cells[0].Text = "All Members";
                }
            }
            else
            {
                Response.Write("If statement not verified<br />");
            }
        }
    }

Open in new window

0
 

Author Comment

by:rondre
ID: 33517944
I finally got this working, thanks for pointing me in the right direction!
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

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…
When setting up new project requests for our site, one of the most powerful tools our team has available to use is Axure (http://www.axure.com/). It’s a tool for creating software and web prototypes that can function and interact as if it were the a…
The purpose of this video is to demonstrate how to create a Printer Friendly PDF on a WordPress Page. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome Screenshot” Google Chrome Extension, and SmallPDF.com Log…
The purpose of this video is to demonstrate how to properly insert a Vimeo Video into a WordPress site or Blog. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp…

752 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