string[] columnName = new string[20];
اکنون در رویداد لود فرم نام ستونهايي از DataTable را كه قرار است عملیات جستجو بر طبق آن صورت گیرد به این آرايه اضافه مي كنيم :
columnName[0] = dt.Columns["Name"].ColumnName; columnName[1] = dt.Columns["Family"].ColumnName; columnName[2] = dt.Columns["idPerson"].ColumnName;
public string resaultOfSearch1(DataTable rdt , string txt , string[] listColumn) { }
if (txt != "") { }
if (txt != "") { try { int x = Convert.ToInt32(txt); items = string.Empty; } catch (Exception) { } }
try
{
int x = Convert.ToInt32(txt); items = string.Empty;
for (int i = 0; i < rdt.Columns.Count; i++)
{
if (Array.IndexOf(listColumn, rdt.Columns[i].ToString()) != -1)
{
if (rdt.Columns[i].DataType == typeof(Int32))
{
if (i == rdt.Columns.Count - 1) { items += rdt.Columns[i] + "=" + txt;
}
else
{
items += rdt.Columns[i] + "=" + txt + " OR ";
}
}
else if (rdt.Columns[i].DataType == typeof(string))
{
if (i == rdt.Columns.Count - 1)
{
items += rdt.Columns[i] + " LIKE '%" + txt + "%'";
}
else
{
items += rdt.Columns[i] + " LIKE '%" + txt + "%'" + " OR ";
}
}
}
}
}
catch (Exception)
{
try
{
items = string.Empty;
for (int i = 0; i < rdt.Columns.Count; i++)
{
if (Array.IndexOf(listColumn, rdt.Columns[i].ToString()) != -1)
{
if (rdt.Columns[i].DataType == typeof(string))
{
if (i == rdt.Columns.Count)
{
items += rdt.Columns[i] + " LIKE '%" + txt + "%'";
}
else
{
items += rdt.Columns[i] + " LIKE '%" + txt + "%'" + " OR ";
}
}
}
}
}
catch (Exception) { }
}
private void textBox4_TextChanged(object sender, EventArgs e) { Class.search1 srch1 = new Class.search1(); dt.DefaultView.RowFilter = srch1.resaultOfSearch1(dt,textBox4.Text,columnName); }