-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataTableHelper.cs
More file actions
82 lines (78 loc) · 3 KB
/
DataTableHelper.cs
File metadata and controls
82 lines (78 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace ServerListQueryFormDll
{
public partial class DataTableHelper
{
private static IList<string> _colNameCollection = new List<string>();
private static IDictionary<string, bool> _colCheckStatusIndicator = new Dictionary<string, bool>();
/// <summary>
/// public member,filter datatable with whole index
/// </summary>
/// <param name="source"></param>
/// <param name="filterText"></param>
/// <returns></returns>
public static DataTable GetFilteredDatatable(DataTable source, String filterText)
{
if (source == null) return null;
DataTable dt = CopyDtDef(source.Columns);
ImportDtFilteredData(source, dt, filterText);
return dt;
}
/// <summary>
/// Get a dictionary with all the dt cols included,and set the value to be false;
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
public static IDictionary<string, bool> GetDataTableColumnNameFalseDict(DataTable dt)
{
if (dt == null) return null;
IDictionary<string, bool> dic = new Dictionary<string, bool>();
foreach (DataColumn col in dt.Columns)
{
dic[col.ColumnName] = false;
}
return dic;
}
private static void ImportDtFilteredData(DataTable source, DataTable dt, string filterText)
{
string filter = string.Empty;
if (_colNameCollection.Count == 0) return;
_colNameCollection.ToList().ForEach(colName =>
{
filter += BuildStringTypeColFilter(source, colName, filterText);
});
filter = filter.EndsWith("or") ? filter.Substring(0, filter.Length - 2).Trim() : filter;
source.Select(filter).ToList().ForEach(row =>
{
dt.ImportRow(row);
});
}
private static string BuildStringTypeColFilter(DataTable source, string colName, string filterText)
{
if (source.Columns[colName].DataType == typeof(string))
{
return string.Format("{0} like '%{1}%' or", colName, filterText);
}
return string.Empty;
}
private static DataTable CopyDtDef(DataColumnCollection dataColumnCollection)
{
DataTable retDt = new DataTable();
InitColNameCollection(dataColumnCollection, retDt);
return retDt;
}
private static void InitColNameCollection(DataColumnCollection dataColumnCollection, DataTable retDt)
{
_colNameCollection.Clear();
foreach (DataColumn column in dataColumnCollection)
{
retDt.Columns.Add(new DataColumn(column.ColumnName));
_colNameCollection.Add(column.ColumnName);
}
}
}
}