예제 #1
0
        /// <summary>
        /// 删除字段
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Delete_Click(object sender, EventArgs e)
        {
            if (null == this.dataGridView1.SelectedCells ||
                1 > this.dataGridView1.SelectedCells.Count)
            {
                MessageBox.Show("请选择要删除的列。");
                return;
            }
            //新建选择列列表
            List <DataGridViewColumn> colsToDel = new List <DataGridViewColumn>();

            for (int i = 0; i < this.dataGridView1.SelectedCells.Count; i++)
            {
                //获取到datagridView中选中的列信息
                var column = this.dataGridView1.Columns[this.dataGridView1.SelectedCells[i].ColumnIndex];
                if (!colsToDel.Contains(column))
                {
                    //添加列信息
                    colsToDel.Add(column);
                }
            }
            //提示是否删除
            if (MessageBox.Show(string.Format("是否删除{0}字段?\n此操作不可撤销。", string.Join(",", colsToDel.AsEnumerable().Select(c => c.Name))),
                                "消息", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                //获得所有要素
                GSOFeatures features = _layer.GetAllFeatures();
                //遍历选中列的列表
                foreach (var item in colsToDel)
                {
                    //遍历所有要素
                    for (int i = 0; i < features.Length; i++)
                    {
                        GSOFeature feature = features[i];
                        //获取选中列在要素属性表中的索引
                        int index = feature.GetFieldIndex(item.Name);
                        //删除该字段
                        feature.DeleteField(index);
                    }
                    //同时删除控件中的列
                    this.dataGridView1.Columns.Remove(item);
                }
                //保存图层
                _layer.Save();
            }
        }