Select multiple Rows without pressing Control Key

后端 未结 6 1715
日久生厌
日久生厌 2021-01-19 07:36

I have a gridview where I can select multiple rows by pressing a control key. Is it possible to achieve the same without pressing a control key.

6条回答
  •  再見小時候
    2021-01-19 08:13

    I had to do this for a touch screen where I wanted multiselect on single click, without ugly check boxes. Why over complicate it, just keep a not of the Index'es

        private List SelectedIndexs { get; set; }
    

    Then add or remove them from the list as you click them... Then look through you datagrid and select the rows in the list.

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (SelectedIndexs.Contains(e.RowIndex))
            {
                SelectedIndexs.Remove(e.RowIndex);
            }
            else
            {
                SelectedIndexs.Add(e.RowIndex);
            }
    
            foreach (DataGridViewRow row in this.dataGridView1.Rows)
            {
                row.Selected = SelectedIndexs.Contains(row.Index);
            }
        }
    

提交回复
热议问题