Добрый день, сделал телефонный справочник, который берет данные из таблицы excel(по тз), нужно сделать фильтр
1)Мой фильтр ищет только ПОЛНОЕ совпадение ввода в TextBox, Как сделать так, чтобы Фильтрация начиналась уже с первой буквы.
Я начинаю писать Р(пропадает все, что не начинается с р) У(уже останутся только одни руководители) и т.д.
2) И второй вопрос, как сделать так, чтобы он и по цифрам тоже фильтровал ?
Заранее спасибо
C# |
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
| using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;
using System.IO;
using System.Reflection;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
OleDbConnection conn = new OleDbConnection(Excel_Load());
DataSet da = new DataSet();
OleDbDataAdapter adapter = new OleDbDataAdapter();
private static string Excel_Load() //Берем файл excel из папки сборки
{
string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
path = Path.Combine(path, "textfortest.xlsx");
string ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + @"; Extended Properties=""Excel 12.0 Macro;HDR=Yes;ImportMixedTypes=Text;TypeGuessRowsIMEX=1;TypeGuessRows=0""";
return ConnectionString;
}
private void DataView(DataSet da,string filtre ="")
{
dataGridView1.DataSource = da.Tables[0];
DataTableCollection tables = da.Tables;
DataView view1 = new DataView(tables[0]);
BindingSource source1 = new BindingSource();
source1.DataSource = view1;
dataGridView1.DataSource = source1;
if(String.IsNullOrWhiteSpace(filtre)==false)//Фильтр
source1.Filter = "Подразделение = '" + filtre + "' OR ФИО = '" + filtre +"' OR Должность = '" + filtre+"'";
dataGridView1.Columns[0].Width = 55;
dataGridView1.Columns[1].Width = 200;
dataGridView1.Columns[2].Width = 55;
dataGridView1.Columns[3].Width = 70;
dataGridView1.Columns[5].Width = 200;
dataGridView1.Dock = DockStyle.Fill;
}
private void DateBase(OleDbConnection conn, string s ="")
{
conn = new OleDbConnection(Excel_Load());
string Strcmd = "select * from[List$A1:G350]";
OleDbCommand cmd = new OleDbCommand(Strcmd, conn);
try
{
conn.Open();
da.Clear();
adapter.SelectCommand = cmd;
adapter.Fill(da);
DataView(da,s);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
conn.Close();
}
}
private void Form1_Load(object sender, EventArgs e)
{
pictureBox2_MouseClick(new object { }, new EventArgs { });
}
private void pictureBox2_MouseClick(object sender, EventArgs e)
{
DateBase(conn);
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
DateBase(conn,textBox1.Text);
}
}
} |
|