开发者

TextBox Example Into Datagridview

开发者 https://www.devze.com 2023-01-27 01:50 出处:网络
I have simple textbox example as below: private void Form1_Load(object sender, EventArgs e) { textBox1.Text = \"Apple\";

I have simple textbox example as below:

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";
            }
        }
    }

By default textbox1 should return "Apple" on Form load but when I press "b" or "B" then it should return "Ball" on textbox1. I have a confusion on utilize it into datagridview. how can i do it in datagridview?.

Suppose I have One column on datagridview like below:

private void Form1_Load(object sender, EventArgs e)
    {
        DataGridViewColumn Particulars = new DataGridViewTextBoxColumn();
        dataG开发者_开发问答ridView1.Columns.Insert(0, Particulars );
    }

If I have above column In datagridview1 than How to do I with datagridview1 which I have did with textbox?.


You might find it more straightforward to use the auto-complete functionality built-in to the textbox control, rather than trying to code for all possible scenarios yourself.

There are two important properties of the TextBox control that you must configure to enable its auto-completion functionality: AutoCompleteMode and AutoCompleteSource.

The AutoCompleteMode property allows you to choose how the textbox autocomplete function will look in action. You can choose between any of the AutoCompleteMode values

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.

The AutoCompleteSource property allows you to specify the strings that you want the textbox to propose auto-completion with. In your case, you will probably want to specify a CustomSource, which requires you to set the AutoCompleteCustomSource property to a user-defined collection of strings—something like "Apple, Ball, ..." etc.

The DataGridViewTextBoxColumn simply hosts a standard TextBox control, so all of the auto-complete functionality it provides is already available to you for free. You can set the appropriate properties of this textbox by handling the EditingControlShowing event of your DataGridView, like so:

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;
    }
}

EDIT: If you'd prefer to keep the same behavior as you have in the original textbox example, you can just handle the TextChanged event for the DataGridViewTextBoxColumn. As I already explained above, the DataGridViewTextBoxColumn simply hosts a standard TextBox control, so it's fairly straightforward to add a handler for its TextChanged event and use the same code you had before:

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";
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号