Шустрый

Профиль
Группа: Участник
Сообщений: 95
Регистрация: 23.5.2007
Репутация: нет Всего: нет
|
class FilterEdit: Код | using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Text; using System.Windows.Forms;
namespace FilterEditControl { public partial class FilterEdit : UserControl, IDataGridViewEditingControl { #region Constructors public FilterEdit() { InitializeComponent(); }
public void SetupControls(Filter filter) { this.FilterForEditSession = new Filter(); this.operation.SelectedItem = filter.Operation; this.Checked = filter.Checked; this.Text = filter.Text; } #endregion
#region Handlers void textBox1_TextChanged(object sender, EventArgs e) { this.OnValueChanged(); }
void operation_SelectedValueChanged(object sender, EventArgs e) { this.OnValueChanged(); }
void enable_CheckedChanged(object sender, EventArgs e) { this.OnValueChanged(); }
private void OnValueChanged() { this._valueChanged = true; this.FilterForEditSession.Checked = this.Checked; this.FilterForEditSession.Operation = this.Operation; this.FilterForEditSession.Text = this.Text; DataGridView dgv = this.EditingControlDataGridView; if (dgv != null) dgv.NotifyCurrentCellDirty(true); } #endregion
#region FilterEdit Members public bool Checked { get { return enable.Checked; } set { if (enable.Checked != value) { enable.Checked = value; } } }
public string Operation { get { return (string)operation.SelectedItem; } set { if ((string)operation.SelectedItem != value) { operation.SelectedItem = value; } } }
public new string Text { get { return textBox1.Text; } set { if (textBox1.Text != value) { textBox1.Text = value; } } }
public string FullText { get { string txt = Text; double result; switch (Operation) { case "in": if (!double.TryParse(txt, out result))//введенное значение не является числом-скорее всего-это строка { txt=txt.Replace(" ,", ",");//поубираем пробелы слева и справа от запятых, чтоб они не вошли в состав значений txt=txt.Replace(", ", ","); txt=txt.Replace(",", "','");//все значения разделенные запятыми возьмем в одиночные кавычки return Operation + " ('" + txt + "')"; } else return Operation + " (" + Text + ")"; break; case "IS NULL": return Operation; break; default: if (!double.TryParse(txt, out result))//введенное значение не является числом-скорее всего-это строка return Operation + " '" + Text + "'"; else return Operation + " " + Text; break; } } } #endregion
#region Fields private int _row; private DataGridView _dgv; private bool _valueChanged = false; private Filter FilterForEditSession; #endregion
#region IDataGridViewEditingControl Members public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle) { this.enable.Font = this.operation.Font = this.textBox1.Font = dataGridViewCellStyle.Font; this.MinimumSize = this.Size; }
public DataGridView EditingControlDataGridView { get { return this._dgv; } set { this._dgv = value; } }
public object EditingControlFormattedValue { get { return this.FilterForEditSession; } set { //nothing to do... } }
public int EditingControlRowIndex { get { return this._row; } set { this._row = value; } }
public bool EditingControlValueChanged { get { return this._valueChanged; } set { this._valueChanged = value; } }
public bool EditingControlWantsInputKey(Keys keyData, bool dataGridViewWantsInputKey) { switch (keyData & Keys.KeyCode) { case Keys.Down: case Keys.Right: case Keys.Left: case Keys.Up: case Keys.Home: case Keys.End: return true; default: return false; } }
public Cursor EditingPanelCursor { get { return Cursor.Current; } }
public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context) { return this.EditingControlFormattedValue; }
public void PrepareEditingControlForEdit(bool selectAll) { }
public bool RepositionEditingControlOnValueChange { get { return false; } } #endregion }
public class DataGridViewFilterCell : DataGridViewTextBoxCell { private int _heightOfGridBeforeEditMode; private int _heightOfRowBeforeEditMode; private int _widthOfColumnBeforeEditMode;
public DataGridViewFilterCell() : base() { }
public override Type EditType { get { return typeof(FilterEdit); } }
public override Type ValueType { get { return typeof(Filter); } }
public override Type FormattedValueType { get { return typeof(Filter); } }
public override object DefaultNewRowValue { get { return string.Empty; } }
public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle) { base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle); FilterEdit filterEditCtrl = this.DataGridView.EditingControl as FilterEdit; this._heightOfGridBeforeEditMode = this.DataGridView.Height; this._heightOfRowBeforeEditMode = this.OwningRow.Height; this._widthOfColumnBeforeEditMode = this.OwningColumn.Width; int CtrlWidth = filterEditCtrl.Width; int HeightPlus = 15;//надбавка на возможное появление горизонтальной полосы прокрутки if (this.DataGridView.Height < filterEditCtrl.Height + HeightPlus) this.DataGridView.Height = filterEditCtrl.Height + HeightPlus; if (this.OwningRow.Height < filterEditCtrl.Height) this.OwningRow.Height = filterEditCtrl.Height; if (this.OwningColumn.Width < CtrlWidth) this.OwningColumn.Width = CtrlWidth; else filterEditCtrl.Width = this.OwningColumn.Width;
Filter filterInCell = this.Value as Filter; if (filterInCell == null) filterInCell = new Filter(); filterEditCtrl.SetupControls(filterInCell); }
public override void DetachEditingControl() { if (this._heightOfGridBeforeEditMode > 0) this.DataGridView.Height = this._heightOfGridBeforeEditMode; if (this._heightOfRowBeforeEditMode > 0) this.OwningRow.Height = this._heightOfRowBeforeEditMode; if (this._widthOfColumnBeforeEditMode > 0) this.OwningColumn.Width = this._widthOfColumnBeforeEditMode; base.DetachEditingControl(); }
protected override object GetFormattedValue(object value, int rowIndex, ref DataGridViewCellStyle cellStyle, TypeConverter valueTypeConverter, TypeConverter formattedValueTypeConverter, DataGridViewDataErrorContexts context) { if (value == null) return string.Empty; else return TypeDescriptor.GetConverter(value).ConvertToString(value); } }
public class DataGridViewFilterColumn : DataGridViewColumn { public DataGridViewFilterColumn() : base(new DataGridViewFilterCell()) { }
public override DataGridViewCell CellTemplate { get { return base.CellTemplate; } set { if (value != null && !value.GetType().IsAssignableFrom(typeof(DataGridViewFilterCell))) throw new InvalidCastException("Cell must be a FilterCell"); base.CellTemplate = value; } } }
[TypeConverter(typeof(FilterConverter))] [Serializable] public class Filter { public Filter() { } public Filter(bool check, string oper, string txt) { this.Checked = check; this.Operation = oper; this.Text = txt; }
private bool _Checked; public bool Checked { get { return _Checked; } set { _Checked = value; } }
private string operation; public string Operation { get { return operation; } set { operation = value; } }
private string text; public string Text { get { return text; } set { text = value; } }
public string FullText { get { string txt = text; double result; switch (Operation) { case "in": if (!double.TryParse(txt, out result))//введенное значение не является числом-скорее всего-это строка { txt=txt.Replace(" ,", ",");//поубираем пробелы слева и справа от запятых, чтоб они не вошли в состав значений txt=txt.Replace(", ", ","); txt=txt.Replace(",", "','");//все значения разделенные запятыми возьмем в одиночные кавычки return Operation + " ('" + txt + "')"; } else return Operation + " (" + text + ")"; break; case "IS NULL": return Operation; break; default: if (!double.TryParse(txt, out result))//введенное значение не является числом-скорее всего-это строка return Operation + " '" + text + "'"; else return Operation + " " + text; break; } } } }
public class FilterConverter : TypeConverter { // Overrides the ConvertTo method of TypeConverter. public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType) { if (destinationType == typeof(string)) { Filter filter = value as Filter; return filter.FullText; } return base.ConvertTo(context, culture, value, destinationType); } } }
|
FilterEdit.Designer.cs: Код | namespace FilterEditControl { partial class FilterEdit { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null;
/// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); }
#region Component Designer generated code
/// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.enable = new System.Windows.Forms.CheckBox(); this.operation = new System.Windows.Forms.ComboBox(); this.textBox1 = new System.Windows.Forms.TextBox(); this.SuspendLayout(); // // enable // this.enable.AccessibleRole = System.Windows.Forms.AccessibleRole.None; this.enable.AutoSize = true; this.enable.Location = new System.Drawing.Point(2, 4); this.enable.Name = "enable"; this.enable.Size = new System.Drawing.Size(59, 17); this.enable.TabIndex = 0; this.enable.Text = "Enable"; this.enable.UseVisualStyleBackColor = true; this.enable.CheckedChanged += new System.EventHandler(this.enable_CheckedChanged); // // operation // this.operation.FormattingEnabled = true; this.operation.Items.AddRange(new object[] { "Like", "=", "<>", ">", ">=", "<", "<=", "in", "IS NULL"}); this.operation.Location = new System.Drawing.Point(57, 2); this.operation.Name = "operation"; this.operation.Size = new System.Drawing.Size(47, 21); this.operation.TabIndex = 1; this.operation.SelectedValueChanged += new System.EventHandler(this.operation_SelectedValueChanged); // // textBox1 // this.textBox1.Dock = System.Windows.Forms.DockStyle.Bottom; this.textBox1.Location = new System.Drawing.Point(0, 26); this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(106, 20); this.textBox1.TabIndex = 2; this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged); // // FilterEdit // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.Controls.Add(this.textBox1); this.Controls.Add(this.operation); this.Controls.Add(this.enable); this.Name = "FilterEdit"; this.Size = new System.Drawing.Size(106, 46); this.ResumeLayout(false); this.PerformLayout();
}
#endregion
private System.Windows.Forms.CheckBox enable; private System.Windows.Forms.ComboBox operation; private System.Windows.Forms.TextBox textBox1; } }
|
(В TextBox не вводятся символы q % ' " .)
|