C# DataGridView中實現類似ComboBox可編輯的下拉框功能

http://www.cnblogs.com/zwaleaf/archive/2009/05/22/1487214.html

 

Google了一下, 原來這個問題很多人碰到過,也有一些解決方案.不過感覺大多不好,有些許的問題.也許有好的,我沒找到吧.於是在前人基礎上,小小的改動了下.
目前還有兩個小問題待解決:
1) DataGridView最後一行,第一列的ComboBox輸入值,DataGridView應該自動增加一行(編輯其他列會自動增加一行).
2) 第一列的ComboBox中,假設其Items中有Shenzhen, 我輸入Shen,Cell中的Value將會時Shen. 當再次點擊ComboBox,顯示下拉Item時,Item中的Shenzhen會高亮顯示,ComboBox的Text會自動變成Shenzhen. 其實這也是ComboxBox的一個功能.可能有時不需要這個功能.

 

using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
//此Form中有兩個控件,一個DataGridView(dataGridView1),一個ComboBox(comboBox1)
//DataGridView中有兩列(都是DataGridViewTextBoxColumn的),其中第一列實現類似ComboBox的功能
//第一列中只有正在編輯的Cell纔會顯示ComboBox,其他的不會顯示,這樣好看一點
namespace Test
{
    
public partial class Form1 : Form
    
{
        
private int comboBoxColumnIndex = 0// DataGridView的首列

        
public Form1()
        
{
            InitializeComponent();
            InitComboBoxValues();
            
this.dataGridView1.Controls.Add(this.comboBox1); 
            
this.dataGridView1.CellEnter += new DataGridViewCellEventHandler(dataGridView1_CellEnter);
            
this.dataGridView1.CellLeave+=new DataGridViewCellEventHandler(dataGridView1_CellLeave);
        }


        
private void InitComboBoxValues()
        
{
            
this.comboBox1.Items.AddRange(new String[] "Beijing""Shanghai""Guangzhou""Wuhan""Shenzhen" });
            
this.comboBox1.AutoCompleteMode = AutoCompleteMode.Suggest; //輸入提示
            this.comboBox1.AutoCompleteSource = AutoCompleteSource.ListItems;
        }


        
private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
        
{
            
if (e.ColumnIndex == comboBoxColumnIndex)
            
{
                
//此處cell即CurrentCell
                DataGridViewCell cell = this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
                Rectangle rect 
= this.dataGridView1.GetCellDisplayRectangle(cell.ColumnIndex, cell.RowIndex, true);
                
this.comboBox1.Location = rect.Location;
                
this.comboBox1.Size = rect.Size;
                comfirmComboBoxValue(
this.comboBox1, (String)cell.Value);
                
this.comboBox1.Visible = true;             
            }

        }


        
private void dataGridView1_CellLeave(object sender, DataGridViewCellEventArgs e)
        
{
            
if (e.ColumnIndex == comboBoxColumnIndex)
            
{
                
//此處cell不爲CurrentCell
                DataGridViewCell cell = this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex]; 
                cell.Value 
= this.comboBox1.Text;
                
this.comboBox1.Visible = false;
            }

        }


        
private void comfirmComboBoxValue(ComboBox com, String cellValue)
        
{
            com.SelectedIndex 
= -1;
            
if (cellValue == null)
            
{
                com.Text 
= "";
                
return;
            }

            com.Text 
= cellValue;
            
foreach (Object item in com.Items)
            
{
                
if ((String)item == cellValue)
                
{
                    com.SelectedItem 
= item;
                }

            }

        }

    }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章