We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

How do I manipulate a control during the Databound event in a GridView?

mmedi005
mmedi005 asked
on
Medium Priority
377 Views
Last Modified: 2012-06-21
How do I manipulate a control during the Databound event in a GridView?

I am doing this in the code below. How do I get this to work?
protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e) 
    {
        GridViewRow row = e.Row;
        int dataKeyIndex;
        string address;
 
        if (row.RowType == DataControlRowType.DataRow)
        {
            Panel p = (Panel)row.FindControl("uxAddressPanel");
 
            dataKeyIndex = row.DataItemIndex;
            address = GridView.DataKeys[dataKeyIndex].Values["Address"].ToString();
 
            switch (row.RowState)
            {
                case DataControlRowState.Normal:
 
                    if ((address != null) && (address != ""))
                        p.Visible = true;
                    else
                        p.Visible = false;
 
                    break;
 
                case (DataControlRowState.Edit | DataControlRowState.Selected):
 
                    RadioButtonList r = (RadioButtonList)row.FindControl("AddRadioButtonList");
                    
                    if ((address != null) && (address != ""))
                    {
                        p.Visible = true;
                        r.SelectedValue = "True";
                    }
                    else
                    {
                        p.Visible = false;
                        r.SelectedValue = "False";
                    }
                        
                    break;
            }
        }
    }

Open in new window

Comment
Watch Question

It would be much easier to answer if you told us what in the code doesn't work...
That error are you getting?

Author

Commented:
Your right...sorry...was in a hurry writing this....

It doesn't execute the switch statement.
Commented:
Enum type sometimes don't work with switch statement. Why don't you just use if-else statement?

        if (e.Row.RowState == DataControlRowState.Normal)
        {
        }
        else if ((e.Row.RowState == DataControlRowState.Edit) || (e.Row.RowState == DataControlRowState.Selected))
        {
        }
        else
        {
        }

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
The state is a bitwise combination...
So to check for a state you would use a bitwise comparison like this:

if (row.RowState&DataControlRowState.Normal)
{
...
}

Open in new window

Author

Commented:
>>Enum type sometimes don't work with switch statement. Why don't you just use if-else statement?

Tried and it still doesn't execute either condition

>>if (row.RowState&DataControlRowState.Normal)

This fails

Any ideas?

Author

Commented:
>>if (row.RowState&DataControlRowState.Normal)
{
...
}


I get this error:

Compiler Error Message: CS0029: Cannot implicitly convert type 'System.Web.UI.WebControls.DataControlRowState' to 'bool'

Author

Commented:
k, got it to almost work...

used DataControlRowState.Alternate with Normal to get these rows in a normal state,

But when I click Edit I get every other row to work...how do I get the Alternate row in Edit mode?
if (row.RowState == DataControlRowState.Normal || row.RowState == DataControlRowState.Alternate)
{
...
}
else if (e.Row.RowState == DataControlRowState.Edit)
{
...
}

Open in new window

Try "
if (row.RowState&DataControlRowState.Normal == DataControlRowState.Normal)
...

Author

Commented:
>>if (row.RowState&DataControlRowState.Normal == DataControlRowState.Normal)
...

I get an error:

Compiler Error Message: CS0019: Operator '&' cannot be applied to operands of type 'System.Web.UI.WebControls.DataControlRowState' and 'bool'

When I click Edit. All odd rows are executing the statements within the if statement.  All even rows are not.

This is when I have the code below as the if statement.

if (row.RowState == DataControlRowState.Edit)
{
....
}

Open in new window

Sorry for the multiple tries...
But:
if ((row.RowState&DataControlRowState.Normal) == DataControlRowState.Normal)
Should do the trick...
Problem is that I dont have an access to VS so this is from head...
Try reading how to compare bitwise fields in C#...

 
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.