在向上和向下按箭头键的同时将焦点设置在DataGridViewCombobox上

时间:2013-04-15 08:36:56

标签: c# asp.net winforms datagridviewcombobox

我有一个datagridview,其中有三列。实际上这是一个数据处理应用程序,这就是我想要使用键盘的原因(最少使用鼠标) 代码如下 [code languge =“csharp”]

 private void Form2_Load(object sender, EventArgs e)
        {
            DataTable odt = new DataTable();
            DataColumn odc = new DataColumn();
            DataColumn odcSec = new DataColumn();
            DataColumn odcThird = new DataColumn();
            DataColumn odcForth = new DataColumn();
            odc.ColumnName = "Class";
            odt.Columns.Add(odc);
            odcSec.ColumnName = "Subject Name";
            odt.Columns.Add(odcSec);
            odcThird.ColumnName = "Grade";
            odt.Columns.Add(odcThird);
            odcForth.ColumnName = "GradeCollection";
            odt.Columns.Add(odcForth);
            DataRow odr = odt.NewRow();
            odr["Class"] = "FYBA";
            odr["Subject Name"] = "Hindi";
            odr["Grade"] = "A";
            odr["GradeCollection"] = "A,B,C,D,E";
            odt.Rows.Add(odr);
            DataRow odrFirst = odt.NewRow();
            odrFirst["Class"] = "SYBA";
            odrFirst["Subject Name"] = "English";
            odrFirst["Grade"] = "B";
            odrFirst["GradeCollection"] = "A,B,C,D,E";
            odt.Rows.Add(odrFirst);
            DataRow odrSecond = odt.NewRow();
            odrSecond["Class"] = "SYBA";
            odrSecond["Subject Name"] = "English";
            odrSecond["Grade"] = "C";
            odrSecond["GradeCollection"] = "A,B,C,D,E";
            odt.Rows.Add(odrSecond);
            DataRow odrThird = odt.NewRow();
            odrThird["Class"] = "TYBA";
            odrThird["Subject Name"] = "Marathi";
            odrThird["Grade"] = "D";
            odrThird["GradeCollection"] = "A,B,C,D,E";
            odt.Rows.Add(odrThird);
            DataRow odrForth = odt.NewRow();
            odrForth["Class"] = "FYBA";
            odrForth["Subject Name"] = "Telagu";
            odrForth["Grade"] = "E";
            odrForth["GradeCollection"] = "A,B,C,D,E";
            odt.Rows.Add(odrForth);
            if (odt != null && odt.Rows.Count > 0)
            {
            DataGridViewTextBoxColumn txtClass = new DataGridViewTextBoxColumn();
            txtClass.HeaderText = "Class";
            txtClass.MaxInputLength = 20;
            txtClass.Width = 70;
            txtClass.Name = "Class";
            kryptonDataGridView1.Columns.Add(txtClass);

            DataGridViewTextBoxColumn txtSubjectName = new DataGridViewTextBoxColumn();
            txtSubjectName.HeaderText = "SubjectName";
            txtSubjectName.MaxInputLength = 20;
            txtSubjectName.Width = 70;
            txtSubjectName.Name = "SubjectName";           
            kryptonDataGridView1.Columns.Add(txtSubjectName);

            DataGridViewComboBoxColumn comboboxColumn = new DataGridViewComboBoxColumn();
            comboboxColumn.HeaderText = "Grade";
            comboboxColumn.DropDownWidth = 160;
            comboboxColumn.Width = 90;
            comboboxColumn.MaxDropDownItems = 3;
            comboboxColumn.FlatStyle = FlatStyle.Flat;              
            kryptonDataGridView1.TabStop = true;
            kryptonDataGridView1.Focus();
            kryptonDataGridView1.Columns.Insert(2, comboboxColumn);
            for (int i = 0; i < odt.Rows.Count; ++i)
            {
                string[] row1 = new string[] { odt.Rows[i]["Class"].ToString(), odt.Rows[i]["Subject Name"].ToString() };
                kryptonDataGridView1.Rows.Add(row1);
                string sItemNames = odt.Rows[i]["GradeCollection"].ToString();                  
                char[] charArray = new char[] { ',' };
                string[] sItemNameArray = sItemNames.Split(charArray, StringSplitOptions.RemoveEmptyEntries);                  
                DataTable dt = new DataTable();
                DataColumn dc1 = new DataColumn("Name");               
                dt.Columns.Add(dc1);              
                DataRow odrinner = dt.NewRow();
                odrinner["Name"] = "--Select--";
                dt.Rows.Add(odrinner);
                foreach (string item in sItemNameArray)
                {
                    dt.Rows.Add(item);
                }
                comboboxColumn.DataSource = dt;
                comboboxColumn.DisplayMember = "Name";                    
                if (!string.IsNullOrEmpty(odt.Rows[i]["Grade"].ToString()))
                {
                    kryptonDataGridView1.Rows[i].Cells[2].Value = odt.Rows[i]["Grade"].ToString();
                }
                else
                {
                    comboboxColumn.DefaultCellStyle.NullValue = "--Select--";
                }
            }
        }                       
    }

[/代码]

现在我想使用箭头键以及为组合框选择输入键但是当我按箭头键然后焦点转到下一行组合框列时,它没有被选中那个特定的组合框。当我使用输入时键然后焦点转到下一列。我想当我按下回车键或箭头键然后选择组合框(意味着焦点在组合框上),以便我可以从该组合框中选择没有空间命中的项目。

1 个答案:

答案 0 :(得分:0)

您可以使用此代码:

    private void kryptonDataGridView1_CellEnter(object sender, 
                                                DataGridViewCellEventArgs e)
    {
        if (e.ColumnIndex == 2)
        {
            kryptonDataGridView1.BeginEdit(true);
        }
    }