簡體   English   中英

c# - 如何在不按CTRL鍵的情況下多選DataGridView行?

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

我是 C# 的新手並使用 Windows 窗體。

我在窗體上有一個 DataGridView 控件,我需要允許用戶在不按 CTRL 鍵和不使用復選框列的情況下多選行。 我已經啟用了 mutli-select 屬性。

我知道這是Here上的重復問題,但我嘗試了第一個答案(Bolu 答案)並且它起作用了,但是每次我選擇一行時,datagridvied 都會刷新並輕彈。

我想嘗試“編輯:更好的解決方案”(在同一個答案中),但對我來說太復雜了,我不明白這些步驟。

我的問題:如何在選擇一行時擺脫輕彈/刷新過程並使其平滑? (下面顯示的代碼),我也很高興收到任何新的解決方案。 請幫幫我,謝謝

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;
        }
    }
}

試試這個; 保留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