如何给WPF中的DataGrid的ComBox编辑列添加数据

如何给WPF中的DataGrid的ComBox编辑列添加数据,第1张

帮助别人就是帮助自己! 如果这里解决了您的问题,请您点一下推荐 × WPF项目中现有一个DataGrid,其中有一个名为“姓名”的模板列和名为“工号”的文本列,模板列在编辑时是一个ComBox,要实现的功能:ComBox的选项是从数据查询得到的姓名列表(如人员表中性别为女的人的姓名列表),选择其中的某个姓名,TextBlock显示选择的该姓名,“工号”列显示该姓名的工号,如何实现数据的绑定及将记录保存到数据库中<DataGrid AutoGenerateColumns="False" Height="200" HorizontalAlignment="Left" Margin="12,200,0,130" Name="dataGrid1" ItemsSource="{Binding}" Width="613">�0�2<DataGrid.Columns>�0�2<DataGridTemplateColumn x:Name="selectxmCol" Header="姓名" Width="100">�0�2<DataGridTemplateColumn.CellTemplate>�0�2<DataTemplate>�0�2<TextBlock Name="selectXm" Text="{Binding Path=xm}" ></TextBlock>�0�2</DataTemplate>�0�2</DataGridTemplateColumn.CellTemplate>�0�2<DataGridTemplateColumn.CellEditingTemplate>�0�2<DataTemplate>�0�2<Grid Name="selectxmGrid" FocusManager.FocusedElement="{Binding ElementName=xmComb}">�0�2<ComboBox Name="xmComb" DisplayMemberPath="xm" SelectedValuePath="gh" IsEditable="True" IsSynchronizedWithCurrentItem="True"></ComboBox>�0�2</Grid>�0�2</DataTemplate>�0�2</DataGridTemplateColumn.CellEditingTemplate>�0�2�0�2</DataGridTemplateColumn>�0�2�0�2<DataGridTextColumn Header="工号" Binding="{Binding Path=gh}" Width="100"/>�0�2</DataGrid.Columns>例如:Datatable data=new Datatable()首先你需要给你的Combox绑定待选的数据HTML code<DataGrid x:Name="gridEmployers" AutoGenerateColumns="False" ItemsSource="{Binding}" DataContext="{Binding}"><DataGridTemplateColumn.CellEditingTemplate><DataTemplate><ComboBox Text="{Binding Name}" IsEditable="True" DisplayMemberPath="Name" ItemsSource="{Binding Source={StaticResource employers}}" SelectionChanged="OnChangeNameSelection"/></DataTemplate></DataGridTemplateColumn.CellEditingTemplate><Window.Resources><ObjectDataProvider x:Key="employers" ObjectType="{x:Type local:_2011_12_28_01_Data}" MethodName="GetEmployers"/></Window.Resources>是用ObjectDataProvider来获取数据C# codepublicclass _2011_12_28_01_Data { public IEnumerable<Employer>GetEmployers() { returnnew Employer[]{ new Employer{ Name="A", Id=1}, new Employer{ Name="B", Id=2}, new Employer{ Name="C", Id=3}, new Employer{ Name="D", Id=4}, new Employer{ Name="E", Id=5} }} } 然后需要给Combox添加SelectionChanged事件:C# codeprivatevoid OnChangeNameSelection(object sender, SelectionChangedEventArgs e) { var data =this.FindResource("employers") as ObjectDataProvidervar employers = data.Data as IEnumerable<Employer>var cmb = sender as ComboBoxvar selectedItem =this.gridEmployers.SelectedItem as Employervar employer = employers.FirstOrDefault(x =>x.Name == (cmb.SelectedItem as Employer).Name)if (employer !=null) { selectedItem.Id = employer.IdselectedItem.Name = employer.Name} } 最后一点就是给你的数据实现INotifyPropertyChanged接口,否则看不到效果C# codepublicclass Employer : INotifyPropertyChanged { publicstring Name { get { return name} set { name = valueif (PropertyChanged !=null) { PropertyChanged(this, new PropertyChangedEventArgs("Name"))} } } publicstring namepublicint Id { get { return id} set { id = valueif (PropertyChanged !=null) { PropertyChanged(this, new PropertyChangedEventArgs("Id"))} } } publicint idpublicevent PropertyChangedEventHandler PropertyChanged}注意,DataGrid和Combox绑定的不一样给DataGrid绑定是在初始化随便加一个作为示例C# codepublic _2011_12_28_01() { InitializeComponent()this.DataContext =new Employer[] { new Employer { Name ="A", Id =1 } }} 2011-12-28 20:48 推荐: 0 次 有帮助? 请推荐 怎么木有人回答啊

第一种方法:

DataTable dt = new DataTable()

dt.Columns.Add( "name" )

dt.Columns.Add( "value" )

DataRow dr = dt.NewRow()

dr[0] = "活动"

dr[1] = "1"

dt.Rows.Add(dr)DataRow dr1 = dt.NewRow()

dr1[0] = "生活"

dr1[1] = "2"

dt.Rows.Add(dr1)

this .comboBox1.DataSource = dt

this .comboBox1.DisplayMember = "name"

this .comboBox1.ValueMember = "value"

//调用方法:

//string _value = comboBox1.SelectedValue.ToString()

第二种:

//首先添加一个ComboBoxItem类

public class ComboBoxItem

{

private string _text = null

private object _value = null

public string Text

{ get {

return this ._text

} set {

this ._text = value

} }

public object Value

{ get {

return this ._value

} set {

this ._value = value

} }

public override string ToString()

{

return this ._text

} }

// 赋值方法

ComboBoxItem newitem = new ComboBoxItem()

newitem.Text = "男"

newitem.Value = "1"

ComboBoxItem newitem1 = new ComboBoxItem()

newitem1.Text = "女"

newitem1.Value = "0"

com_sex.Items.Add(newitem)

com_sex.Items.Add(newitem1)

// 调用方法:

ComboBoxItem sex_item = (ComboBoxItem)com_sex.SelectedItem

int com_sex_value = Convert.ToInt32(sex_item.Value)

string _Name = sex_item.Text

第三种:

//首先添加一个SetCls类

publicclassSetCls

{

privatestringID

privatestringNAME

publicSetCls( stringpid, stringpName)

{

this .ID =pid

this .NAME =pName

}

publicstringpID

{

get { returnID}

}

publicstringpName

{

get { returnNAME}

} }

// 赋值方法:(使用ArrayList 要先引用命名空间using System.Collections)

ArrayList lists = new ArrayList()

lists .Add( new SetCls ( "1" , "活动" ))

lists .Add( new SetCls ( "2" , "生活" ))

this .COMBOX.DisplayMember = "pID"

this .COMBOX.ValueMember = "pName"

this .COMBOX.DataSource = lists

// 调用方法:

string com_sex_value = COMBOX.SelectedValue.ToString()

我用DataSet填充的数据库中的内容(我这个是直接赋值,并不像上面三个添加值给ComBox)

DataSet ds_zubie = new DataSet()

da = new SqlDataAdapter(sql_zubie, PublicDB.DBzbw)

da.Fill(ds_zubie, "zubie" )

com_paidan.DataSource = ds_zubie.Tables[ "zubie" ].DefaultView//绑定数据源

com_paidan.ValueMember = "zubie_id" //赋值Value

com_paidan.DisplayMember = "zubie_name" //赋值显示名称

//调用方法:

string com_zubie_id = com_paidan.SelectedValue.ToString()

ComboBox对象.Items.Add(new DictionaryEntry("指定显示值", index))

ComboBox对象.SelectedValuePath = "Value"

ComboBox对象.DisplayMemberPath = "Key"

Value 和Key是固定格式不用修改


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11250230.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-14
下一篇 2023-05-14

发表评论

登录后才能评论

评论列表(0条)

保存