Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Gridview label text changing from dataset population

Posted on 2010-08-20
6
Medium Priority
?
686 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 2000 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
Plesk WordPress Toolkit

Plesk's WordPress Toolkit allows server administrators, resellers and customers to manage their WordPress instances, enabling a variety of development workflows for WordPress admins of all skill levels, from beginners to pros.

See why 2/3 of Plesk servers use it.

 

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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The purpose of this video is to demonstrate how to automatically show related posts at the bottom of a blog post in WordPress. This will be demonstrated using a Windows 8 PC. Plugin “Yet Another Related Posts Plugin” will be used. Go to your…
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…

670 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