devexpress treelist 绑定数据时让部分节点默认选择

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了devexpress treelist 绑定数据时让部分节点默认选择相关的知识,希望对你有一定的参考价值。

绑定的数据源里有个字段type,当type为“板式”,“沙发”时,树节点前面的复选框自动被选中,怎么实现

            DataTable dtTree = new DataTable();
            dtTree.Columns.Add("ID",typeof(string));
            dtTree.Columns.Add("Code", typeof(string));
            dtTree.Columns.Add("Name", typeof(string));
            dtTree.Columns.Add("Description", typeof(string));
            dtTree.Columns.Add("ParentID", typeof(string));
            //新增加的列 布尔类型才能绑定checkbox
            dtTree.Columns.Add("Tag", typeof(bool));
            dtTree.Columns.Add("Happy", typeof(string));

            DataRow row = dtTree.NewRow();
            row["ID"] = "1";
            row["Code"] = "001";
            row["Name"] = "野原广志";
            row["Description"] = "野原广志";
            row["ParentID"] = "-1";
            row["tag"] = false;
            row["Happy"] = "爱吃青椒";
            dtTree.Rows.Add(row);

            DataRow row2 = dtTree.NewRow();
            row2["ID"] = "2";
            row2["Code"] = "002";
            row2["Name"] = "野原美呀";
            row2["Description"] = "野原美呀";
            row2["ParentID"] = "-1";
            row2["tag"] = false;
            row2["Happy"] = "爱吃青椒";
            dtTree.Rows.Add(row2);

            DataRow row3 = dtTree.NewRow();
            row3["ID"] = "3";
            row3["Code"] = "003";
            row3["Name"] = "野原新之助";
            row3["Description"] = "野原新之助";
            row3["ParentID"] = "-1";
            row3["tag"] = false;
            row3["Happy"] = "不爱吃青椒";
            dtTree.Rows.Add(row3);

            DataRow row4 = dtTree.NewRow();
            row4["ID"] = "4";
            row4["Code"] = "004";
            row4["Name"] = "野原小白";
            row4["Description"] = "野原小白";
            row4["ParentID"] = "3";
            row4["tag"] = false;
            row4["Happy"] = "不爱吃青椒";
            dtTree.Rows.Add(row4);

            //模拟数据 将查询出来的datatable添加一列 例如:tag
            //然后查询datatable type列 当type为“板式”,“沙发”时 将tag列置为true
            DataRow[] drs = dtTree.Select("Happy='爱吃青椒'");
            foreach (DataRow item in drs)
           
                item["tag"] = true;
           
            treeList1.DataSource = dtTree;

 

 

参考技术A

可以使用DataTrigger

<DataTrigger Binding="Binding Path=Type" Value="板式">
        <Setter Property="IsSelected" Value="True" />
</DataTrigger>
<DataTrigger Binding="Binding Path=Type" Value="沙发">
        <Setter Property="IsSelected" Value="True" />
</DataTrigger>

详细参考MSDN

请问DEVExpress TreeList 控件怎么绑定数据? 是哪个事件触发绑定的?谢谢了!

参考技术A 跟C#里面自带的那个一样的绑定,时间是databound事件 参考技术B 绑定
private void initTree()


string sqltree = "select fitemid id,fnumber 代码,fname 名称,fparentid parentid from T_ITEM WHERE FITEMCLASSID=4 and fdeleted=0 and substring(fnumber,1,1)='8' and fdetail=0";
DataTable dtTree = SqlFunc.ExecSQL(sqltree);
treeList1.DataSource = dtTree;
treeList1.Columns["id"].Visible = false;
treeList1.Columns["parentid"].Visible = false;
treeList1.ExpandAll();
gridView1.OptionsView.AllowCellMerge = true;

单击事件
private void treeList1_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e)

DataRowView drv = (DataRowView)treeList1.GetDataRecordByNode(treeList1.FocusedNode);
DataRow dr = drv.Row;
int p = Convert.ToInt32(dr[0]);
………………
本回答被提问者采纳

以上是关于devexpress treelist 绑定数据时让部分节点默认选择的主要内容,如果未能解决你的问题,请参考以下文章

请问DEVExpress TreeList 控件怎么绑定数据? 是哪个事件触发绑定的?谢谢了!

DevExpress XtraTreeList TreeList复选框选择

treelist设置展开卡顿

devexpress treelist 过滤

DevExpress的TreeList实现自定义节点NodeCell的背景颜色和前景色

[DevExpress]treeList1背景色设置与自定义图标