using System; using System.Data; using System.Windows.Forms; using Dongke.WinForm.Controls; using Dongke.WinForm.Utilities; namespace Dongke.IBOSS.PRD.Client.Controls.SearchBox { /// /// 查询窗体 /// public partial class SearchBoxForm : FormDialog, ISearchBoxForm { #region 成员变量 /// /// 指示是否能够多项选择。 /// private bool _multiSelect = false; /// /// 查询的范围权限类型 /// private PurviewType _purviewType = PurviewType.None; /// /// 选定的数据 /// private DataTable _checkedData = null; /// /// 查询结果的主键 /// private string _pk = null; /// /// 扩展属性 /// private ExtendedProperties _properties = null; #endregion #region 构造函数 /// /// 查询窗体 /// protected SearchBoxForm() { InitializeComponent(); if (!this.DesignMode) { this.ResetFormLayout(); } } #endregion #region 属性 /// /// 获取或设置查询结果的主键。 /// public string PKMember { get { return this._pk; } set { if (this._pk != value) { if (this._checkedData != null && !string.IsNullOrEmpty(value)) { if (this._checkedData.Columns.Contains(value)) { this._checkedData.PrimaryKey = new DataColumn[] { this._checkedData.Columns[value] }; } else { return; } } this._pk = value; } } } /// /// 获取或设置一个值,该值指示是否能够多项选择。 /// public bool MultiSelect { get { return this._multiSelect; } set { if (this._multiSelect != value) { this._multiSelect = value; this.ResetFormLayout(); } } } /// /// 获取或设置限制查询的范围权限类型。 /// public PurviewType PurviewType { get { return this._purviewType; } set { if (this._purviewType != value) { this._purviewType = value; } } } /// /// 获取选定的数据项。 /// public DataTable CheckedData { get { return this._checkedData; } } /// /// 获取或设置其他自定义属性。 /// public ExtendedProperties Properties { get { if (this._properties == null) { this._properties = new ExtendedProperties(); } return this._properties; } set { this._properties = value; } } /// /// 获取其他自定义属性个数。 /// public int PropertieCount { get { if (this._properties == null) { return 0; } return this._properties.Count; } } #endregion #region 公有方法 /// /// 窗体中数据保存后,需要继续操作,并初始化窗体时调用。 /// public override void RefreshData() { base.RefreshData(); this.dgvChecked.DataSource = null; } /// /// 点击查询按钮时调用。 /// /// 操作结果 public override bool QueryData() { bool result = base.QueryData(); DataTable data = this.dgvSelected.DataSource as DataTable; if (data != null && !string.IsNullOrEmpty(this._pk)) { data.PrimaryKey = new DataColumn[] { data.Columns[this._pk] }; } if (this._multiSelect && !data.Columns.Contains("SEL")) { DataColumn col = new DataColumn("SEL", typeof(int)); col.DefaultValue = 0; data.Columns.Add(col); data.AcceptChanges(); } if (this.dgvSelected.RowCount > 0) { this.dgvSelected.Rows[0].Selected = true; } this.dgvSelected.IsSetInputColumnsColor = true; return result; } /// /// 设置查询条件 /// public virtual void SetConditions(params object[] values) { if (values.Length > 0) { //this.txtCondition1.Text = values[0].ToString(); } } /// /// 清除查询条件 /// public override void ClearConditions() { //this.txtCondition1.Clear(); } /// /// 清除查询结果 /// public override void ClearQueryResults() { this.dgvSelected.DataSource = null; } /// /// 根据多选、单选设定窗体显示样式 /// public virtual void ResetFormLayout() { if (this._multiSelect) { // 多选 this.dgvChecked.Visible = true; this.colChk.Visible = true; this.dgvSelected.SelectionMode = DataGridViewSelectionMode.RowHeaderSelect; this.colSel.Visible = true; this.dgvSelected.ReadOnly = false; this.splSplitContainer.Panel2Collapsed = false; this.splSplitContainer.IsSplitterFixed = false; } else { // 单选 this.dgvChecked.Visible = false; this.colChk.Visible = false; this.dgvSelected.SelectionMode = DataGridViewSelectionMode.FullRowSelect; this.colSel.Visible = false; this.dgvSelected.ReadOnly = true; this.splSplitContainer.Panel2Collapsed = true; this.splSplitContainer.IsSplitterFixed = true; } } #endregion #region 保护方法 /// /// 窗体加载前调用(设置Form.Text、按钮等控件Text等)。 /// protected override void InitForm() { this.tsbtnAdaptive.Text = Dongke.IBOSS.PRD.Basics.BaseResources.Constant.TSBTN_ADAPTIVE; this.tsbtnSearch.Text = Dongke.IBOSS.PRD.Basics.BaseResources.Constant.BTN_SEARCH; this.tsbtnClearCondition.Text = Dongke.IBOSS.PRD.Basics.BaseResources.Constant.BTN_CLEARCONDITION; this.btnOK.Text = Dongke.IBOSS.PRD.Basics.BaseResources.Constant.BTN_SUBMIT; this.btnCancel.Text = Dongke.IBOSS.PRD.Basics.BaseResources.Constant.BTN_CANCEL; this.btnCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; this.btnOK.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; this.dgvChecked.AutoGenerateColumns = false; this.dgvSelected.AutoGenerateColumns = false; // 不显示选过的 foreach (DataGridViewColumn item in this.dgvSelected.Columns) { if (item.Name == "colSel") { continue; } DataGridViewColumn col = (DataGridViewColumn)item.Clone(); col.Name = "c" + col.Name; this.dgvChecked.Columns.Add(col); } } /// /// 窗体加载时调用(获取窗体初始化数据等)。 /// protected override bool LoadForm() { //if (this._multiSelect) //{ // if (this._checkedData != null && this._checkedData.Rows.Count > 0) // { // this.dgvChecked.DataSource = this._checkedData; // this.colChk.HeaderCellEx.CheckedAllState = CheckState.Checked; // } //} return true; } /// /// 验证窗体中数据是否改变。 /// /// 数据被更改true,其他false protected override bool CheckDirty() { return false; } /// /// 验证画面输入项目 /// /// 验证通过true,其他false protected override bool CheckInputData() { return true; } /// /// 保存或设置数据到其他位置(其他画面或DB) /// /// 保存或设置成功true,其他false protected override bool SetDataToOther() { this._checkedData = null; if (this._multiSelect) { //this.dgvSelected.EndEdit(); DataTable data = this.dgvSelected.DataSource as DataTable; if (data != null) { data.DefaultView.Sort = ""; data.DefaultView.RowFilter = colSel.DataPropertyName + " = 1"; this._checkedData = data.DefaultView.ToTable(); this._checkedData.PrimaryKey = new DataColumn[] { this._checkedData.Columns[this._pk] }; } //this.dgvChecked.EndEdit(); data = this.dgvChecked.DataSource as DataTable; if (data != null) { data.DefaultView.Sort = ""; data.DefaultView.RowFilter = this.colChk.DataPropertyName + " = 1"; if (this._checkedData == null) { this._checkedData = data.DefaultView.ToTable(); this._checkedData.PrimaryKey = new DataColumn[] { this._checkedData.Columns[this._pk] }; } else { this._checkedData.Merge(data.DefaultView.ToTable()); } } //this.dgvSelected.DataSource = null; //this.dgvChecked.DataSource = null; } else { if (this.dgvSelected.SelectedRows != null && this.dgvSelected.SelectedRows.Count > 0) { DataTable data = this.dgvSelected.DataSource as DataTable; DataRowView rowView = this.dgvSelected.SelectedRows[0].DataBoundItem as DataRowView; if (data != null && rowView != null) { this._checkedData = data.Clone(); this._checkedData.ImportRow(rowView.Row); } } } return true; } /// /// 保存或设置数据成功后提示消息 /// protected override void ShowMessageOnSetSucceed() { } /// /// 保存或设置数据成功后提示消息(新建或复制时,询问是否继续操作) /// /// 继续操作true,其他false protected override bool ShowMessageOnAddSucceed() { return false; } /// /// 验证查询条件。 /// /// 验证通过true,其他false protected override bool CheckQueryConditions() { return true; } /// /// 查询数据。 /// /// 验证通过true,其他false protected override bool QueryDataFromOther() { return true; } #endregion #region 事件处理 /// /// 自适应列宽 /// /// /// private void tsbtnAdaptive_Click(object sender, EventArgs e) { this.dgvSelected.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells); this.dgvChecked.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells); } /// /// 点击查询按钮 /// /// /// private void btnQuery_Click(object sender, System.EventArgs e) { try { this.QueryData(); } catch { } } /// /// 点击清除按钮 /// /// /// private void btnClear_Click(object sender, System.EventArgs e) { try { this.ClearConditions(); } catch { } } /// /// 点击确定按钮 /// /// /// private void btnOK_Click(object sender, System.EventArgs e) { try { this.SetData(); } catch { } } /// /// 点击取消按钮 /// /// /// private void btnCancel_Click(object sender, System.EventArgs e) { this.Close(); } /// /// 双击选择 /// /// /// private void dgvSelected_CellDoubleClick(object sender, DataGridViewCellEventArgs e) { try { if (this._multiSelect || e.ColumnIndex < 0 || e.RowIndex < 0) { return; } this.SetData(); } catch { } } #endregion private void dgvSelected_Sorted(object sender, EventArgs e) { this.dgvSelected.IsSetInputColumnsColor = true; } private void dgvChecked_Sorted(object sender, EventArgs e) { this.dgvChecked.IsSetInputColumnsColor = true; } } }