我使用IDataGridViewEditingControl在DataGridView中实现了一个过滤行。这是我从MSDN http://msdn.microsoft.com/en-us/library/7tas5c80(v=vs.110).aspx
获得的示例代码public class UserControlCell : DataGridViewTextBoxCell
{
public UserControlCell()
: base()
{
}
public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
{
// Set the value of the editing control to the current cell value.
base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
UserControlEditingControl ctl = DataGridView.EditingControl as UserControlEditingControl;
}
public override Type EditType
{
get { return typeof(UserControlEditingControl); }
}
public override Type ValueType
{
get { return typeof(String); }
}
public override object DefaultNewRowValue
{
get { return ""; }
}
}
class UserControlEditingControl : UserControl1, IDataGridViewEditingControl
{
DataGridView dataGridView;
private bool valueChanged = false;
int rowIndex;
// Implements the IDataGridViewEditingControl.EditingControlFormattedValue property.
public object EditingControlFormattedValue
{
get { return this.textBox1.Text; }
set { this.textBox1.Text = value.ToString(); }
}
// Implements the IDataGridViewEditingControl.GetEditingControlFormattedValue method.
public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
{
return EditingControlFormattedValue;
}
// Implements the IDataGridViewEditingControl.ApplyCellStyleToEditingControl method.
public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
{
this.Font = dataGridViewCellStyle.Font;
}
// Implements the IDataGridViewEditingControl.EditingControlRowIndex property.
public int EditingControlRowIndex
{
get { return rowIndex; }
set { rowIndex = value; }
}
// Implements the IDataGridViewEditingControl.EditingControlWantsInputKey method.
public bool EditingControlWantsInputKey(Keys key, bool dataGridViewWantsInputKey)
{
// Let the DateTimePicker handle the keys listed.
switch (key & Keys.KeyCode)
{
case Keys.Left:
case Keys.Up:
case Keys.Down:
case Keys.Right:
case Keys.Home:
case Keys.End:
case Keys.PageDown:
case Keys.PageUp:
return true;
default:
return !dataGridViewWantsInputKey;
}
}
// Implements the IDataGridViewEditingControl.PrepareEditingControlForEdit method.
public void PrepareEditingControlForEdit(bool selectAll)
{
// No preparation needs to be done.
}
// Implements the IDataGridViewEditingControl.RepositionEditingControlOnValueChange property.
public bool RepositionEditingControlOnValueChange
{
get { return false; }
}
// Implements the IDataGridViewEditingControl.EditingControlDataGridView property.
public DataGridView EditingControlDataGridView
{
get { return dataGridView; }
set { dataGridView = value; }
}
// Implements the IDataGridViewEditingControl.EditingControlValueChanged property.
public bool EditingControlValueChanged
{
get { return valueChanged; }
set { valueChanged = value; }
}
// Implements the IDataGridViewEditingControl.EditingPanelCursor property.
public Cursor EditingPanelCursor
{
get { return base.Cursor; }
}
}
在其中,UserControl1是一个UserControl,它将在fisrt行的每个单元格中托管。
我的问题是当我在每个过滤单元格中输入任何条件时,所有剩余单元格中出现的条件。那么我该如何解决这个问题?
这是我将过滤器控件分配给网格单元的代码:
DataGridViewRow rows = new DataGridViewRow();
UserControlCell[] textBoxFilterCell = new UserControlCell[3];
for (int i = 0; i < 3; i++)
{
textBoxFilterCell[i] = new UserControlCell();
rows.Cells.Add(textBoxFilterCell[i]);
}
dataGridView1.Rows.Add(rows);