如何动态更新数据网格中组合框列的选择?

发布于 2024-12-13 15:25:48 字数 1002 浏览 5 评论 0原文

我正在尝试将数据表(dt)中的两列显示到数据网格中。并添加另一列,它是一个组合列,以便我可以为每行分配扇区。问题是组合框的选择无法动态更新。当我单击其他内容时,选择消失了。有什么办法可以解决这个问题吗?

   dg_display.DataContext = dt.DefaultView;
   dg_display.Columns[0].Header = "Symbol";
   dg_display.Columns[1].Header = "Company name";          
   DataGridComboBoxColumn columnComboBox = new DataGridComboBoxColumn();
   string[] sectorarray = new[]
                                    {
                                       "Consumer Discretionary", "Consumer     Staples", "Energy", "Financial",
                                       "Financials", "Health Care", "Industrials", "Information Technology",
                                       "Materials", "Other", "Technology","Telecommunication Services", "Utilities"
                                   };
        columnComboBox.ItemsSource = sectorarray;
        columnComboBox.IsReadOnly = false;
                    dg_display.Columns.Add(columnComboBox);
        dg_display.Columns[2].Header = "Sector";

I'm trying to display two columns in a datatable(dt) into a datagrid. And add another column which is a comboxcolumn so that I can assign sectors to each rows. The problem is that the selection of comboboxes cannot be dynamically updated. when I click something else, the selection is gone. Is there any way to fix this?

   dg_display.DataContext = dt.DefaultView;
   dg_display.Columns[0].Header = "Symbol";
   dg_display.Columns[1].Header = "Company name";          
   DataGridComboBoxColumn columnComboBox = new DataGridComboBoxColumn();
   string[] sectorarray = new[]
                                    {
                                       "Consumer Discretionary", "Consumer     Staples", "Energy", "Financial",
                                       "Financials", "Health Care", "Industrials", "Information Technology",
                                       "Materials", "Other", "Technology","Telecommunication Services", "Utilities"
                                   };
        columnComboBox.ItemsSource = sectorarray;
        columnComboBox.IsReadOnly = false;
                    dg_display.Columns.Add(columnComboBox);
        dg_display.Columns[2].Header = "Sector";

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟酒忠诚 2024-12-20 15:25:48

您没有将 ComboBox

Set 中的 SelectedItem 绑定到 SelectedItemBindingSelectedValueBinding

You're not binding the SelectedItem in the ComboBox

Set either SelectedItemBinding or SelectedValueBinding

铃予 2024-12-20 15:25:48

你可以这样尝试......

 DataGridComboBoxColumn col = new DataGridComboBoxColumn();
        col.Header = "Name";
        col.DisplayMemberPath = "Name"; 
        col.SelectedValueBinding = new Binding("Name");
        col.ItemsSource = simacc;
        col.TextBinding = new Binding("Name");
        col.CanUserSort = false;
        dataGrid1.Columns.Add(col);

you Can try like this....

 DataGridComboBoxColumn col = new DataGridComboBoxColumn();
        col.Header = "Name";
        col.DisplayMemberPath = "Name"; 
        col.SelectedValueBinding = new Binding("Name");
        col.ItemsSource = simacc;
        col.TextBinding = new Binding("Name");
        col.CanUserSort = false;
        dataGrid1.Columns.Add(col);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文