This post will provide an extension method for the
IEnumerable interface to perform an action on each element which belong to a derived type of
T. Using this method you can invoke actions on a part of a collection with just one line of code.
While developing my project, I was left without Internet access for a week, and I couldn’t search for an easy and already written solution for my problem, so I decided to create my own solution.
I had a
DataGridView on one of my forms with a
DataSource linked to an SQLserver through LINQ to SQL and I wanted to be able to filter the result rows, based on user input. Since the clients (the users of my program) will connect to the database through the Internet, some of them via poor connections, I didn’t want to make a new query with each change of the filter. I found out that the
DataGridViewRow has a
Visible property, and it looked like an efficient way to set this according to the filters. So I created a new class inheriting from the
DataGridView, and added some extra functionality to support filtering.
My solution consists of two classes:
In my project, I had a
TextBox on one of the forms for entering quantity, and I thought it would be a cool feature if it could understand such basic expressions as 10+42 or 4*8 and so on…
So I decided to create a new Control derived from the TextBox, because this could be useful in the future, and now I’m sharing my result. See an example on the following screenshot:
First of all, since I decided to create a new control, I wanted to make it as flexible as possible, so the expressionsthat can be understood by the control can include the four basic operations (+, -, *, /) and parentheses, in any valid combination (spaces can be used, they will be skipped).
e.g.: 40+2 or (12+3/ 4) * 4-3*3 or 10+2*(2+5* (4-(2-0.5))+1.5)
Second, I added some extra functionality to the
TextBox, some are taken from
NumericUpDown control, but there are some which I used already but now I built them in the control (changing background color based on value).