Gridview label text changing from dataset population

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.
rondreAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Carl TawnConnect With a Mentor Systems and Integration DeveloperCommented:
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
 
Carl TawnSystems and Integration DeveloperCommented:
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
 
rondreAuthor Commented:
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
rondreAuthor Commented:
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
 
rondreAuthor Commented:
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
 
rondreAuthor Commented:
I finally got this working, thanks for pointing me in the right direction!
0
All Courses

From novice to tech pro — start learning today.