我在使用WPF C#时遇到了一些麻烦,需要使用文本框来过滤来自数据库的数据网格值。确实,我是新手,需要在这种情况下寻求帮助。
这是我的XAML:
<Button Name="btnSelect"
Content="Select All"
Height="30"
Width="80"
Margin="4"
HorizontalAlignment="Center"
Click="btn_SelectUser"/>
<DataGrid Name="dtgUser"
AutoGenerateColumns="True"
HorizontalAlignment="Left"
VerticalAlignment="Top" Height="380" Width="684"
Margin="10,54,0,0"/>
这是我的代码后台,用于在按下“选择”按钮后从数据库中选择值:
private void btn_SelectUser(object sender, RoutedEventArgs e)
{
_con = new SqlConnection(_strConn);
try
{
_con.Open();
string query = "select id_int_user, name_str_user from tbl_user";
_cmd = new SqlCommand(query, _con);
_cmd.ExecuteNonQuery();
_adp = new SqlDataAdapter(_cmd);
_dt = new DataTable("tbl_user");
_adp.Fill(_dt);
dtgUser.ItemsSource = _dt.DefaultView;
_adp.Update(_dt);
_con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
这是选择文本框中刚编辑的值的方法。如何实现?(注意:我需要通过数据网格从数据库获取值):
private void txt_SearchUser(object sender, TextChangedEventArgs e)
{
//Here is my difficulty //I know nothing how to do it.
}
DataGrid
的ItemsSource
属性可以让你将数据放入其中,但它只接受IEnumerable<T>
类型的数据,因此你可能需要找到一个解决方法。 - Clive DMUpdate
将数据放入了DataTable
中。我猜他想从客户端查询并过滤所有数据。 - Clive DM