选择DataGrid列,数据显示在ComboBox中

问题描述 投票:0回答:1
        DataGrid list = (DataGrid)sender;
        DataRowView row_selected = list.SelectedItem as DataRowView;
        if (row_selected != null)
        {
            // DataTable dt = pad.Selectbrand();
            Product_Form pf = new Product_Form(this);

            pf.btn_save.IsEnabled = false;
            pf.txt_pcode.Text = row_selected["pcode"].ToString();
            pf.txt_barcode.Text = row_selected["bcode"].ToString();
            pf.txt_pdes.Text = row_selected["pdes"].ToString();
            pf.txt_price.Text = row_selected["price"].ToString();
            pf.cmb_brand.SelectedItem=row_selected.ToString();

            pf.cmb_brand.ItemsSource = row_selected.DataView;
            pf.cmb_cate.ItemsSource = row_selected.DataView;

            pf.ShowDialog();

我有一页产品列表和 1 个表格产品表格 在产品形式中,我有一些文本框和两个 cmbobox 在产品列表中我有数据网格 现在我想从数据网格更新数据 但是当我单击数据时,数据未显示在组合框中

我怎样才能做到这一点

c# wpf
1个回答
0
投票

根据我对您的代码和问题的猜测,您想将 ComboBoxes 的选定项目更改为选定行

Brand
Category
正确吗?
如果
Brand
Categories
strings
的列表,那么你的代码应该是这样的:

pf.cmb_brand.ItemsSource = DataGridItemSource.Brands;
pf.cmb_cate.ItemsSource = DataGridItemSource.Categories;

对于所选项目,您的代码应该如下所示:

 pf.cmb_brand.SelectedItem = row_selected["brand"];

此事件处理程序将附加到 DataGrid

SelectionChanged
事件:

private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
   DataGrid list = (DataGrid)sender;
   DataRowView row_selected = list.SelectedItem as DataRowView;
   if (row_selected != null)
   {
       Product_Form pf = new Product_Form(this);

       pf.btn_save.IsEnabled = false;
       pf.txt_pcode.Text = row_selected["pcode"].ToString();
       pf.txt_barcode.Text = row_selected["bcode"].ToString();
       pf.txt_pdes.Text = row_selected["pdes"].ToString();
       pf.txt_price.Text = row_selected["price"].ToString();

       pf.cmb_brand.SelectedItem = row_selected["brand"]; 

      
       pf.cmb_brand.ItemsSource = list.ItemSource.Brands;
       pf.cmb_cate.ItemsSource = list.ItemSource.Categories;

       pf.ShowDialog();
   }
}
© www.soinside.com 2019 - 2024. All rights reserved.