public static string BuildColumnFilter(string filterExpression, IList table) { if (table is DataView) { return(RowFilterBuilder.BuildColumnFilter(filterExpression, (DataView)table)); } else { var _typeOfList = table.GetType(); if (_typeOfList.IsGenericType) { var _genericArguments = _typeOfList.GetGenericArguments(); if (_genericArguments.Length > 0) { return(RowFilterBuilder.BuildColumnFilter(filterExpression, _genericArguments[0].GetProperties().Where(c => c.PropertyType != typeof(Guid) && c.PropertyType != typeof(Boolean)).Select(c => c.Name).ToArray())); } else { return(string.Empty); } } else { return(string.Empty); } } }
/// <summary> /// /// </summary> /// <param name="DataSource"></param> /// <param name="SelectedItem"></param> /// <param name="Columns"></param> private SelectItemForm(object DataSource, object SelectedItem, params SelectItemForm.ColumnDefinition[] Columns) { // setting up the bindingSource this.bindingSource = new BindingSource(); this.bindingSource.DataSource = DataSource; if (SelectedItem != null) { this.bindingSource.MoveLast(); while (this.bindingSource.Position > 0) { if (this.bindingSource.Current == SelectedItem) { break; } else { var drv = this.bindingSource.Current as DataRowView; if (drv != null && drv.Row == SelectedItem) { break; } if (drv != null && SelectedItem is DataRow && drv.Row.ItemArray.SequenceEqual(((DataRow)SelectedItem).ItemArray)) { break; } } this.bindingSource.MovePrevious(); } } // Setting up the controls this.dataGrid = new DataGridView(); if (this.bindingSource.SupportsFiltering) { this.searchTextBox = new PromptedTextBox(); } this.headerLabel = new Label(); this.footerLabel = new Label(); this.btnClose = new Button(); if (SelectedItem != null) { this.btnOk = new Button(); } var mainPanel = new TableLayoutPanel(); mainPanel.SuspendLayout(); this.SuspendLayout(); // // mainPanel // mainPanel.ColumnCount = 4; mainPanel.ColumnStyles.Add(new ColumnStyle()); mainPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100F)); mainPanel.ColumnStyles.Add(new ColumnStyle()); mainPanel.ColumnStyles.Add(new ColumnStyle()); if (this.bindingSource.SupportsFiltering) { mainPanel.Controls.Add(this.searchTextBox, 2, 0); } mainPanel.Controls.Add(this.dataGrid, 0, 1); mainPanel.Controls.Add(this.headerLabel, 0, 0); mainPanel.Controls.Add(this.footerLabel, 0, 2); mainPanel.Controls.Add(this.btnClose, 3, 2); if (SelectedItem != null) { mainPanel.Controls.Add(this.btnOk, 2, 2); } mainPanel.Dock = DockStyle.Fill; mainPanel.RowCount = 3; mainPanel.RowStyles.Add(new RowStyle()); mainPanel.RowStyles.Add(new RowStyle(SizeType.Percent, 100F)); mainPanel.RowStyles.Add(new RowStyle()); mainPanel.RowStyles.Add(new RowStyle(SizeType.Absolute, 20F)); mainPanel.Padding = new Padding(6); // // searchTextBox // if (this.bindingSource.SupportsFiltering) { this.searchTextBox.Anchor = AnchorStyles.Right | AnchorStyles.Bottom; mainPanel.SetColumnSpan(this.searchTextBox, 2); this.searchTextBox.FocusSelect = true; this.searchTextBox.PromptForeColor = SystemColors.ControlDark; this.searchTextBox.PromptText = "searching..."; this.searchTextBox.Size = new Size(150, 20); this.searchTextBox.TabIndex = 1; this.searchTextBox.TextChanged += delegate { string _txt = this.searchTextBox.Text.Trim(); if (string.IsNullOrEmpty(_txt)) { this.bindingSource.RemoveFilter(); } else { this.bindingSource.Filter = RowFilterBuilder.BuildColumnFilter(_txt, this.dataGrid).Trim(); } foreach (DataGridViewRow row in this.dataGrid.SelectedRows) { row.Selected = false; } if (this.dataGrid.Rows.Count > 0) { this.dataGrid.Rows[0].Selected = true; } }; } // // dataGrid // mainPanel.SetColumnSpan(this.dataGrid, 4); this.dataGrid.Dock = DockStyle.Fill; this.dataGrid.TabIndex = 0; this.dataGrid.PrepareStyleForShowingData(); this.dataGrid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells; this.dataGrid.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells; if (Columns == null || Columns.Length == 0) { this.dataGrid.AutoGenerateColumns = true; } else { this.dataGrid.AutoGenerateColumns = false; if (Columns[0] != null) { foreach (var _column in Columns) { DataGridViewColumn _c; if (_column.DataPropertyType == SqlDbType.Bit) { _c = this.dataGrid.AddCheckColumn(_column.DataPropertyName, _column.HeaderText); } else if (_column.DataPropertyType == SqlDbType.Date) { _c = this.dataGrid.AddTextColumn(_column.DataPropertyName, _column.HeaderText).SetDateStyle(); } else if (_column.DataPropertyType == SqlDbType.DateTime) { _c = this.dataGrid.AddTextColumn(_column.DataPropertyName, _column.HeaderText).SetDateTimeWithSecondsStyle(); } else if (_column.DataPropertyType == SqlDbType.Int || _column.DataPropertyType == SqlDbType.TinyInt) { _c = this.dataGrid.AddTextColumn(_column.DataPropertyName, _column.HeaderText).SetNumberStyle(); } else if (_column.DataPropertyType == SqlDbType.Decimal || _column.DataPropertyType == SqlDbType.Float) { _c = this.dataGrid.AddTextColumn(_column.DataPropertyName, _column.HeaderText).SetDecimalStyle(); } else if (_column.DataPropertyType == SqlDbType.Money) { _c = this.dataGrid.AddTextColumn(_column.DataPropertyName, _column.HeaderText).SetMoneyStyle(); } else { _c = this.dataGrid.AddTextColumn(_column.DataPropertyName, _column.HeaderText); } if (_column.ContentAlignment != DataGridViewContentAlignment.NotSet) { _c.SetStyles(50, _column.ContentAlignment); } if (_column.FillWeight.HasValue) { _c.SetAutoSizeFillStyle(100, _column.FillWeight.Value); } } } } if (SelectedItem == null) { this.dataGrid.DataBindingComplete += delegate { foreach (DataGridViewRow row in this.dataGrid.SelectedRows) { row.Selected = false; } } } ; else { this.dataGrid.DoubleClick += delegate { this.AcceptButton.PerformClick(); }; this.dataGrid.KeyDown += delegate(object sender, KeyEventArgs e) { if (e.KeyData == Keys.Enter || e.KeyData == Keys.Return) { e.Handled = true; this.AcceptButton.PerformClick(); } }; } this.dataGrid.DataSource = this.bindingSource; // // headerLabel // this.headerLabel.Anchor = AnchorStyles.Left; mainPanel.SetColumnSpan(this.headerLabel, 2); this.headerLabel.AutoSize = true; this.headerLabel.ForeColor = SystemColors.ControlDarkDark; this.headerLabel.TextAlign = ContentAlignment.MiddleLeft; this.headerLabel.Visible = false; // // footerLabel // this.footerLabel.Anchor = AnchorStyles.Left; mainPanel.SetColumnSpan(this.footerLabel, 2); this.footerLabel.AutoSize = true; this.footerLabel.ForeColor = SystemColors.ControlDarkDark; this.headerLabel.TextAlign = ContentAlignment.MiddleLeft; this.footerLabel.Visible = false; // // btnOk // if (SelectedItem != null) { this.btnOk.Anchor = AnchorStyles.Right; this.btnOk.DialogResult = DialogResult.OK; this.btnOk.Size = new Size(80, 25); this.btnOk.TabIndex = 3; this.btnOk.Text = "Select"; this.btnOk.Margin = new Padding(3, 9, 3, 6); } // // btnClose // this.btnClose.Anchor = AnchorStyles.Right; this.btnClose.DialogResult = DialogResult.Cancel; this.btnClose.Size = new Size(80, 25); this.btnClose.TabIndex = 4; this.btnClose.Text = SelectedItem != null ? "Cancel" : "Close"; this.btnClose.Margin = new Padding(3, 9, 3, 6); // // SelectItemForm // this.AutoScaleDimensions = new SizeF(6F, 13F); this.AutoScaleMode = AutoScaleMode.Font; this.AcceptButton = SelectedItem != null ? this.btnOk : this.btnClose; this.CancelButton = this.btnClose; this.ClientSize = new Size(624, 362); this.Controls.Add(mainPanel); this.MaximizeBox = false; this.MinimizeBox = false; this.MinimumSize = new Size(320, 200); this.ShowIcon = false; this.ShowInTaskbar = false; this.StartPosition = FormStartPosition.CenterScreen; this.Text = SelectedItem != null ? "Select a row" : "Data"; mainPanel.ResumeLayout(false); mainPanel.PerformLayout(); this.ResumeLayout(false); }