导航

DevExpress TreeList加载大批量数据的时候绑定数据源 dataTable.

注意事项1: 由于一旦绑定了数据源dataTable的些许变化便在TreeList中有所体现, 所以等dataTable完全填充好了之后再绑定数据源.

注意事项2:dataTable每行的父节点ID当加载到目前为止,还没有找到那么可能就当成空了, 所以最后treelist呈现就有问题, 所以解决办法有3个, 1: 大范围数据,也就是父节点的数据一定要先于子节点在表中排列.(这个不好控制).  2: 等完全填充完datatable时再重新设置每行的父节点ID.   3:可以边填充边设置, 最后再绑定数据源, 在填充dataTable之前先解绑数据源, 即先把数据源设为空,完全填充好再重新绑定(经过测试这种方法的加载速度最快了)

参看代码如下:

public void ShowData(ForecastService.TrafficResult[] result, int iscrop, bool AllType)

        {

            //填充数据

            try

            {

                ResetCtrl(AllType);

                this.result = result;

                treeListResult.Nodes.Clear();

treeListResult.DataSource = null; //一定要先解绑

                FillTypeFirstData(result, this.dtResult);

treeListResult.DataSource = dtResult;

                if (treeListResult.Nodes.Count > 0)

                {

                    for (int i = 0; i < treeListResult.Nodes.Count; i++)

                    {

                        treeListResult.Nodes[i].HasChildren = true;

                        treeListResult.Nodes[i].Expanded = true;

                    }

                }

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message + ex.StackTrace);

            }

        }

private void FillTypeFirstData(Inspur.Forecast.ForecastService.TrafficResult[] result, DataTable dataTable)

        {

            try

            {

                dataTable.Rows.Clear();

                for (int i = 0; i < result.Length; i++)

                {

                    if (result[i].trafficType == "ALL" && result[i].city == "ALL")//全省的数据

                    {

                        DataRow dr = dataTable.NewRow();

dr["keyFieldName"] = "湖南";//设置当前行ID

dr["parentFieldName"] = DBNull.Value;  //设置父节点ID

                        dr["维度"] = "湖南";

                        dr["去年基准日话务量"] = result[i].lastFir.Trim();

                        dr["去年预测日话务量"] = result[i].lastSec.Trim();

                        dr["增长幅度"] = result[i].add.Trim();

                        dr["今年基准日话务量"] = result[i].nowFir.Trim();

                        dr["今年预测日话务量"] = result[i].nowSec.Trim();

                        dr["目前电路数"] = result[i].count.Trim();

                        dr["预测2M电路数"] = result[i].fcastcount.Trim();

                        dr["需要新增的电路数"] = result[i].addcount.Trim();

                        dataTable.Rows.Add(dr);

                    }

                    else if (result[i].trafficType != "ALL" && result[i].city == "ALL" && result[i].nename == "ALL")//汇总到类型的数据

                    {

                        DataRow dr = dataTable.NewRow();

                        dr["keyFieldName"] = result[i].trafficType;

                        dr["parentFieldName"] = "湖南";

                        dr["维度"] = result[i].trafficType;

                        dr["去年基准日话务量"] = result[i].lastFir.Trim();

                        dr["去年预测日话务量"] = result[i].lastSec.Trim();

                        dr["增长幅度"] = result[i].add.Trim();

                        dr["今年基准日话务量"] = result[i].nowFir.Trim();

                        dr["今年预测日话务量"] = result[i].nowSec.Trim();

                        dr["目前电路数"] = result[i].count.Trim();

                        dr["预测2M电路数"] = result[i].fcastcount.Trim();

                        dr["需要新增的电路数"] = result[i].addcount.Trim();

                        dataTable.Rows.Add(dr);

                    }

                    else if (result[i].trafficType != "ALL" && result[i].city != "ALL" && result[i].nename == "ALL")//汇总到地市的数据

                    {

                        DataRow dr = dataTable.NewRow();

                        dr["keyFieldName"] = result[i].trafficType + "_" + result[i].city;

                        dr["parentFieldName"] = result[i].trafficType;

                        dr["维度"] = result[i].city;

                        dr["去年基准日话务量"] = result[i].lastFir.Trim();

                        dr["去年预测日话务量"] = result[i].lastSec.Trim();

                        dr["增长幅度"] = result[i].add.Trim();

                        dr["今年基准日话务量"] = result[i].nowFir.Trim();

                        dr["今年预测日话务量"] = result[i].nowSec.Trim();

                        dr["目前电路数"] = result[i].count.Trim();

                        dr["预测2M电路数"] = result[i].fcastcount.Trim();

                        dr["需要新增的电路数"] = result[i].addcount.Trim();

                        dataTable.Rows.Add(dr);

                    }

                    else if (result[i].trafficType != "ALL" && result[i].city != "ALL" && result[i].nename != "ALL" && result[i].middle == "ALL")//汇总到网元的数据

                    {

                        DataRow dr = dataTable.NewRow();

                        dr["keyFieldName"] = result[i].trafficType + "_" + result[i].city + "_" + result[i].nename;

                        dr["parentFieldName"] = result[i].trafficType + "_" + result[i].city;

                        dr["维度"] = result[i].nename;

                        dr["去年基准日话务量"] = result[i].lastFir.Trim();

                        dr["去年预测日话务量"] = result[i].lastSec.Trim();

                        dr["增长幅度"] = result[i].add.Trim();

                        dr["今年基准日话务量"] = result[i].nowFir.Trim();

                        dr["今年预测日话务量"] = result[i].nowSec.Trim();

                        dr["目前电路数"] = result[i].count.Trim();

                        dr["预测2M电路数"] = result[i].fcastcount.Trim();

                        dr["需要新增的电路数"] = result[i].addcount.Trim();

                        dataTable.Rows.Add(dr);

                    }

                    else if (result[i].trafficType != "ALL" && result[i].city != "ALL" && result[i].nename != "ALL" && result[i].middle != "ALL")//到中继的数据

                    {

                        DataRow dr = dataTable.NewRow();

                        dr["keyFieldName"] = result[i].trafficType + "_" + result[i].city + "_" + result[i].nename + "_" + result[i].middle;

                        dr["parentFieldName"] = result[i].trafficType + "_" + result[i].city + "_" + result[i].nename;

                        dr["维度"] = result[i].middle;

                        dr["去年基准日话务量"] = result[i].lastFir.Trim();

                        dr["去年预测日话务量"] = result[i].lastSec.Trim();

                        dr["增长幅度"] = result[i].add.Trim();

                        dr["今年基准日话务量"] = result[i].nowFir.Trim();

                        dr["今年预测日话务量"] = result[i].nowSec.Trim();

                        dr["目前电路数"] = result[i].count.Trim();

                        dr["预测2M电路数"] = result[i].fcastcount.Trim();

                        dr["需要新增的电路数"] = result[i].addcount.Trim();

                        dataTable.Rows.Add(dr);

                    }

                }

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message + ex.StackTrace);

            }

        }

public TrafficResultTree(IApplication application)

        {

            InitializeComponent();

            this.application = application;

            SetTreeListColumn("Config\\Forecast\\TrafficForecastResultTree.xml", "/Head", treeListResult);

            //Test

            PublicFunction.SetDataTableColumn(treeListResult, dtResult);

treeListResult.ParentFieldName = "parentFieldName";  //设置树的ParentFieldName 属性

treeListResult.KeyFieldName = "keyFieldName";  //设置树的KeyFieldName 属性

            //

        }

public static void SetDataTableColumn(DevExpress.XtraTreeList.TreeList treeListResult, DataTable dtResult)

        {

            try

            {

                dtResult.Columns.Clear();

DataColumn dcid = new DataColumn("keyFieldName", Type.GetType("System.String"));

DataColumn dcparentId = new DataColumn("parentFieldName", Type.GetType("System.String"));

                dtResult.Columns.Add(dcid);

                dtResult.Columns.Add(dcparentId);

                for (int i = 0; i < treeListResult.Columns.Count; i++)

                {

                    DataColumn dc = new DataColumn(treeListResult.Columns[i].FieldName);

                    dtResult.Columns.Add(dc);

                }

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message + ex.StackTrace);

            }

        }

private void InsertRelayData(string time, string type, string cityName, string equipeName, string burName, Inspur.Analysis.AnalysisService.Relay resultRelay)

        {

            try

            {

                DataRow drRelay = dtResult.NewRow();

                drRelay["keyFieldName"] = time + "_" + type + "_" + cityName + "_" + equipeName + "_" + burName + "_" + resultRelay.RelayName + "_" + DateTime.Now.ToString();

                drRelay["parentFieldName"] = time + "_" + type + "_" + cityName + "_" + equipeName + "_" + burName;

                drRelay["维度"] = resultRelay.RelayName;

                drRelay["定义电路数"] = resultRelay.Total.CIRCUITS;

                drRelay["来话试呼次数"] = resultRelay.Total.in_att;

                drRelay["去话试呼次数"] = resultRelay.Total.out_att;

                drRelay["来话应答次数"] = resultRelay.Total.in_ans;

                drRelay["去话应答次数"] = resultRelay.Total.out_ans;

                drRelay["来话话务量"] = resultRelay.Total.in_traf;

                drRelay["去话话务量"] = resultRelay.Total.out_traf;

                drRelay["总话务量"] = resultRelay.Total.traf;

                drRelay["来话市话话务量"] = resultRelay.Total.in_local_traf;

                drRelay["去话市话话务量"] = resultRelay.Total.out_local_traf;

                drRelay["来话长话话务量"] = resultRelay.Total.in_long_traf;

                drRelay["去话长话话务量"] = resultRelay.Total.out_long_traf;

                drRelay["市话话务量"] = resultRelay.Total.local_traf;

                drRelay["长话话务量"] = resultRelay.Total.long_traf;

                dtResult.Rows.Add(drRelay);

                //DevExpress.XtraTreeList.Nodes.TreeListNode noderelay = null;

                //for (int i = 0; i < nodebureauD.Nodes.Count; i++)

                //{

                //    if (nodebureauD.Nodes[i]["维度"].ToString() == relay.RelayName)

                //    {

                //        noderelay = nodebureauD.Nodes[i];

                //        break;

                //    }

                //}

                //if (noderelay == null)

                //{

                //    List<string> relayList = new List<string>();

                //    relayList.Add(relay.RelayName);

                //    noderelay = treeList.AppendNode(relayList.ToArray(), nodebureauD);

                //}

                //if (noderelay != null)

                //{

                //    InsertTargetData(noderelay, relay.RelayName, relay.Total);

                //}

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message + ex.StackTrace);

            }

        }