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; DevComponents.DotNetBar.Controls.DataGridViewX dataGridView1; PanelEx panel; ToolStripStatusLabel toolStripNumbers; ToolStripStatusLabel toolStripFeatureLength; static FrmBasicQuery frm; /// <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, DevComponents.DotNetBar.Controls.DataGridViewX _dataGirdView, PanelEx p, ToolStripStatusLabel t1, ToolStripStatusLabel t2) { if (frm == null) { frm = new FrmBasicQuery(_ctl, _dataGirdView, p, t1, t2); frm.Show(_ctl.Parent); } else { if (frm.WindowState == FormWindowState.Minimized) { frm.WindowState = FormWindowState.Normal; } } } public FrmBasicQuery(GSOGlobeControl _ctl,DevComponents.DotNetBar.Controls.DataGridViewX _dataGirdView,PanelEx p,ToolStripStatusLabel t1,ToolStripStatusLabel t2) { InitializeComponent(); globeControl1 = _ctl; dataGridView1 = _dataGirdView; panel = p; toolStripNumbers = t1; toolStripFeatureLength = t2; } /// <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); } string strFiled = cbxFields.SelectedItem.ToString(); if (table.Rows[0][strFiled].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][strFiled].GetType() == typeof(int) || table.Rows[0][strFiled].GetType() == typeof(float) || table.Rows[0][strFiled].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][strFiled].GetType() == typeof(decimal)) { 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("施工") && !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(); DataTable table = OledbHelper.QueryTable(sql); if (table != null && table.Rows.Count > 0) { panel.Visible = true; MainFrm.m_CurrentQueryLayer = cbxLayers.Text; dataGridView1.DataSource = table; toolStripNumbers.Text = cbxLayers.Text; toolStripFeatureLength.Text = " 共有:" + table.Rows.Count + "个"; } else { MessageBox.Show("没有查找到符合条件的数据!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); dataGridView1.DataSource = null; toolStripNumbers.Text = ""; toolStripFeatureLength.Text = " 管线里程:"; panel.Visible = false; } } 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) { dataGridView1.DataSource = null; dataGridView1.Refresh(); panel.Visible = false; toolStripNumbers.Text = ""; toolStripFeatureLength.Text = " 管线里程:"; frm = null; } /// <summary> /// 取消按钮事件处理 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void buttonX3_Click(object sender, EventArgs e) { this.Close(); } } }