简体   繁体   English

c# - 如何在不按CTRL键的情况下多选DataGridView行?

[英]How to multiselect DataGridView rows without pressing CTRL key in c#?

I am new to c# and using windows forms.我是 C# 的新手并使用 Windows 窗体。

I have a DataGridView control on a form and I need to allow a user to multi-select rows without pressing the CTRL key and without using check box column.我在窗体上有一个 DataGridView 控件,我需要允许用户在不按 CTRL 键和不使用复选框列的情况下多选行。 I have already enabled the mutli-select property.我已经启用了 mutli-select 属性。

I know this is duplicated question on Here but I tried the first answer (Bolu answer) and it worked but the datagridvied refreshes and flicks every time I select a row.我知道这是Here上的重复问题,但我尝试了第一个答案(Bolu 答案)并且它起作用了,但是每次我选择一行时,datagridvied 都会刷新并轻弹。

I wanted to try the "Edit: Better solution" (in the same answer) and it is too complicated for me I did not understand the steps.我想尝试“编辑:更好的解决方案”(在同一个答案中),但对我来说太复杂了,我不明白这些步骤。

My question: How can I get rid of the flicking/ refreshing process and make it smooth when I select a row?我的问题:如何在选择一行时摆脱轻弹/刷新过程并使其平滑? (the code shown below), also I am happy to receive any new solutions. (下面显示的代码),我也很高兴收到任何新的解决方案。 Please help me, thank you请帮帮我,谢谢

DataGridViewRow[] old;
private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
    old = new DataGridViewRow[dataGridView1.SelectedRows.Count];
    dataGridView1.SelectedRows.CopyTo(old, 0);
}

private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
    foreach (DataGridViewRow gr in old)
    {
        if (gr == dataGridView1.CurrentRow)
        {
            gr.Selected = false;
        }
        else
        {
            gr.Selected = true;
        }
    }
}

Try this;试试这个; keep the datagridview ( dgvTopics in my case) mutiselect true :保留datagridviewdgvTopics在我的情况)mutiselect true

private void dgvTopics_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
    if (dgvTopics.Rows[e.RowIndex].Selected == true)
        dgvTopics.Rows[e.RowIndex].ErrorText = "U";
    else
        dgvTopics.Rows[e.RowIndex].ErrorText = "S";  
}

private void dgvTopics_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
{
    foreach (DataGridViewRow dgvr in dgvTopics.Rows)
    {
        if(dgvr.ErrorText == "S")
            dgvr.Selected = true;
        else
            dgvr.Selected = false;
    }
}

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

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