⌘BABAKOV.NET Let's code!
100% [dark] [ru]

DataTable, Filters, and Record Count Selection.

This publication is part of a series on what not to do. Instead, do it like this: read MSDN first, then create, not the other way around. And as a result, the problem: "Why is it showing me 56 records? I applied a filter and there should be exactly 1 record!".

So, if you apply a filter to a DataTable, do not do it like this:

DataTable table = new DataTable();
//Здесь - заполняем нашу таблицу чем нибудь...
table.DefaultView.RowFilter = "[поле_таблицы]=значение"; 
//Если значение символьное - то в одинарных ковычках.
int count = table.Rows.Count; 
//Узнаём сколько строк после фильтра неполучится - выдаст общее количество!
string value = table.Rows[0]["поле_таблицы"].ToString();
//Получаем значение ячейки из первой строки (тоже без фильтра сделает).


Do it like this instead:

DataTable table = new DataTable();
//Здесь - заполняем нашу таблицу чем нибудь...
table.DefaultView.RowFilter = "[поле_таблицы]=значение"; 
//Если значение символьное - то в одинарных ковычках.
int count = table.DefaultView.Count; 
//Узнаём сколько строк осталось после наложения фильтра.
string value = table.DefaultView[0]["поле_таблицы"].ToString();
//Получаем значение ячейки из первой строки.



That's it.

P.S.
Square field names in the filter must be enclosed in square brackets, especially if you are applying a filter to multiple fields.

Comments (0)

No comments yet.

Leave a comment
Comment
Images (up to 5 MB)
Your comment will appear after moderation.