Solved

Gridview label text changing from dataset population

Posted on 2010-08-20
6
672 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
  • 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
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.
The purpose of this video is to demonstrate how to reset a WordPress password if you are locked out and cannot reset the password. A typical use would be if you cannot access the email to which WordPress would send the password recovery email to…
The purpose of this video is to demonstrate how to Import and export files in WordPress. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Click on Too…

708 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