Datagrid WPF中的Combobox [英] Combobox in Datagrid WPF

查看:96
本文介绍了Datagrid WPF中的Combobox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网格,我在这个Datagrid中添加了一个组合框,它的来源是一个集合并且可以很好地填充组合。



I have a grid and I have added a combo box in this Datagrid, its source is a collection and filling the combos fine.

<Grid>
<ComboBox x:Name="cmb" SelectedValuePath="Name" Height="24" Width="150" Margin="126,87,228,200"> 
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding Code}" FontWeight="Bold" Foreground="Black" 
                                   Width="30"/>
                        <TextBlock Text="{Binding Name}" Foreground="Black"/>
                    </StackPanel>
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>
 </Grid>





我必须从数据库填充此网格和所有组合,但无法执行此操作,



如何实现这一目标?



I have to fill this grid and all combos from the DB, but unable to do this,

How can I achieve this?

推荐答案

你可以这样使用

You can use like this
SqlConnection conn = new SqlConnection("connection string");
SqlDataAdapter da = new SqlDataAdapter("your Query", conn);
DataSet ds = new DataSet();
da.Fill(ds, "tblName");
cmb.ItemsSource = ds.Tables[0].DefaultView;





如果您使用MVVM,请查看此内容...

http://blog.cylewitruk.com/2010/09/wpf-combobox-and-databinding-datacontext-itemssource-displaymemberpath-selecteditem-selectedvalue-selectedvaluepath / [ ^ ]


这篇关于Datagrid WPF中的Combobox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆