简体   繁体   English

如何在特定列的DataGridView中添加TextBox?

[英]How to add TextBox in DataGridView at specific column?

void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
     TextBox txtbox = e.Control as TextBox;
     if (this.dataGridView1.CurrentCell.ColumnIndex == 0)
     {
         if (txtbox != null)
         {
             //
         }
     }
}

Also I have coded with AutoCompleteStringCollection. 我也用AutoCompleteStringCollection编码。

Code is working, 代码有效,

  1. Before edit Column 1, It won't allow autoCompletion for any of other column. 在编辑列1之前,不允许对其他任何列进行自动填充。

  2. Once edited Column 1, all column will work same as Column one. 一旦编辑了列1,所有列将与列1相同。

Please help me how to fix issue or else any other best way to do that, please share here. 请帮助我解决问题或其他最佳方法,请在此处分享。

This should work. 这应该工作。

private bool firstColEdited = false;
/************************************************************/
var source = new AutoCompleteStringCollection();
String[] stringArray = Array.ConvertAll<DataRow, String>(products.Select(), delegate(DataRow row) { return (String)row["code"]; });
source.AddRange(stringArray);
/************************************************************/
void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
    TextBox txtbox = e.Control as TextBox;
    if (this.dataGridView1.CurrentCell.ColumnIndex == 0 || firstColEdited)
    {
        firstColEdited = true;
        txtbox.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
        txtbox.AutoCompleteCustomSource = source;
        txtbox.AutoCompleteSource = AutoCompleteSource.CustomSource;
    }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM