【问题标题】:DataGridView C# wpf select row to textboxesDataGridView C# wpf 选择行到文本框
【发布时间】:2021-12-18 13:28:48
【问题描述】:

我在 C# WPF 中有一个带有数据网格视图和文本框的表单。 我使用基于字段上的姓名和姓氏的搜索选项从 MySql 数据库中获取数据。

                MySqlCommand cmd = new MySqlCommand("SELECT * FROM  owners WHERE name='" + namesearch + "' OR last_name= '" + lastnamesearch + "'", conn);

我将一些基本数据,如姓名、姓氏、电话、国家 ID 绑定到我的 Datagrid 中。

            <DataGrid.Columns>
            <DataGridTextColumn  Header="" Width="3" IsReadOnly="True" />
            <DataGridTextColumn Binding="{Binding Path=name}" Header="نام" Width="150" IsReadOnly="True" />
            <DataGridTextColumn Binding="{Binding Path=last_name}" Header="نام خانوادگی" Width="150" IsReadOnly="True" />
            <DataGridTextColumn Binding="{Binding Path=national_code}" Header="کد ملی" Width="200" IsReadOnly="True" />
            <DataGridTextColumn Binding="{Binding Path=father_name}" Header="نام پدر" Width="100" IsReadOnly="True" />
            <DataGridTextColumn Binding="{Binding Path=mobile}" Header="شماره همراه" Width="150" IsReadOnly="True" />
        </DataGrid.Columns>

现在我想要这个选项:当我从数据网格视图行中选择一行时,我之前使用查询命令获得的所有数据,都只出现在文本框中,例如,名称转到 TexboxName 姓氏到 TexboxLastname,以及我获得但未在 Datagrid 视图中显示的所有其他数据。 这些是我桌子上的字段

我为每个人都有一个文本框 对不起我的英语不好,希望你能理解。

【问题讨论】:

标签: c# mysql wpf datagrid


【解决方案1】:

您可以将TextBox 绑定到DataGridSelectedItem 属性:

<DataGrid x:Name="dg" ... />

<TextBox x:Name="TexboxName" Text="{Binding SelectedItem.name, ElementName=dg}" />

【讨论】:

  • 问题是我没有将所有数据绑定到DataGrid。只有姓名和姓氏绑定到 DataGrid l,例如当有人搜索 John 时,有一个姓名搜索框,Johns 的所有结果(姓名、姓氏和 id)都将出现在 DataGrid 中,如果我选择其中一个,其余的字段(电话、地址、生日和...)出现在文本框中。
  • 这就是为什么您应该绑定到DataGridSelectedItem 属性(请参阅我的编辑)。即使您在DataGrid 中只看到它们的子集,它也会返回具有所有这些属性/列的对象。
  • 我明白了,它成功了,谢谢你的帮助,你让我开心
猜你喜欢
  • 2015-06-28
  • 2021-05-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-29
相关资源
最近更新 更多