日期:2014-05-18  浏览次数:20940 次

DataGridViewComboBoxColumn 可以让用户输入并自动匹配的选项的问题
使用以下方法解决了让用户输入并自动匹配选项的问题.但是发现如果全部使用键盘输入的话.当焦点移出此单元格后此单位元格未选中任何一项.显示为空.原因见代码中...
C# code

        private void dgvInfoPerson_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (dgvInfoPerson.CurrentCellAddress.X == dgvInfoPerson.Columns["colCboDeptNo"].Index)
            {
                cbo = e.Control as ComboBox;
                if (cbo != null)
                {
                    cbo.DropDownStyle = ComboBoxStyle.DropDown;
                    cbo.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                }
            }
        }

        private void dgvInfoPerson_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            if (dgvInfoPerson.CurrentCellAddress.X == dgvInfoPerson.Columns["colCboDeptNo"].Index)
            {
                //问题出在这里.如果你输入的是ValueMember的值的话就没问题.但你如果输入的是DisplayMember的值的话就不会选中.
                //如ValueMember="MAN" DisplayMember="管理部",如果你在此单元格输入MAN就没问题,但输入管理部就不会选中.但用户就应该是输入管理部
                  //而不是输入MAN.
                dgvInfoPerson.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = e.FormattedValue;
            }
        }


已经搜过很多地方了.都没好的解决方案.还请各位有经验的前辈指点迷津.谢谢!

------解决方案--------------------
C# code

            //判断键盘按下的键。如果等于“enter”,则send一个“{tab}”。模拟按下“tab”键,实现单元格。向下移动
            if (keyData == Keys.Enter )
            {
                e.Handled = false;//加上这个试试。不行的话像下一句发送一个回车的按键消息
                System.Windows.Forms.SendKeys.Send("{tab}");
                return true;
            }

------解决方案--------------------
public void ProcessHotkey(Message m) //按下设定的键时调用该函数
{
try
{
IntPtr id = m.WParam; //IntPtr用于表示指针或句柄的平台特定类型
//MessageBox.Show(id.ToString());
string sid = id.ToString();
switch (sid)
{
case "100":
additem.ShowDialog();
if (additem.DialogResult == DialogResult.OK)
{
dgItemSellList[4, dgItemSellList.CurrentCell.RowIndex].Value = additem.txtnum.Text.ToString();
}
break;
case "200":
if(MessageBox.Show("确定要关闭此窗体吗?","关闭提示",MessageBoxButtons.YesNo)==DialogResult.Yes)
this.Close();
break;
case "300":
fullpay = txttotal.Text;
billcode = txtbillcode.Text;
date = txtdate.Text;
handle = comhandle.Text;
cust = comcust.Text;
memo = txtMemo.Text;
SellDetail.SellPayment sell = new SellMain.SellDetail.SellPayment();
if (newitemtable.Rows.Count != 0)
{
sell.ShowDialog();
}
if (sell.DialogResult == DialogResult.OK)
{
txtsfull.Text = sell.txtfullpay.Text;
txtspay.Text = sell.txtpay.Text;
txtschange.Text = sell.txtchange.Text;
txttotal.Text = "";
txtallnum.Text = "";
newitemtable.Clea