与文本框WPF相等的DataGrid

时间:2019-02-17 11:17:26

标签: c# wpf visual-studio

当带文本框的text =显示id时,我想在单元格[1]中搜索。

我在DataGrid中有相同的文字,但总是向我显示

not matching...

我使用以下代码:

for (int i = 0; i < datagrid_customer.Items.Count; i++)
{
    if (Convert.ToString((datagrid_customer.SelectedCells[1].Column.GetCellContent(datagrid_customer.Items[i]) as TextBlock).Text) == txtcom.Text)
    {
        txt_f1.Text = Convert.ToString((datagrid_customer.SelectedCells[0].Column.GetCellContent(datagrid_customer.Items[i]) as TextBlock).Text);
    }
    else
    { txt_f1.Text = "Not Matching..."; }
}

什么问题

我该怎么办?

DataGrid代码:

<DataGrid x:Name="datagrid_customer" HorizontalAlignment="Left" Height="322" VerticalAlignment="Top" 
          Margin="668,105,-667,0"  Width="699" FlowDirection="RightToLeft" AutoGenerateColumns="False" 
          SelectionChanged="datagrid_customer_SelectionChanged"  >
    <DataGrid.Columns>
        <DataGridTextColumn Header="کد محصول" Binding="{Binding MahID}">
            <DataGridTextColumn.HeaderStyle>
                <Style TargetType="{x:Type DataGridColumnHeader}">
                    <Setter Property="FontFamily" Value="B Yekan" />
                    <Setter Property="FontSize" Value="12" />
                    <Setter Property="HorizontalContentAlignment" Value="Center" />
                </Style>
            </DataGridTextColumn.HeaderStyle>
        </DataGridTextColumn>
        <DataGridTextColumn Header="نام محصول" Binding="{Binding MahName}">
            <DataGridTextColumn.HeaderStyle>
                <Style TargetType="{x:Type DataGridColumnHeader}">
                    <Setter Property="FontFamily" Value="B Yekan" />
                    <Setter Property="FontSize" Value="12" />
                    <Setter Property="HorizontalContentAlignment" Value="Center" />
                </Style>
            </DataGridTextColumn.HeaderStyle>
        </DataGridTextColumn>
    </DataGrid.Columns>
</DataGrid>

0 个答案:

没有答案