我正在使用WPF創建一個應用程序,其中一個搜索字符串將過濾一個datagridview,但它可以包含多個搜索字符串。 我試着在下面創建一個過濾器函數,但是它在最後一個位置設置可見性的位置並不完全正常。你們能幫我瞭解我哪裏出了問題嗎? 感謝WPF Foreach Row Visibility Set
private void BindGrid(string parameter)
{
string[] array = parameter.Split();
string constring = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\SearchTable.mdf;Integrated Security=True;Connect Timeout=30";
using (SqlConnection con = new SqlConnection(constring))
{
con.Open();
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Projects", con))
{
cmd.CommandType = CommandType.Text;
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
dataGridView1.SetBinding(ItemsControl.ItemsSourceProperty, new Binding { Source = dt });
foreach (DataRow row in dt.Rows)
{
SqlDataReader reader = cmd.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
int index = reader.GetInt32(0);
string a = reader.GetString(1);
string b = reader.GetString(2);
string c = reader.GetString(3);
string d = reader.GetString(4);
string e = reader.GetString(5);
string f = reader.GetString(6);
string g = reader.GetString(7);
string h = reader.GetString(8);
string i = reader.GetString(9);
string j = reader.GetString(10);
string t = a + " " + b + " " + c + " " + d + " " + e + " " + f + " " + g + " " + h + " " + i + " " + j;
foreach (string value in array)
{
if (t.IndexOf(value, StringComparison.CurrentCulture) == -1)
{
row.Visibility = Visibility.Collapsed;
}
else if (t.IndexOf(value, StringComparison.CurrentCulture) > 0)
{
row.Visibility = Visibility.Visible;
}
}
}
}
reader.Close();
}
rows = dataGridView1.Items.Count.ToString();
Rows.Content = rows + " Entries";
}
}
}
con.Close();
}
}
WPF不支持'DataGridView'構造。 (另外'DataGridView'沒有'Items'屬性。)也許你的意思是['DataGrid'](https://msdn.microsoft.com/en-us/library/system.windows.controls.datagrid(v = vs。 110)的.aspx)?如果是這樣,請編輯您的問題標籤,以便您的問題可以從正確的人獲得更多關注。 – OhBeWise