Hello. I have such a task: I have a gridcontrol and there is a DataTable, which for the grid is a DataSource. The table is a compilation of some data on travel. I want to add the ability to filter data. Suppose I have three combo boxes - users, trip status, city of departure. When you select a value in each box, the filter for the data set changes. Unfortunately, nothing smarter than about this option, I did not come up with:

DataView dv = new DataView(Tables.dtTrips); dv.RowFilter = (!string.IsNullOrEmpty(dv.RowFilter) ? dv.RowFilter + " and " : string.Empty) + "id_trip_state='" + id_state.ToString() + "'"; 

And such a code to change the value in each box. There can be a lot of filters, they can change, or they can be added after they are added. Accordingly, the question is whether the path is right. If it is correct, the second question - let's say we selected the user in the box, added a string to the filter "id_natural = '" + u_id.tostring () + "'". if you then change the user again, then the same line but with a different ID will simply add what is wrong. Accordingly, it was decided to use a regular expression that will find the value in single quotes after id_natural =, but as I did not try to write a regular schedule, I have not yet succeeded

    1 answer 1

    If anyone is interested, he eventually wrote a class for this purpose: Here it is specifically for my requirements.

     public static class TripFilters { static Dictionary<string, string> trips_filters = new Dictionary<string, string>(); // Π”ΠΎΠ±Π°Π²Π»Π΅Π½ΠΈΠ΅, ΠΈΠ·ΠΌΠ΅Π½Π΅Π½ΠΈΠ΅, ΠΈΠ»ΠΈ ΡƒΠ΄Π°Π»Π΅Π½ΠΈΠ΅ записи public static void Refresh(string key,string value) { if (string.IsNullOrEmpty(value)) { trips_filters.Remove(key); return; } if (!trips_filters.ContainsKey(key)) trips_filters.Add(key, value); else trips_filters[key] = value; } // ΠŸΠΎΠ»ΡƒΡ‡Π°Π΅ΠΌ ΠΈΡ‚ΠΎΠ³ΠΎΠ²Ρ‹ΠΉ Ρ„ΠΈΠ»ΡŒΡ‚Ρ€ для DataView public static string GetFilterString() { string res = string.Empty; string condition = string.Empty; foreach (var x in trips_filters) { switch (x.Key) { case "date_from": condition = ">="; break; case "date_to": condition = "<="; break; default: condition = "="; break; } res += (string.IsNullOrEmpty(res) ? string.Empty : " and ") + string.Format("{0}{1}'{2}'",x.Key.Contains("date")?"date":x.Key,condition,x.Value); } return res; } }