运行 搜索整个 table
Run search in entire table
我的程序运行,客户端输入他要查找的内容并在 SQL 数据库中搜索。当他点击搜索时,它出现在数据网格视图中。
我希望客户输入任何单词,单词的开头或结尾,而不是在一列中查找,而是在整个 table.
中查找
我该如何编程?
clsdatasource.search 获取文本框中的内容并将其存储在变量中。
DataView myview = new DataView(mytable);
myview.RowFilter = "CUSTOMER='" + clsDataSource.search + "'";
dataGridView1.DataSource = myview;
我尝试了什么:
for (int i = 0; i < myset.Tables[0].Columns.Count; i++) {
DataRow[] MyDR = myset.Tables[0].Select(myset.Tables[0].Columns[i].ToString() + "='" + clsDataSource.search + "'");
if (MyDR.Length > 0) {
dataGridView1.DataSource = myview;
}
}
试试这个:
public partial class Form1 : Form
{
DataGridView dataGridView;
DataTable dataTable;
DataView dataView;
TextBox textBoxSearch;
public Form1()
{
//InitializeComponent();
Width = 400;
dataGridView = new DataGridView { Parent = this, Dock = DockStyle.Top };
textBoxSearch = new TextBox { Parent = this, Top = 200 };
textBoxSearch.TextChanged += TextBoxSearch_TextChanged;
dataTable = new DataTable();
dataTable.Columns.Add("Id", typeof(int));
dataTable.Columns.Add("Name", typeof(string));
dataTable.Columns.Add("BirthDate", typeof(DateTime));
dataTable.Rows.Add(1, "Bob", new DateTime(2001, 01, 01));
dataTable.Rows.Add(2, "John", new DateTime(1999, 09, 09));
dataTable.Rows.Add(3, "Alice", new DateTime(2002, 02, 02));
dataView = new DataView(dataTable);
dataGridView.DataSource = dataView;
}
private void TextBoxSearch_TextChanged(object sender, EventArgs e)
{
var text = textBoxSearch.Text;
var values = dataTable.Columns
.OfType<DataColumn>()
.Select(c => "Convert([" + c.ColumnName + "], System.String)")
.Select(c => c + " like '%" + text + "%'");
var filter = string.Join(" or ", values);
dataView.RowFilter = filter;
}
}
为了在所有不同类型的列中进行搜索,我将它们转换为字符串。然后使用 like
运算符。最后,结合 or
.
我的程序运行,客户端输入他要查找的内容并在 SQL 数据库中搜索。当他点击搜索时,它出现在数据网格视图中。
我希望客户输入任何单词,单词的开头或结尾,而不是在一列中查找,而是在整个 table.
中查找我该如何编程?
clsdatasource.search 获取文本框中的内容并将其存储在变量中。
DataView myview = new DataView(mytable);
myview.RowFilter = "CUSTOMER='" + clsDataSource.search + "'";
dataGridView1.DataSource = myview;
我尝试了什么:
for (int i = 0; i < myset.Tables[0].Columns.Count; i++) {
DataRow[] MyDR = myset.Tables[0].Select(myset.Tables[0].Columns[i].ToString() + "='" + clsDataSource.search + "'");
if (MyDR.Length > 0) {
dataGridView1.DataSource = myview;
}
}
试试这个:
public partial class Form1 : Form
{
DataGridView dataGridView;
DataTable dataTable;
DataView dataView;
TextBox textBoxSearch;
public Form1()
{
//InitializeComponent();
Width = 400;
dataGridView = new DataGridView { Parent = this, Dock = DockStyle.Top };
textBoxSearch = new TextBox { Parent = this, Top = 200 };
textBoxSearch.TextChanged += TextBoxSearch_TextChanged;
dataTable = new DataTable();
dataTable.Columns.Add("Id", typeof(int));
dataTable.Columns.Add("Name", typeof(string));
dataTable.Columns.Add("BirthDate", typeof(DateTime));
dataTable.Rows.Add(1, "Bob", new DateTime(2001, 01, 01));
dataTable.Rows.Add(2, "John", new DateTime(1999, 09, 09));
dataTable.Rows.Add(3, "Alice", new DateTime(2002, 02, 02));
dataView = new DataView(dataTable);
dataGridView.DataSource = dataView;
}
private void TextBoxSearch_TextChanged(object sender, EventArgs e)
{
var text = textBoxSearch.Text;
var values = dataTable.Columns
.OfType<DataColumn>()
.Select(c => "Convert([" + c.ColumnName + "], System.String)")
.Select(c => c + " like '%" + text + "%'");
var filter = string.Join(" or ", values);
dataView.RowFilter = filter;
}
}
为了在所有不同类型的列中进行搜索,我将它们转换为字符串。然后使用 like
运算符。最后,结合 or
.