mah*_*esh 2 c# datagridview visual-studio
我有简单的文本框示例如下:
private void Form1_Load(object sender, EventArgs e)
{
textBox1.Text = "Apple";
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (textBox1.Text.Length == 1)
{
if (textBox1.Text == "B" || textBox1.Text == "b")
{
textBox1.Text = "Ball";
}
}
}
Run Code Online (Sandbox Code Playgroud)
默认情况下,textbox1应该在Form加载时返回"Apple",但是当我按"b"或"B"时它应该在textbox1上返回"Ball".我对将其用于datagridview感到困惑.我怎样才能在datagridview中做到这一点?
假设我在datagridview上有一列,如下所示:
private void Form1_Load(object sender, EventArgs e)
{
DataGridViewColumn Particulars = new DataGridViewTextBoxColumn();
dataGridView1.Columns.Insert(0, Particulars );
}
Run Code Online (Sandbox Code Playgroud)
如果我在datagridview1上面的列比如何使用datagridview1,我用textbox做了吗?
您可能会发现使用内置于文本框控件的自动完成功能更直接,而不是尝试自己编写所有可能的方案.
TextBox您必须配置控件的两个重要属性才能启用其自动完成功能:AutoCompleteMode和AutoCompleteSource.
该AutoCompleteMode特性允许您选择如何文本框自动完成功能会看在行动.您可以在任何值之间进行选择AutoCompleteMode
None Disables the automatic completion feature for the ComboBox and TextBox controls. Suggest Displays the auxiliary drop-down list associated with the edit control. This drop-down is populated with one or more suggested completion strings. Append Appends the remainder of the most likely candidate string to the existing characters, highlighting the appended characters. SuggestAppend Applies both Suggest and Append options.
该AutoCompleteSource属性允许您指定希望文本框建议自动完成的字符串.在您的情况下,您可能需要指定一个CustomSource,这需要您将AutoCompleteCustomSource属性设置为用户定义的字符串集合 - 例如"Apple,Ball,..."等.
它DataGridViewTextBoxColumn只是托管一个标准TextBox控件,因此它提供的所有自动完成功能已经免费提供给您.您可以通过处理EditingControlShowing您的事件来设置此文本框的相应属性DataGridView,如下所示:
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
//Create and fill a list to use as the custom data source
var source = new AutoCompleteStringCollection();
source.AddRange(new string[] {"Apple", "Ball"});
//Set the appropriate properties on the textbox control
TextBox dgvEditBox = e.Control as TextBox;
if (dgvEditBox != null)
{
dgvEditBox.AutoCompleteMode = AutoCompleteMode.Suggest;
dgvEditBox.AutoCompleteCustomSource = source;
dgvEditBox.AutoCompleteSource = AutoCompleteSource.CustomSource;
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:如果您希望保持原始文本框示例中的相同行为,则可以只处理该TextChanged事件DataGridViewTextBoxColumn.正如我上面已经解释的那样,它DataGridViewTextBoxColumn只是托管一个标准TextBox控件,所以为它的TextChanged事件添加一个处理程序并使用你之前使用的相同代码是相当简单的:
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
TextBox dgvEditBox = e.Control as TextBox;
if (dgvEditBox != null)
{
//Add a handler for the TextChanged event of the underlying TextBox control
dgvEditBox.TextChanged += new EventHandler(dgvEditBox_TextChanged);
}
}
private void dgvEditBox_TextChanged(object sender, EventArgs e)
{
//Extract the textbox control
TextBox dgvEditBox = (TextBox)sender;
//Insert the appropriate string
if (dgvEditBox.Text.Length == 1)
{
if (dgvEditBox.Text == "B" || dgvEditBox.Text == "b")
{
dgvEditBox.Text = "Ball";
}
}
}
Run Code Online (Sandbox Code Playgroud)