Newer
Older
GHFX_REFACTOR / FrmBasicQuery.cs
xiaowei on 27 Oct 2016 10 KB yxw20161027修改查询功能
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 DevComponents.DotNetBar;
using GeoScene.Data;
using GeoScene.Globe;
using GeoScene.Engine;
namespace Cyberpipe
{
    public partial class FrmBasicQuery : Office2007Form
    {
        private GSOGlobeControl globeControl1;
        GSOFeatureDataset sourcefDataset;
        private GSODataset ds;
        static FrmBasicQuery frm;

        private MainFrm.DataGridViewDelegate m_InitDataGridViewX1;
        DataTable table = new DataTable();
        string strLable = "";
        /// <summary>
        /// 单例模式 创建并显示窗体
        /// </summary>
        /// <param name="_ctl"></param>
        /// <param name="_dataGirdView"></param>
        /// <param name="p"></param>
        /// <param name="t1"></param>
        /// <param name="t2"></param>
        public static void ShowForm(GSOGlobeControl _ctl, MainFrm.DataGridViewDelegate InitDataGridViewX1)
        {
            if (frm == null)
            {
                frm = new FrmBasicQuery(_ctl, InitDataGridViewX1);
                frm.Show(_ctl.Parent);
            }
            else
            {
                if (frm.WindowState == FormWindowState.Minimized)
                {
                    frm.WindowState = FormWindowState.Normal;
                }
            }
        }

        public FrmBasicQuery(GSOGlobeControl _ctl, MainFrm.DataGridViewDelegate InitDataGridViewX1)
        {
            InitializeComponent();
            globeControl1 = _ctl;
            m_InitDataGridViewX1 = InitDataGridViewX1;
        }
        /// <summary>
        /// 选择字段下拉框选中项改变事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void comboBoxEx1_SelectedIndexChanged(object sender, EventArgs e)
        {
            checkBoxX1.Enabled = true;
            checkBoxX2.Enabled = false;
            checkBoxX3.Enabled = false;
            checkBoxX4.Enabled = false;
            checkBoxX5.Enabled = false;
            checkBoxX6.Enabled = false;
            cbxFields.Items.Clear();
            lbxFieldValues.Items.Clear();

            GSOLayer m_layer = globeControl1.Globe.Layers.GetLayerByCaption(cbxLayers.SelectedItem.ToString().Trim());//获取当前选择的layer图层
            if (m_layer == null)
                return;
            GSOFeatureLayer flayer = m_layer as GSOFeatureLayer;
            ds = m_layer.Dataset as GSODataset;
            sourcefDataset = ds as GSOFeatureDataset;
            sourcefDataset.Open();

            for (int j = 0; j < sourcefDataset.FieldCount; j++)
            {
                GSOFieldAttr fieldef = sourcefDataset.GetField(j);
                cbxFields.Items.Add(fieldef.Name);
            }

            cbxFields.SelectedIndex = 0;
        }
        string str1;
        string str2;
        /// <summary>
        /// 字段名列表选中项改变事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbxFields_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbxLayers.Text != "" && cbxFields.Text != "")
            {
                checkBoxX1.Enabled = true;
                checkBoxX2.Enabled = false;
                checkBoxX3.Enabled = false;
                checkBoxX4.Enabled = false;
                checkBoxX5.Enabled = false;
                checkBoxX6.Enabled = false;
                lbxFieldValues.Items.Clear();
                for (int j = 0; j < sourcefDataset.FieldCount; j++)
                {
                    GSOFieldAttr fieldef = sourcefDataset.GetField(j);
                    if (cbxFields.SelectedItem.ToString() == fieldef.Name && fieldef.Type == EnumFieldType.Text)
                    {
                        str1 = "'";
                        str2 = "'";
                    }
                }

                GSOLayer layer = globeControl1.Globe.Layers.GetLayerByCaption(cbxLayers.Text.Trim());
                if (layer == null)
                {
                    MessageBox.Show("图层不存在", "提示");
                    return;
                }
                string sql = "select distinct " + cbxFields.Text + " from " + layer.Name;
                DataTable table = OledbHelper.QueryTable(sql);

                for (int i = 0; i < table.Rows.Count; i++)
                {
                    DataRow dr = table.Rows[i];
                    string colString1 = dr[0].ToString();
                    if (colString1 == null || colString1.Trim() == "")
                    {
                        continue;
                    }
                    string col = str1 + colString1 + str2;
                    lbxFieldValues.Items.Add(col);
                }
                if (table.Rows[0][0].GetType() == typeof(string))
                    {
                        checkBoxX1.Enabled = true;
                        checkBoxX2.Enabled = false;
                        checkBoxX3.Enabled = false;
                        checkBoxX4.Enabled = false;
                        checkBoxX5.Enabled = false;
                        checkBoxX6.Enabled = true;
                    }
                    else if (table.Rows[0][0].GetType() == typeof(int) || table.Rows[0][0].GetType() == typeof(float) || table.Rows[0][0].GetType() == typeof(double))
                    {
                        checkBoxX1.Enabled = true;
                        checkBoxX2.Enabled = true;
                        checkBoxX3.Enabled = true;
                        checkBoxX4.Enabled = true;
                        checkBoxX5.Enabled = true;
                        checkBoxX6.Enabled = true;
                    }
                    else if (table.Rows[0][0].GetType() == typeof(DateTime))
                    {
                        checkBoxX1.Enabled = true;
                        checkBoxX2.Enabled = true;
                        checkBoxX3.Enabled = true;
                        checkBoxX4.Enabled = true;
                        checkBoxX5.Enabled = true;
                        checkBoxX6.Enabled = false;
                    }

                str1 = "";
                str2 = "";
            }
        }
        /// <summary>
        /// 窗体初始化事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmBasicQuery_Load(object sender, EventArgs e)
        {

            for (int i = 0; i < globeControl1.Globe.Layers.Count; i++)//获取树节点后中所有的layer图层
            {
                GSOLayer layer = globeControl1.Globe.Layers[i];
                if (layer != null && layer.Dataset != null && layer.Dataset.IsFeatureDataset)
                {
                    string name = layer.Name;
                    if (layer.Type == EnumLayerType.FeatureLayer && !name.Contains("\\")&&!name.Contains("施工"))
                    {
                        if(name.EndsWith("管线")||name.EndsWith("附属物")||name.EndsWith("管点"))
                        cbxLayers.Items.Add(name);
                    }
                }
            }
        }
        /// <summary>
        /// 查询按钮事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonX1_Click(object sender, EventArgs e)
        {
            if (cbxLayers.SelectedIndex == -1)
            {
                MessageBox.Show("请选择图层!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (cbxFields.SelectedIndex == -1)
            {
                MessageBox.Show("请选择字段!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (lbxFieldValues.SelectedItem != null)
            {
                string s = "";
                if (checkBoxX1.Checked)
                {
                    s = "=";
                }
                else if (checkBoxX2.Checked)
                {
                    s = "<";
                }
                else if (checkBoxX3.Checked)
                {
                    s = ">";
                }
                else if (checkBoxX4.Checked)
                {
                    s = "<=";
                }
                else if (checkBoxX5.Checked)
                {
                    s = ">=";
                }
                else if (checkBoxX6.Checked)
                {
                    s = "<>";
                }
                GSOLayer layer = globeControl1.Globe.Layers.GetLayerByCaption(cbxLayers.Text.Trim());
                if (layer == null)
                {
                    MessageBox.Show("图层不存在", "提示");
                    return;
                }
                string sql = "select " + getpipeLineFields.getFields(cbxLayers.Text, globeControl1) + " from " + layer.Name + " where " + cbxFields.Text + s + lbxFieldValues.SelectedItem.ToString();
                table = OledbHelper.QueryTable(sql);
                if (table != null && table.Rows.Count > 0)
                {
                    strLable = cbxLayers.Text + " 共有:" + table.Rows.Count + "个";
                    m_InitDataGridViewX1(table, strLable, cbxLayers.Text, true);
                }
                else
                {
                    MessageBox.Show("没有查找到符合条件的数据!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }
            else
            {
                MessageBox.Show("请选择字段值!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

        }
        /// <summary>
        /// 窗体关闭事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmBasicQuery_FormClosing(object sender, FormClosingEventArgs e)
        {
            m_InitDataGridViewX1(table, strLable, "", false);
            frm = null;
        }
        /// <summary>
        /// 取消按钮事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonX3_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}