在文本框中更改搜索数据网格(在WPF中)

时间:2019-02-09 03:29:40

标签: c# wpf

如果表A1中的值buttockno是字符串类型,则可以使用以下代码。 如果表Al中的buttockno的值为数字怎么办?

<Window x:Class="DamDarYar_IDMS.Window4"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:DamDarYar_IDMS"
    mc:Ignorable="d"
    Title="Window4" Height="300" Width="300">
<Grid>
    <TextBox x:Name="textBox" HorizontalAlignment="Left" Height="23" Margin="151,44,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120" TextChanged="textBox_TextChanged"/>
    <DataGrid x:Name="dataGrid" HorizontalAlignment="Left" Margin="60,82,0,0" VerticalAlignment="Top" Height="112" Width="211"/>

</Grid>

    private void textBox_TextChanged(object sender, TextChangedEventArgs e)
    {
        IDMS_dbEntities db = new IDMS_dbEntities();

        try
        {
            dataGrid.ItemsSource = db.tblAIs.Where(a => a.ButtockNo.StartsWith(textBox.Text.Trim())).ToList();
        }
        catch
        {
        }
    }
}

}

1 个答案:

答案 0 :(得分:0)

我建议为此使用filter,但使用您的方法只需先转换为字符串即可。

a.NumericProperty.ToString().StartsWith(...
相关问题