0

我有一个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--";
                }
            }
        }                       
    }

[/代码]

现在我想使用箭头键和输入键来选择组合框但是当我按下箭头键然后焦点转到下一行组合框列时,它没有选择那个特定的组合框。同样当我使用输入键然后焦点转到下一列。我希望当我按下回车键或箭头键时,它会选择组合框(意味着焦点应该在组合框上),这样我就可以从该组合框中选择项目而无需空格。

4

1 回答 1

0

您可以使用以下代码:

    private void kryptonDataGridView1_CellEnter(object sender, 
                                                DataGridViewCellEventArgs e)
    {
        if (e.ColumnIndex == 2)
        {
            kryptonDataGridView1.BeginEdit(true);
        }
    }
于 2013-04-15T23:27:45.923 回答