简体   繁体   English

如何在keydown DataGridView C#中给出条件

[英]How to give condition in keydown DataGridView C#

I want to make a condition like this我想做这样的条件

private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
{
}

If the user selects the first column it will execute insert and if they select the third column it will execute edit如果用户选择第一列,它将执行insert ,如果他们选择第三列,它将执行edit

How do I go about doing this?我该怎么做?

Try this尝试这个

if( dataGridView1.SelectedColumns[2] != null)
{
    //Insertion
}
if( dataGridView1.SelectedColumns[0] != null)
{
   //Editing
}

You need to check the selected columns, assuming they can only select one you can do this check您需要检查选定的列,假设他们只能选择一个您可以进行此检查

if(dataGridView1.SelectedColumns[0] == dataGridView1.Columns[0])
{
    //Insert code;
}

else if(dataGridView1.SelectedColumns[0] == dataGridView1.Columns[2])
{
    //Edit code;
}

If they can select multiple columns you should first do this check as well如果他们可以选择多个列,您也应该首先进行此检查

if(dataGridView1.Columns.GetColumnCount(DataGridViewElementStates.Selected) == 1)
{
    if(dataGridView1.SelectedColumns[0] == dataGridView1.Columns[0])
    {
        //Insert code;
    }

    else if(dataGridView1.SelectedColumns[0] == dataGridView1.Columns[2])
    {
        //Edit code;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM