Link to home
Start Free TrialLog in
Avatar of mmedi005
mmedi005

asked on

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

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

Avatar of SunnyDark
SunnyDark
Flag of Israel image

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

ASKER

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

It doesn't execute the switch statement.
SOLUTION
Avatar of drypz
drypz
Flag of Philippines image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

>>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?
>>if (row.RowState&DataControlRowState.Normal)
{
...
}


I get this error:

Compiler Error Message: CS0029: Cannot implicitly convert type 'System.Web.UI.WebControls.DataControlRowState' to 'bool'
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)
...
>>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

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial