||
1、属性表中添加一个toolstripcontainer,然后添加一个toolstrip1,用于放置一组dorpdownbutton;然后再添加一个gridview1,用于放置属性表。
2、orpdownbutton中添加两个按钮,分别为“添加字段”和“导出表格”,用于新建属性字段,以及属性表导出为excel。
3、添加字段功能:
(1)在添加字段按钮的代码中运行“新增字段窗口”
private void 添加字段ToolStripMenuItem_Click(object sender, EventArgs e) { IFeatureLayer pFLayer = pLayer as IFeatureLayer; formAddField formaddfield = new formAddField(pFLayer, dataGridView1); formaddfield.Show(); }
(2)新增字段窗口设计如下:
(3)新增字段窗体的代码如下:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using ESRI.ArcGIS.Carto; using ESRI.ArcGIS.Controls; using ESRI.ArcGIS.DataSourcesFile; using ESRI.ArcGIS.DataSourcesGDB; using ESRI.ArcGIS.DataSourcesRaster; using ESRI.ArcGIS.Display; using ESRI.ArcGIS.esriSystem; using ESRI.ArcGIS.Geometry; using ESRI.ArcGIS.Geodatabase; using ESRI.ArcGIS.GlobeCore; using ESRI.ArcGIS.Output; using ESRI.ArcGIS.SystemUI; using Microsoft.Office.Interop.Excel; namespace WindowsFormsApplication2_1_2 { public partial class formAddField : Form { private IFeatureLayer mFeatureLayer = null; private DataGridView mdgv; public formAddField(IFeatureLayer pFeatureLayer, DataGridView pdgv) { InitializeComponent(); mFeatureLayer = pFeatureLayer; mdgv = pdgv; } private void formAddField_Load(object sender, EventArgs e) { this.cmbFieldType.Items.Add("长整型"); this.cmbFieldType.Items.Add("短整型"); this.cmbFieldType.Items.Add("浮点型"); this.cmbFieldType.Items.Add("双精度"); this.cmbFieldType.Items.Add("文本型"); this.cmbFieldType.Items.Add("日期型"); this.cmbFieldType.SelectedIndex = 0; } private void cmbFieldType_SelectedIndexChanged(object sender, EventArgs e) { string strFieldType = cmbFieldType.Text; switch (strFieldType) { case "长整型": { panelPrecision.Visible = true; panelScale.Visible = false; break; } case "短整型": { panelPrecision.Visible = true; panelScale.Visible = false; break; } case "浮点型": { panelPrecision.Visible = true; panelScale.Visible = true; break; } case "双精度": { panelPrecision.Visible = true; panelScale.Visible = true; break; } case "文本型": { panelPrecision.Visible = true; panelScale.Visible = false; lblPrecision.Text = "长度"; break; } default://日期型0 { panelPrecision.Visible = false; panelScale.Visible = false; break; } } } private void button1_Click(object sender, EventArgs e) { string strFieldName = txtFieldName.Text; string strFieldType = cmbFieldType.Text; try { IFeatureLayer editAttributeLayer = mFeatureLayer; //Field collection IFieldsEdit pFieldsEdit; //获取FeatureLayer IFeatureLayer pFeatureLayer = editAttributeLayer; //从FeatureLayer获取工作空间 IDataset pDataSet = pFeatureLayer.FeatureClass as IDataset; IWorkspace pWorkSpace = pDataSet.Workspace; //设置字段属性 IField pNewField = new FieldClass(); IFieldEdit pFieldEdit = pNewField as IFieldEdit; pFieldEdit.AliasName_2 = strFieldName; pFieldEdit.Name_2 = strFieldName; switch (strFieldType) { case "长整型": { pFieldEdit.Type_2 = esriFieldType.esriFieldTypeInteger; pFieldEdit.Precision_2 = int.Parse(txtPrecision.Text); break; } case "Class1.cs短整型": { pFieldEdit.Type_2 = esriFieldType.esriFieldTypeSmallInteger; pFieldEdit.Precision_2 = int.Parse(txtPrecision.Text); break; } case "浮点型": { pFieldEdit.Type_2 = esriFieldType.esriFieldTypeSingle; pFieldEdit.Precision_2 = int.Parse(txtPrecision.Text); pFieldEdit.Scale_2 = int.Parse(txtScale.Text); break; } case "双精度": { pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble; pFieldEdit.Precision_2 = int.Parse(txtPrecision.Text); pFieldEdit.Scale_2 = int.Parse(txtScale.Text); break; } case "文本型": { pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString; pFieldEdit.Length_2 = int.Parse(txtPrecision.Text); break; } default://日期型0 { pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDate; break; } } //添加字段 try { int theField = pFeatureLayer.FeatureClass.Fields.FindField(strFieldName); if (theField == -1) { pFeatureLayer.FeatureClass.AddField(pFieldEdit); MessageBox.Show("字段添加成功!"); } else { MessageBox.Show("字段已经存在!"); } } catch (Exception ex) { MessageBox.Show("Field " + pFieldEdit.Name + " was not added due to an error (" + ex.Message + " )"); } } catch (System.Exception ex) { MessageBox.Show(ex.Message); } this.Close(); RefreshTable refresh = new RefreshTable(); refresh.Refresh(mdgv, mFeatureLayer); } private void button2_Click(object sender, EventArgs e) { this.Close(); } } }
(4)其中用到了刷新表格的类,新建一个类命名为RefreshTable,代码如下:
using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Windows.Forms; using ESRI.ArcGIS.Geodatabase; using ESRI.ArcGIS.Carto; namespace WindowsFormsApplication2_1_2 { class RefreshTable { //刷新属性表 public void Refresh(DataGridView dataGridView, IFeatureLayer pFeatureLayer) { IFeatureLayer pFLayer = pFeatureLayer; IFeatureClass pFeatureClass = pFLayer.FeatureClass; if (pFeatureClass == null) return; DataTable dt = new DataTable(); DataColumn dc = null; for (int i = 0; i < pFeatureClass.Fields.FieldCount; i++) { dc = new DataColumn(pFeatureClass.Fields.get_Field(i).Name); dt.Columns.Add(dc); } IFeatureCursor pFeatureCuror = pFeatureClass.Search(null, false); IFeature pFeature = pFeatureCuror.NextFeature(); DataRow dr = null; while (pFeature != null) { dr = dt.NewRow(); for (int j = 0; j < pFeatureClass.Fields.FieldCount; j++) { if (pFeatureClass.FindField(pFeatureClass.ShapeFieldName) == j) { dr[j] = pFeatureClass.ShapeType.ToString(); } else { dr[j] = pFeature.get_Value(j).ToString(); } } dt.Rows.Add(dr); pFeature = pFeatureCuror.NextFeature(); } dataGridView.DataSource = dt; } } }
4、导出表格功能:
(1)按钮中添加如下代码:
private void 导出ToolStripMenuItem_Click(object sender, EventArgs e) { IFeatureLayer pFLayer = pLayer as IFeatureLayer; IFeatureClass pFeatureClass = pFLayer.FeatureClass; IFields pFields = pFeatureClass.Fields; ExportExcel(dataGridView1, pFields); }
(2)其中调用了ExprotExcel(dataGridView1, pFields),其方法定义如下:
private void ExportExcel(DataGridView myDGV, IFields pFields) { string saveFileName = ""; SaveFileDialog saveDialog = new SaveFileDialog(); saveDialog.DefaultExt = "xlsx"; saveDialog.Filter = "Excel文件|*.xlsx"; saveDialog.ShowDialog(); saveFileName = saveDialog.FileName; if (saveFileName.IndexOf(":") < 0) return; //被点了取消 Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application(); if (xlApp == null) { MessageBox.Show("无法创建Excel对象,可能您的机子未安装Excel"); return; } Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks; Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet); Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];//取得sheet1 //写入标题 for (int i = 0; i < myDGV.ColumnCount; i++) { worksheet.Columns.Cells[1, i + 1] = myDGV.Columns[i].HeaderText; } //写入数值 for (int r = 0; r < myDGV.Rows.Count; r++) { for (int i = 0; i < myDGV.ColumnCount; i++) { worksheet.Cells[r + 2, i + 1] = myDGV.Rows[r].Cells[i].Value; } System.Windows.Forms.Application.DoEvents(); } worksheet.Columns.EntireColumn.AutoFit();//列宽自适应 if (saveFileName != "") { try { workbook.Saved = true; workbook.SaveCopyAs(saveFileName); } catch (Exception ex) { MessageBox.Show("导出文件时出错,文件可能正被打开!\n" + ex.Message); } xlApp.Quit(); GC.Collect();//强行销毁 MessageBox.Show("导出成功!", "提示", MessageBoxButtons.OK); } }
5、删除字段功能
该功能需要在某个字段段头上右键,弹出右键菜单,选择“删除字段”;
在属性表cell上右键的话,弹出的是另一个菜单,只显示“复制”;
在某一行的行头上右键,弹出的是第三个菜单,显示“删除行”。
(1)在gridview1中添加三个ContextMenuStrip,分别命名为column_header_menu、row_header_menu、cell_menu。分别添加“删除字段”、“删除行”、“复制”菜单。当然,还可以根据需要增加新的菜单,如“字段计算”等。
(2)由于在gridview1中调用ContextMenuStrip时软件有优先级限制,难以根据需要弹出不同的右键菜单,所以需要一个设定:
public int nColumnindex; private void dataGridView1_CellContextMenuStripNeeded(object sender, DataGridViewCellContextMenuStripNeededEventArgs e) { if (e.RowIndex == -1) { e.ContextMenuStrip = column_header_menu; nColumnindex = e.ColumnIndex; } else if (e.ColumnIndex == -1) { e.ContextMenuStrip = row_header_menu; } else { e.ContextMenuStrip = cell_menu; } }
(3)在“删除字段”中调用如下代码:
private void 删除字段ToolStripMenuItem_Click_1(object sender, EventArgs e) { string strField = dataGridView1.Columns[nColumnindex].HeaderText.ToString(); IFeatureLayer pFLayer = pLayer as IFeatureLayer; string strResult = ""; if ((MessageBox.Show("确定要删除该字段吗?", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)) { strResult = DeleteField(pFLayer, strField); dataGridView1.Columns.Remove(strField); MessageBox.Show(strResult, "提示", MessageBoxButtons.OK); } RefreshTable refresh = new RefreshTable(); refresh.Refresh(dataGridView1, pFLayer); }
其中调用了DeleteField(pFLayer, strField),该方法定义如下:
public string DeleteField(IFeatureLayer layer, string fieldName) { try { ITable pTable = (ITable)layer; IFields pfields; IField pfield; pfields = pTable.Fields; int fieldIndex = pfields.FindField(fieldName); pfield = pfields.get_Field(fieldIndex); pTable.DeleteField(pfield); return "删除成功!"; } catch (Exception ex) { return ex.Message; } }
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2024-9-27 06:57
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社