?
Solved

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

Posted on 2009-02-17
10
Medium Priority
?
353 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

0
Comment
Question by:mmedi005
  • 5
  • 4
10 Comments
 
LVL 8

Expert Comment

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

Author Comment

by:mmedi005
ID: 23664906
Your right...sorry...was in a hurry writing this....

It doesn't execute the switch statement.
0
 
LVL 13

Assisted Solution

by:drypz
drypz earned 400 total points
ID: 23666264
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
        {
        }
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 8

Expert Comment

by:SunnyDark
ID: 23667707
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

0
 

Author Comment

by:mmedi005
ID: 23674414
>>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?
0
 

Author Comment

by:mmedi005
ID: 23674433
>>if (row.RowState&DataControlRowState.Normal)
{
...
}


I get this error:

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

Author Comment

by:mmedi005
ID: 23675690
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

0
 
LVL 8

Expert Comment

by:SunnyDark
ID: 23678318
Try "
if (row.RowState&DataControlRowState.Normal == DataControlRowState.Normal)
...
0
 

Author Comment

by:mmedi005
ID: 23682089
>>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

0
 
LVL 8

Accepted Solution

by:
SunnyDark earned 1600 total points
ID: 23682190
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#...

 
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
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.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…
Suggested Courses

569 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