treelist 的编辑与删除

  private void Form2_Load(object sender, EventArgs e)
        {
            for (int i = 0; i < pMap.LayerCount; i++)//从pmap中获取图层
            {
                string sFeaLyrName = pMap.get_Layer(i).Name;
                comboBoxEdit1.Properties.Items.Add(sFeaLyrName);//将sFeaLyrName添加至下拉列表
            }
        }


        private void comboBoxEdit1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string sSelectLyrName = comboBoxEdit1.Properties.Items[comboBoxEdit1.SelectedIndex].ToString();//获取图层索引,string类型,赋值给sSelectlyrname
            for (int i = 0; i < pMap.LayerCount; i++)//for循环,获取所有图层
            {
                IFeatureLayer pFeaLyr = pMap.get_Layer(i) as IFeatureLayer;//定义变量
                string sFeaLyrName = pFeaLyr.Name;
                //清空
                label1.Text = (pFeaLyr.FeatureClass as IDataset).Workspace.PathName;//显示图层的路径
                if (sSelectLyrName == sFeaLyrName)//判断sSelectLyrName是否与sFeaLyrName相同
                {
                    pSelectLyr = pFeaLyr;//pFeaLyr赋给pSelectLyr,跳出循环,进行下一步
                    break;
                }
            }
            IFeatureClass pFeaCls = pSelectLyr.FeatureClass;//把所选的图层的.FeatureClass赋给pFeaCls。
            for (int j=0; j < pFeaCls.Fields.FieldCount;j++ )
            {
                IField pFld = pFeaCls.Fields.get_Field(j);//选第j个属性表
                string sFidName = pFld.Name;//获得字段名
                string sFidLength = pFld.Length.ToString();//获得字段长度
                string sLine = sFidName + "    " + sFidLength + "\r";
                richTextBox1.Text = richTextBox1.Text + sLine;
            }
            RefreshDate();
        }


        public void RefreshDate()
        {
            treeList1.ClearNodes();//清空treelist1
            if (pMap != null)
            {
                IFeatureClass pFeaCls = pSelectLyr.FeatureClass;
                treeList1.BeginUpdate();
                try
                {
                    //添加表头
                    for (int i = 0; i < pFeaCls.Fields.FieldCount; i++)
                    {
                        IField pFid = pFeaCls.Fields.get_Field(i);
                        treeList1.Columns.Add();
                        treeList1.Columns[i].Caption = pFid.Name;
                        treeList1.Columns[i].VisibleIndex = i;
                    }
                    IFeatureCursor pFeaCur = pFeaCls.Search(null, false);
                    IFeature pFea = pFeaCur.NextFeature();
                    int k = 0;
                    while (pFea != null)
                    {
                        string sFirseFidValue = (pFea.get_Value(0)).ToString();
                        treeList1.AppendNode(new object[] { sFirseFidValue }, null);
                        for (int j = 1; j < pFeaCls.Fields.FieldCount; j++)
                        {
                            object pFidValue;
                            if (pSelectLyr.FeatureClass.Fields.get_Field(j).Name == "Shape")
                            {
                                pFidValue = pFea.Shape.GeometryType.ToString();
                            }
                            else
                            {
                                pFidValue = pFea.get_Value(j);
                            }
                            treeList1.Nodes[k][j] = pFidValue;
                        }
                        pFea = pFeaCur.NextFeature();
                        k = k + 1;
                    }


                }
                catch (Exception exc)
                {
                    MessageBox.Show("错误" + exc.ToString());
                }
                finally
                {
                    treeList1.EndUpdate();
                }
            }
        }
        //删除
        private void simpleButton2_Click(object sender, EventArgs e)
        {


            IFeatureLayer pFeaLyr = pSelectLyr as IFeatureLayer;
            for (int i = 0; i < this.treeList1.Columns.Count; i++)
            {
                // for(int i=0;i<lsv.SelectedItems.Count;i++ )
                string sFId = treeList1.Nodes[i].GetValue(0).ToString();
                IQueryFilter pQf = new QueryFilterClass();
                pQf.WhereClause = pFeaLyr.FeatureClass.OIDFieldName + "=" + sFId;
                IFeatureCursor pCur = pFeaLyr.FeatureClass.Update(pQf, false);
                IFeature pFea = pCur.NextFeature();


                if (pFea != null)
                {
                    IDataset pDs = pFeaLyr.FeatureClass as IDataset;
                    IWorkspace pWk = pDs.Workspace;
                    IWorkspaceEdit pWke = pWk as IWorkspaceEdit;


                    pWke.StartEditing(false);
                    pWke.StartEditOperation();


                    bool blok = true;
                    try
                    {
                        //pFea.Delete();
                        pCur.DeleteFeature();
                        pFea = pCur.NextFeature();
                    }
                    catch
                    {
                        blok = false;
                    }
                    finally
                    {
                        if (blok)
                        {
                            pWke.StopEditOperation();


                        }
                        else
                        {
                            pWke.AbortEditOperation();
                            pWke.StopEditing(blok);
                        }
                    }
                }
            }
            RefreshDate();
        }
        //编辑
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            if (treeList1.Selection.Count > 0)
            {
                string sObjectID = treeList1.Selection[0].GetValue(0).ToString();
                edit frm = new edit();


                IFeatureClass pFeaCls = pSelectLyr.FeatureClass;
                IQueryFilter pQf = new QueryFilterClass();
                pQf.WhereClause = pFeaCls.OIDFieldName + "=" + sObjectID;


                IFeatureCursor pFeacur = pFeaCls.Update(pQf, false);
                IFeature pFea = pFeacur.NextFeature();
                if (pFea != null)
                {
                    frm.m_Feature = pFea;
                    if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        IWorkspace pWks = (pFeaCls as IDataset).Workspace;
                        IWorkspaceEdit pWke = pWks as IWorkspaceEdit;
                        pWke.StartEditing(false);
                        pWke.StartEditOperation();
                        bool blOK = false;
                        string sError = "";
                        try
                        {
                            for (int i = 0; i < frm.treeList1.Nodes.Count; i++)
                            {
                                string sFldName = frm.treeList1.Nodes[i].GetValue(0).ToString();
                                string sFldValue = frm.treeList1.Nodes[i][1].ToString();
                                int iFldIdx = pFea.Fields.FindField(sFldName);
                                if (iFldIdx >= 0)
                                {
                                    pFea.set_Value(iFldIdx, sFldValue);
                                }
                            }
                            pFea.Store();
                            blOK = true;
                        }
                        catch (Exception ex)
                        {
                            sError = ex.Message;
                            blOK = false;
                        }
                        finally
                        {
                            if (blOK)
                                (pWks as IWorkspaceEdit).StopEditOperation();
                            else
                                (pWks as IWorkspaceEdit).AbortEditOperation();


                            (pWks as IWorkspaceEdit).StopEditing(blOK);


                            if (blOK)
                                RefreshDate();
                            else
                                MessageBox.Show("保存失败,来自异常:" + sError);
                        }
                    }
                }
            }
            else
                MessageBox.Show("请选择要编辑的要素!");
        }  
    }
}

猜你喜欢

转载自blog.csdn.net/qq_40216244/article/details/78880947