2010-11-23 58 views
2

我有如下簡單的文本框例子:文本框例在DataGridView的

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

默認情況下TextBox1中應該返回「蘋果」的形式負載,但是當我按「B」或「B」,那麼它應該返回「球「在textbox1上。我對將它用於datagridview存在困惑。我怎麼能在datagridview中做到這一點?

假設我有datagridview的一列象下面這樣:

private void Form1_Load(object sender, EventArgs e) 
    { 
     DataGridViewColumn Particulars = new DataGridViewTextBoxColumn(); 
     dataGridView1.Columns.Insert(0, Particulars); 
    } 

如果我上面列在datagridview1比我如何與datagridview1我所用文本框做對?

回答

5

您可能會發現使用內置於文本框控件的自動完成功能更直接,而不是嘗試自行編寫所有可能的場景。

有該TextBox控件,您必須配置,以使其自動完成功能的兩個重要的屬性:AutoCompleteModeAutoCompleteSource

AutoCompleteMode屬性允許您選擇文本框自動完成功能將如何看在行動。您可以將任何的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. 

選擇了AutoCompleteSource屬性允許你指定你想要的文本提出自動完成與字符串。在你的情況,你可能會想指定一個CustomSource,這就需要你的AutoCompleteCustomSource屬性設置爲字符串,就像一個用戶定義集「蘋果,球,...」等

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

編輯:如果您希望保留相同的行爲您在原始文本框的例子,你可以只爲處理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"; 
     } 
    } 
} 
+0

@Cody,你的代碼是工程和我很讚賞你解釋它的方式。只需編輯您的代碼。你忘了添加「;」到「dgvEditBox.AutoCompleteCustomSource = source」 – mahesh 2010-11-23 14:13:21