簡體   English   中英

該值未顯示在DataGridView單元格中

[英]The Value is not shown in DataGridView Cell

我在Microsoft Visual Studio 2010中有一個DataGridView,我希望在其中插入兩個字符“ I”和“ O”。 但觸發KeyPress所需的文本未顯示在單元格中

可能是什么問題呢?

private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {              
        switch (treeView1.SelectedNode.Name)
        {
            case "PanelProperties":

                e.Control.KeyPress -= new KeyPressEventHandler(Control_KeyPress);
                e.Control.KeyPress += new KeyPressEventHandler(Control_KeyPress);
                break;
            default:
                break;
        }     
    }

    private void Control_KeyPress(object sender, KeyPressEventArgs e)
    {
        int columnIndex = dataGridView1.CurrentCell.ColumnIndex;
        switch (columnIndex)
        {
            case 5:
                if (!(e.KeyChar.Equals('i') || e.KeyChar.Equals('I')) && !(e.KeyChar.Equals('o') || e.KeyChar.Equals('O')))
                    e.Handled = true;
                else
                {
                    if (e.KeyChar.Equals('i') || e.KeyChar.Equals('I'))
                        dataGridView1.CurrentCell.Value = "In";
                    else
                        dataGridView1.CurrentCell.Value = "Out";
                }
                break;
            case 6:
            case 7:
                if (!(e.KeyChar.Equals('n') || e.KeyChar.Equals('N')) && !(e.KeyChar.Equals('y') || e.KeyChar.Equals('Y')))
                    e.Handled = true;
                else
                {
                    if (e.KeyChar.Equals('n') || e.KeyChar.Equals('N'))
                        dataGridView1.CurrentCell.Value = "No";
                    else
                        dataGridView1.CurrentCell.Value = "Yes";
                }
                break;
        }
    }    

只需將編輯的更改提交到datagrid視圖即可。

        if (dataGridView1.IsCurrentCellDirty)
        {
            dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
        }

只需在我的示例應用程序中嘗試使用它即可:

dataGridView1.CurrentCell.Value = "In";    
dataGridView1.EndEdit(); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM