如何在Datagrid中访问单元格对行的控制 [英] How to Access cell’s control of the row in Datagrid

查看:69
本文介绍了如何在Datagrid中访问单元格对行的控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用WPF数据网格,我已以编程方式添加了列DataGridComboBoxColumn和DataGridTemplateColumn.

我希望具有不同数据源的列的每一行(每个comboBox)都可能是第一行的ComboBox将具有5个项目,第二行的ComboBox将具有3个项目.

------------添加了空白StackPanel ---------------

I am using the WPF datagrid, I have added the columns DataGridComboBoxColumn, DataGridTemplateColumn programatically.

I want every row (every comboBox) of the the column with the different datasource may be first row’s ComboBox will have 5 items and second row’s ComboBox will have 3 items.

------------added blank StackPanel ---------------

<UserControl.Resources>
        <DataTemplate x:Key="manageAreaCellTemplate">
            <StackPanel Orientation="Horizontal">
            </StackPanel>
        </DataTemplate>
</UserControl.Resources>





//--------------Added Column ------------
DataGridTemplateColumn dgTemplateColumn = new DataGridTemplateColumn();
dgTemplateColumn.Width = new DataGridLength(1, DataGridLengthUnitType.Star);
dgTemplateColumn.Header = "Manage Options";
dgTemplateColumn.CellTemplate = this.FindResource("manageAreaCellTemplate") as DataTemplate;
dataGrid1.Columns.Add(dgTemplateColumn);
//-------------- dataGrid1_LoadingRow  Event------------

void dataGrid1_LoadingRow(object sender, Microsoft.Windows.Controls.DataGridRowEventArgs e)
{
            DataTemplate dt = (DataTemplate)e.Row.FindResource("manageAreaCellTemplate");
            object stk = dt.LoadContent();
            StackPanel stk1 = new StackPanel();
            stk1 = (StackPanel)stk;
            ComboBox cmb = new ComboBox();
            PortInfoCollection portInfoCollection = new PortInfoCollection();
            cmb.ItemsSource = portInfoCollection;
            cmb.SelectedValuePath = "Visits";
            cmb.DisplayMemberPath = "Port";
            stk1.Children.Add(cmb);
}


我的问题是组合框没有填充到网格中.


My problem is combobox is not getting populated in the grid.

推荐答案

按以下方式进行操作:

首先填充数据表或数据集.

您可以访问网格的组合框,如下所示

ComboBox cb =(ComboBox)e.row.FindControl("YourCombo")


cb.DataSource = dt或ds

cb.databind()


干杯
Go like this way:

First fill datatable or dataset.

u can access the combobox of the grid like below

ComboBox cb=(ComboBox)e.row.FindControl("YourCombo")


cb.DataSource=dt or ds

cb.databind()


Cheers


这篇关于如何在Datagrid中访问单元格对行的控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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