using CnasSynchronusClient; using CnasSynchrousModel; 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; namespace CNAS_DBSync { public delegate void SourceDataFilterHandler(SourceDataFilter sourceDataFilter); //声明一个委托,用于向父窗口传输值 public partial class frmSourceFilter : Form { public SourceDataFilterHandler sourceDataFilterHandler; //在子窗口中定义一个SourceDataFilterHandler类型委托 public SyncInstrumentItemInfo syncInstrumentItem = new SyncInstrumentItemInfo(); public DataTable dtCurrentTableStruct = new DataTable(); //当前表的列和数据类型 private ComboBox cbxTableColumns; //定义一个ComboBox,用于选择列 private ComboBox cbxAlgorithm; //定义一个ComboBox, 用于根据不同的列绑定不同的条件 private ComboBox cbxConditionValue; //定义一个ComboBox,用于显示输入条件值的方式 private TextBox txtConditionValue; //定义一个TextBox,用于输入自定义的条件值 private List lstIntOrDouble = new List() { "等于", "不等于", "大于", "小于", "不大于", "不小于" }; private List lstText = new List() { "等于", "不等于", "包含", "不包含" }; private List lstConditionValueType = new List() { "{自定义...}","RegEx:{全数字}", "RegEx:{英文+数字}","RegEx:{中文+数字}" }; public string strTableInfoMode { get; set; } public List lstFilterConditions { get; set; } //临时过滤条件 public frmSourceFilter(SyncInstrumentItemInfo syncInstrumentItem,string strTableInfoMode) { InitializeComponent(); this.syncInstrumentItem = syncInstrumentItem; this.strTableInfoMode = strTableInfoMode; if (syncInstrumentItem.SourceFilter == null || syncInstrumentItem.SourceFilter.lstFilterConditions == null) this.lstFilterConditions = new List(); else this.lstFilterConditions =GlobalCommonOperation.Clone(syncInstrumentItem.SourceFilter.lstFilterConditions); dgvConditions.AutoGenerateColumns = false; } private void frmSourceFilter_Load(object sender, EventArgs e) { if (syncInstrumentItem == null) return; if (syncInstrumentItem.LstSyncPramas == null) return; if (syncInstrumentItem.LstSyncPramas.Count <= 0) return; //加载表名数据 this.txtCurrentTableName.Text = syncInstrumentItem.LstSyncPramas[0].SourceTable; //加载表结构(需要重新加载,因为父窗口可能没有数据或数据已经过时) if (strTableInfoMode == "0") { InstrumentData instrumentData = InstrumentDataFact.CreateInstrumentDataSource(syncInstrumentItem.SyncInstrumentDSInfo, new object[] { "", "", "" }); Dictionary dictInstruTables = instrumentData.GetInstrumentData(); if (dictInstruTables.ContainsKey(this.txtCurrentTableName.Text)) dtCurrentTableStruct = dictInstruTables[this.txtCurrentTableName.Text]; } else { InstrumentData instrumentData = InstrumentDataFact.CreateInstrumentDataSource(syncInstrumentItem.SyncInstrumentDSInfo, new object[] { this.txtCurrentTableName.Text, "", "" }); dtCurrentTableStruct = instrumentData.GetInstrumentDataStruct(); } //根据表结构手动构建ComboBox,用于用户选择增加列 InitalColumnComboBox(dtCurrentTableStruct); InitalAlgorithmComboBox(); InitalConditionValueComboBox(); IntitalConditionValueTextBox(); //加载过滤条件类型(与/或) if (syncInstrumentItem.SourceFilter == null) syncInstrumentItem.SourceFilter=new SourceDataFilter(); if (syncInstrumentItem.SourceFilter.FilterConditionLinkType == null) syncInstrumentItem.SourceFilter.FilterConditionLinkType = ""; this.cbxConditionLink.Text = ConvertLinkTypeToShow(syncInstrumentItem.SourceFilter.FilterConditionLinkType); //加载过滤条件 if (lstFilterConditions == null) lstFilterConditions=new List(); dgvConditions.DataSource = new BindingList(); dgvConditions.DataSource = new BindingList(lstFilterConditions); } private void InitalColumnComboBox(DataTable dtCurrentTableStruct) { cbxTableColumns = new ComboBox(); cbxTableColumns.SelectedIndexChanged += new EventHandler(cbxTableColumns_SelectedIndexChanged); cbxTableColumns.SelectionChangeCommitted += new EventHandler(cbxTableColumns_SelectionChangedCommitted); DataTable dtShow = new DataTable(); dtShow.Columns.Add("InstruFieldName"); dtShow.Columns.Add("InstruDataType"); foreach (DataColumn dc in dtCurrentTableStruct.Columns) { dtShow.Rows.Add(new object[] { dc.ColumnName, dc.DataType }); } cbxTableColumns.DataSource = dtShow; cbxTableColumns.DisplayMember = "InstruFieldName"; cbxTableColumns.ValueMember = "InstruDataType"; cbxTableColumns.Visible = false; } private void InitalAlgorithmComboBox() { cbxAlgorithm = new ComboBox(); cbxAlgorithm.SelectedIndexChanged += new EventHandler(cbxAlgorithm_SelectedIndexChanged); cbxAlgorithm.SelectionChangeCommitted += new EventHandler(cbxAlgorithm_SelectionChangedCommitted); cbxAlgorithm.Visible = false; } private void InitalConditionValueComboBox() { cbxConditionValue = new ComboBox(); cbxConditionValue.SelectedIndexChanged += new EventHandler(cbxConditionValue_SelectedIndexChanged); cbxConditionValue.SelectionChangeCommitted += new EventHandler(cbxConditionValue_SelectionChangedCommitted); cbxConditionValue.Visible = false; cbxConditionValue.DataSource = lstConditionValueType; } private void IntitalConditionValueTextBox() { txtConditionValue = new TextBox(); txtConditionValue.Leave += new EventHandler(txtConditionValue_Leave); } private void cbxConditionValue_SelectionChangedCommitted(object sender, EventArgs e) { cbxConditionValue.Visible = false; } private void cbxConditionValue_SelectedIndexChanged(object sender, EventArgs e) { //当前选中单元格的值将随之发生更改 if (dgvConditions.CurrentCell == null) return; string strConditionValue = ""; switch (cbxConditionValue.Text) { case "RegEx:{全数字}": strConditionValue = @"RegEx:^[1-9]\d*$"; dgvConditions.Rows[dgvConditions.CurrentCell.RowIndex].Cells["ConditionValue"].Value = strConditionValue; dgvConditions.Rows[dgvConditions.CurrentCell.RowIndex].Cells["Algorithm"].Value = "等于"; break; case "RegEx:{英文+数字}": strConditionValue = @"RegEx:[a-zA-Z][0-9]"; dgvConditions.Rows[dgvConditions.CurrentCell.RowIndex].Cells["ConditionValue"].Value = strConditionValue; dgvConditions.Rows[dgvConditions.CurrentCell.RowIndex].Cells["Algorithm"].Value = "等于"; break; case "RegEx:{中文+数字}": strConditionValue = @"RegEx:[\u4e00-\u9fa5][0-9]"; dgvConditions.Rows[dgvConditions.CurrentCell.RowIndex].Cells["ConditionValue"].Value = strConditionValue; dgvConditions.Rows[dgvConditions.CurrentCell.RowIndex].Cells["Algorithm"].Value = "等于"; break; case "{自定义...}": default: //显示textbox控件,输入后,离开时提交数据。否则不提交任何数据 ShowTextBox(dgvConditions.Rows[dgvConditions.CurrentCell.RowIndex].Cells["ConditionValue"].Value==null?"": dgvConditions.Rows[dgvConditions.CurrentCell.RowIndex].Cells["ConditionValue"].Value.ToString()); //dgvConditions.Rows[dgvConditions.CurrentCell.RowIndex].Cells["ConditionValue"].Value = strConditionValue; break; } } private void cbxAlgorithm_SelectionChangedCommitted(object sender, EventArgs e) { cbxAlgorithm.Visible = false; } private void cbxAlgorithm_SelectedIndexChanged(object sender, EventArgs e) { //当前选中单元格的值将随之发生更改 if (dgvConditions.CurrentCell == null) return; dgvConditions.Rows[dgvConditions.CurrentCell.RowIndex].Cells["Algorithm"].Value = cbxAlgorithm.Text; } private void cbxTableColumns_SelectionChangedCommitted(object sender, EventArgs e) { this.cbxTableColumns.Visible = false; } private void cbxTableColumns_SelectedIndexChanged(object sender, EventArgs e) { //当前选中单元格的值将随之发生更改 if (dgvConditions.CurrentCell == null) return; dgvConditions.Rows[dgvConditions.CurrentCell.RowIndex].Cells["ColumnName"].Value = cbxTableColumns.Text; dgvConditions.Rows[dgvConditions.CurrentCell.RowIndex].Cells["ColumnDataType"].Value = cbxTableColumns.SelectedValue; } private string ConvertLinkTypeToShow(string filterConditionLinkType) { string strReturnValue = ""; switch (filterConditionLinkType.ToLower()) { case "or": strReturnValue = "或"; break; case "and": default: strReturnValue = "与"; break; } return strReturnValue; } /// /// 新增一个条件 /// /// /// private void btnAdd_Click(object sender, EventArgs e) { //最多支持5个条件,否则无限制的条件将极大影响性能 if (lstFilterConditions.Count == 15) { MessageBox.Show("最多增加15项条件。"); return; } //添加一条空数据 FilterCondition condition = new FilterCondition(); condition.TableName = this.txtCurrentTableName.Text; lstFilterConditions.Add(condition); //重新绑定 dgvConditions.DataSource = new BindingList(); dgvConditions.DataSource = new BindingList(lstFilterConditions); //选择最后一条 dgvConditions.CurrentCell = dgvConditions.Rows[dgvConditions.Rows.Count - 1].Cells[1]; } private void btnDelete_Click(object sender, EventArgs e) { if (dgvConditions.CurrentCell == null) return; if (dgvConditions.Rows[dgvConditions.CurrentCell.RowIndex].Cells["ColumnName"].Value == null) return; if (dgvConditions.Rows[dgvConditions.CurrentCell.RowIndex].Cells["ConditionValue"].Value == null) return; if (dgvConditions.Rows[dgvConditions.CurrentCell.RowIndex].Cells["Algorithm"].Value == null) return; string strColumnName = dgvConditions.Rows[dgvConditions.CurrentCell.RowIndex].Cells["ColumnName"].Value.ToString(); string strConditionValue = dgvConditions.Rows[dgvConditions.CurrentCell.RowIndex].Cells["ConditionValue"].Value.ToString(); string strAlgorithm = dgvConditions.Rows[dgvConditions.CurrentCell.RowIndex].Cells["Algorithm"].Value.ToString(); var query = lstFilterConditions.Where(s => s.ColumnName == strColumnName && s.Algorithm == strAlgorithm && s.ConditionValue == strConditionValue); if (query.Count() > 0) { foreach (var item in query.ToList()) { lstFilterConditions.Remove(item); } dgvConditions.DataSource = new BindingList(lstFilterConditions); } } private void dgvConditions_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { cbxTableColumns.Visible = false; cbxAlgorithm.Visible = false; } private void dgvConditions_CellEndEdit(object sender, DataGridViewCellEventArgs e) { cbxTableColumns.Visible = false; cbxAlgorithm.Visible = false; } private void dgvConditions_CellDoubleClick(object sender, DataGridViewCellEventArgs e) { if (dgvConditions.CurrentCell == null) return; if (dgvConditions.CurrentCell.ColumnIndex == 0) //此时表示准备编辑列名 { ShowConditionColumnSelect(); } if (dgvConditions.CurrentCell.ColumnIndex == 1) //此时表示准备编辑条件 { ShowConditionAlgorithm(); } if (dgvConditions.CurrentCell.ColumnIndex == 2) //此时表示准备编辑条件值 { ShowConditionValue(); } } /// /// 显示条件列 /// private void ShowConditionColumnSelect() { Rectangle TmpRect = dgvConditions.GetCellDisplayRectangle(dgvConditions.CurrentCell.ColumnIndex, dgvConditions.CurrentCell.RowIndex, true); cbxTableColumns.Size = TmpRect.Size; cbxTableColumns.Top = TmpRect.Top; cbxTableColumns.Left = TmpRect.Left; cbxTableColumns.DropDownStyle = ComboBoxStyle.DropDownList; cbxTableColumns.FormattingEnabled = true; cbxTableColumns.Visible = true; if (!this.dgvConditions.Controls.Contains(cbxTableColumns)) this.dgvConditions.Controls.Add(cbxTableColumns); } /// /// 显示选择运算条件 /// private void ShowConditionAlgorithm() { //1.加载数据 string strColumnName = ""; if (dgvConditions.Rows[dgvConditions.CurrentCell.RowIndex].Cells["ColumnName"].Value != null) strColumnName = dgvConditions.Rows[dgvConditions.CurrentCell.RowIndex].Cells["ColumnName"].Value.ToString(); if (strColumnName != "") { string strColumnType = dtCurrentTableStruct.Columns[strColumnName].DataType.ToString(); switch (strColumnType) { case "System.DateTime": case "System.Double": case "System.Int64": case "System.Int32": case "System.Decimal": cbxAlgorithm.DataSource = lstIntOrDouble; break; case "System.Byte[]": case "System.String": default: cbxAlgorithm.DataSource = lstText; break; } } //2.在合适的位置显示控件 Rectangle TmpRect = dgvConditions.GetCellDisplayRectangle(dgvConditions.CurrentCell.ColumnIndex, dgvConditions.CurrentCell.RowIndex, true); cbxAlgorithm.Size = TmpRect.Size; cbxAlgorithm.Top = TmpRect.Top; cbxAlgorithm.Left = TmpRect.Left; cbxAlgorithm.DropDownStyle = ComboBoxStyle.DropDownList; cbxAlgorithm.FormattingEnabled = true; cbxAlgorithm.Visible = true; if (!this.dgvConditions.Controls.Contains(cbxAlgorithm)) this.dgvConditions.Controls.Add(cbxAlgorithm); } private void ShowConditionValue() { Rectangle TmpRect = dgvConditions.GetCellDisplayRectangle(dgvConditions.CurrentCell.ColumnIndex, dgvConditions.CurrentCell.RowIndex, true); cbxConditionValue.Size = TmpRect.Size; cbxConditionValue.Top = TmpRect.Top; cbxConditionValue.Left = TmpRect.Left; cbxConditionValue.DropDownStyle = ComboBoxStyle.DropDownList; cbxConditionValue.FormattingEnabled = true; cbxConditionValue.Visible = true; if (!this.dgvConditions.Controls.Contains(cbxConditionValue)) this.dgvConditions.Controls.Add(cbxConditionValue); } private void ShowTextBox(string strConditionValue) { Rectangle TmpRect = dgvConditions.GetCellDisplayRectangle(dgvConditions.CurrentCell.ColumnIndex, dgvConditions.CurrentCell.RowIndex, true); txtConditionValue.Size = TmpRect.Size; txtConditionValue.Top = TmpRect.Top; txtConditionValue.Left = TmpRect.Left; txtConditionValue.Visible = true; txtConditionValue.Text = strConditionValue; if (!this.dgvConditions.Controls.Contains(txtConditionValue)) this.dgvConditions.Controls.Add(txtConditionValue); } private void txtConditionValue_Leave(object sender, EventArgs e) { txtConditionValue.Visible = false; dgvConditions.Rows[dgvConditions.CurrentCell.RowIndex].Cells["ConditionValue"].Value = txtConditionValue.Text.Trim(); } private void btnOK_Click(object sender, EventArgs e) { //初始化一个过滤器 SourceDataFilter dataFilter = new SourceDataFilter { FilterConditionLinkType = this.cbxConditionLink.Text=="与"?"and":"or", lstFilterConditions=new List() }; //添加过滤条件 foreach (DataGridViewRow dgr in dgvConditions.Rows) { if (dgr.Cells["ColumnName"].Value == null) continue; if (dgr.Cells["Algorithm"].Value == null) continue; if (dgr.Cells["ColumnDataType"].Value == null) continue; if (dgr.Cells["ConditionValue"].Value == null) //此时需要判断列的数据类型 { ////如果当前选择的条件列是字符串类型,则认为此处是空值,否则,必须手动输入值才能正常存储 //if (dgr.Cells["ColumnDataType"].Value.ToString().ToLower() != "system.string") continue; dgr.Cells["ConditionValue"].Value = ""; } string strColumnName = dgr.Cells["ColumnName"].Value.ToString(); string strConditionValue = dgr.Cells["ConditionValue"].Value.ToString(); string strAlgorithm = dgr.Cells["Algorithm"].Value.ToString(); string strColumnDataType = dgr.Cells["ColumnDataType"].Value.ToString(); FilterCondition condition = new FilterCondition { TableName = this.txtCurrentTableName.Text, ColumnName = strColumnName, ColumnDataType=strColumnDataType, Algorithm = ConvertShowAlgorithmToValue(strAlgorithm), ConditionValue = strConditionValue }; if (!dataFilter.lstFilterConditions.Contains(condition)) dataFilter.lstFilterConditions.Add(condition); } //使用委托返回过滤器 sourceDataFilterHandler(dataFilter); this.Close(); } private string ConvertShowAlgorithmToValue(string strAlgorithm) { string strValue = ""; switch (strAlgorithm) { case "等于": strValue = "="; break; case "不等于": strValue = "<>"; break; case "包含": strValue = "like"; break; case "不包含": strValue = "not like"; break; case "大于": strValue = ">"; break; case "小于": strValue = "<"; break; case "不大于": strValue = "<="; break; case "不小于": strValue = ">="; break; default: strValue = strAlgorithm; break; } return strValue; } private string ConvertAlgorithmValueToShow(string Algorithm) { string strValue = ""; switch (Algorithm) { case "=": strValue = "等于"; break; case "<>": strValue = "不等于"; break; case "like": strValue = "包含"; break; case "not like": strValue = "不包含"; break; case ">": strValue = "大于"; break; case "<": strValue = "小于"; break; case "<=": strValue = "不大于"; break; case ">=": strValue = "不小于"; break; } return strValue; } private void dgvConditions_CellClick(object sender, DataGridViewCellEventArgs e) { if (cbxTableColumns != null) cbxTableColumns.Visible = false; if (cbxAlgorithm != null) cbxAlgorithm.Visible = false; if (cbxConditionValue != null) cbxConditionValue.Visible = false; if (txtConditionValue != null) txtConditionValue.Visible = false; } private void dgvConditions_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { if (e.ColumnIndex == 1&&e.Value!=null) { string strShowValue = ConvertAlgorithmValueToShow(e.Value.ToString()); e.Value = strShowValue != ""?strShowValue: e.Value; } } #region 判断DGV是否点击空白 public int GetRowIndexAt(int mouseLocation_Y,DataGridView dgv) { if (dgv.FirstDisplayedScrollingRowIndex < 0) { return -1; } if (dgv.ColumnHeadersVisible == true && mouseLocation_Y <= dgv.ColumnHeadersHeight) { return -1; } int index = dgv.FirstDisplayedScrollingRowIndex; int displayedCount = dgv.DisplayedRowCount(true); for (int k = 1; k <= displayedCount;) { if (dgv.Rows[index].Visible == true) { Rectangle rect = dgv.GetRowDisplayRectangle(index, true); // 取该区域的显示部分区域 if (rect.Top <= mouseLocation_Y && mouseLocation_Y < rect.Bottom) { return index; } k++; } index++; } return -1; } #endregion private void dgvConditions_MouseClick(object sender, MouseEventArgs e) { if (GetRowIndexAt(e.Y, dgvConditions) == -1) { dgvConditions.EndEdit(); if(cbxTableColumns!=null) cbxTableColumns.Visible = false; if (cbxAlgorithm != null) cbxAlgorithm.Visible = false; if (cbxConditionValue != null) cbxConditionValue.Visible = false; if (txtConditionValue != null) txtConditionValue.Visible = false; } } } }