Dave's Notebook

Assign Multiple enum Values To One Variable

I saw this question and immediately thought, “You can’t! An Enum is an Integer that has been restricted to the values it can accept.”

And I was basically right. But, I forgot that even with an integer you can do the following in CSharp:

image

int i = 1 | 2;

And in VB.NET

Dim i As Integer = 1 Or 2

To end up with a variable i equal to 3 because both do bitwise ands.

So if I had an enumerated value

enum F {
thing1 = 1,
thing2 = 2,
thing3 = 4
}

Or, in VB.NET

Enum F
thing1 = 1
thing2 = 2
thing3 = 4
End Enum

You could then do the following in CSharp:

F fvar;

fvar = F.thing1 | F.thing2;

Or you could do it in VB.NET like this:

Dim fvar As F = F.thing1 Or F.thing2

There’s just one small problem with doing all of this.

If you evaluate fvar, you see that it is equal to 3 because we did not define 3 to be a specific value in our enumeration. However, by adding the Flags attribute to our enum definition:

[Flags]enum F {
thing1 = 1,
thing2 = 2,
thing3 = 4
}

Or

<Flags()> _Enum F
thing1 = 1
thing2 = 2
thing3 = 4
End Enum

fvar will evaluate to:

thing1 | thing2

in CSharp and in VB.NET…

Well, in VB.NET it still evaluates to 3.

ASP.NET GridView Edit All Rows At Once

I just saw a question about this yesterday and realized that while I know how to do this, not everyone does. So, here we go…

Here’s the problem. You want to be able to edit all of the rows in the gridview at once instead of having to switch to edit mode and save one row at the time. Normally, you’d want to do this when only a couple of items need to be changed per row and not the entire row’s worth of data.

image

Photo credit: tico_24 via VisualHunt.com / CC BY

You can do this easily if you make the columns that need to be edited templated columns and place editable controls in them (checkbox, textbox, etc) You can then either make these controls “AutoPostback” controls, or you can provide a control at the bottom of the screen that triggers the update. In either case, the code you are going to write at the codebehind level is going to be the same.

For this example, we are going to assume that you only have one column that needs to be updated and that the control is a checkbox control.

One of the issues you are going to run into with this is that you’ll need to know which row is associated with the control when it is updated. The easiest way I’ve found of dealing with this problem is by adding a HiddenField control and databinding the row Id to it. Since we are dealing with a CheckBox control, you will need to create an event handler for the Checked event. The first parameter that will be passed into this event handler will be the sender. Sender represents the control that fired the event. In this case, it will represent the CheckBox control.

The other control you’ll need to retrieve is the HiddenField control that you placed next to the check box. You can retrieve this control by using the FindControl() method that is hanging off the parent control of the check box. Assuming your HiddenField control is named “_hiddenFieldId” you can get the ID by using:

string id = (HiddenField)(((CheckBox)(Sender)).Parent
.FindControl(“_hiddenFieldId”)).Text;

Now that you have the value of the ID and the value of the checkbox, you can update your database.