AE开发第3.5天

1打开图层属性表(补全之前图层操作的内容)

这是form1的click事件,在form1点击,在form2查看属性表。

private void 打开图层属性表ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // 检查是否有图层被选中
            if (selectedLayer != null)
            {
                // 检查选中的图层是否为要素图层
                if (selectedLayer is IFeatureLayer featureLayer)
                {
                    // 获取要素图层的属性表
                    ITable table = featureLayer as ITable;

                    // 检查属性表是否存在
                    if (table != null)
                    {
                        using (Form2 form2 = new Form2())
                        {
                            // 将表格数据传递给 Form2
                            form2.LoadTableData(table);

                            // 显示 Form2
                            form2.ShowDialog();
                        }
                    }
                    else
                    {
                        MessageBox.Show("无法获取图层的属性表。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                else
                {
                    MessageBox.Show("选定的图层不是要素图层,无法打开属性表。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                MessageBox.Show("请先选择一个图层。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

form2要加一个DataGridView控件

using ESRI.ArcGIS.Geodatabase;
using System.Windows.Forms;

namespace AEproject2
{
    public partial class Form2 : Form
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.dataGridView1 = new System.Windows.Forms.DataGridView();
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
            this.SuspendLayout();
            // 
            // dataGridView1
            // 
            this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dataGridView1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.dataGridView1.Location = new System.Drawing.Point(0, 0);
            this.dataGridView1.Name = "dataGridView1";
            this.dataGridView1.RowHeadersWidth = 51;
            this.dataGridView1.RowTemplate.Height = 27;
            this.dataGridView1.Size = new System.Drawing.Size(800, 450);
            this.dataGridView1.TabIndex = 0;
            // 
            // Form2
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(800, 450);
            this.Controls.Add(this.dataGridView1);
            this.Name = "Form2";
            this.Text = "属性表";
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.DataGridView dataGridView1;

        // 方法用于加载并显示表格数据
        public void LoadTableData(ITable table)
        {
            // 检查表格是否为空
            if (table == null)
                return;

            // 清空 DataGridView 中的行和列
            dataGridView1.Rows.Clear();
            dataGridView1.Columns.Clear();

            // 添加列
            for (int i = 0; i < table.Fields.FieldCount; i++)
            {
                dataGridView1.Columns.Add(table.Fields.Field[i].Name, table.Fields.Field[i].Name);
            }

            // 添加行和单元格数据
            ICursor cursor = table.Search(null, false);
            IRow row;
            while ((row = cursor.NextRow()) != null)
            {
                object[] values = new object[table.Fields.FieldCount];
                for (int i = 0; i < table.Fields.FieldCount; i++)
                {
                    values[i] = row.Value[i];
                }
                dataGridView1.Rows.Add(values);
            }
            System.Runtime.InteropServices.Marshal.ReleaseComObject(cursor);

            // 调整列宽
            dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
        }

    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值