在VB.NET中的数据网格视图中多选单元格 [英] Multi select cells in a datagridview in VB.NET

查看:148
本文介绍了在VB.NET中的数据网格视图中多选单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个datagridview,其中我将MultiSelect设置为True,SelectionMode是Cell Select。现在我想让用户选择多个单元格,他们可以将信息从文本框保存到选定的单元格。



我尝试了什么:



我之前从未这样做过,所以我没有什么可以在这里展示的。问题是我首先要在开始保存数据之前选择多个单元格。

I have a datagridview in which I set MultiSelect as True and SelectionMode is Cell Select. Now I want to be enable the user to select multiple cells where they can save information from a textbox to the selected cells.

What I have tried:

I have never done this before so I have nothing to show here. The problem is I first have to get the multiple cells selected before I can start to save the data.

推荐答案

根据控件类型:

WPF: c# - DataGrid获取选定行的列值 - Stack Overflow [ ^ ]

WinForm:如何:获取Windows窗体DataGridView控件中的选定单元格,行和列Microsoft Docs [ ^ ]
Depending on type of control:
WPF: c# - DataGrid get selected rows' column values - Stack Overflow[^]
WinForm: How to: Get the Selected Cells, Rows, and Columns in the Windows Forms DataGridView Control | Microsoft Docs[^]


我的问题是我想在数据网格视图上多选单元格。我做对了,想在这里发布一个解决方案,这样可以帮助别人。然而,我的问题从想要在一行中选择4个单元格到必须选择不同行中的单元格以及每次单击一个单元格以选择4个连续单元格然后拖动选择一组单元格而扩展。

嗯,这个解决方案适用于所有三种情况。



My problem was I wanted to multi select cells on a data grid view. I got that right and would like to post a solution here so it can maybe help someone else. My problem, however expanded from wanting to select 4 cells in one row to having to select cells in different rows and each time a person clicked a cell to select 4 consecutive cells and then to drag select a group of cells.
Well, this solution works for all three scenarios.

'Get the number of rows with selected cells
Dim rows As New List(Of Integer)
Try
    For i As Integer = 1 To MyDataGridView.SelectedCells.Count
        If Not rows.Contains(MyDataGridView.SelectedCells(i - 1).RowIndex) Then
            rows.Add(MyDataGridView.SelectedCells(i - 1).RowIndex)
        End If
    Next
Catch ex As Exception
    MessageBox.Show(ex.Message)
End Try
'---------------------------------------------------------------------------------------------------------------------------------------------------
Try
    'Loop through the rows and columns to see which cells are selected first the rows and then the columns
    For k As Integer = 1 To rows.Count
        If MyDataGridView.Rows(rows(k - 1)).Cells(0 + 0).Selected = True _
        Or MyDataGridView.Rows(rows(k - 1)).Cells(0 + 1).Selected = True _
        Or MyDataGridView.Rows(rows(k - 1)).Cells(0 + 2).Selected = True _
        Or MyDataGridView.Rows(rows(k - 1)).Cells(0 + 3).Selected = True _
        Then
            MyDataGridView.Rows(rows(k - 1)).Cells(0 + 0).Selected = False
            MyDataGridView.Rows(rows(k - 1)).Cells(0 + 1).Selected = False
            MyDataGridView.Rows(rows(k - 1)).Cells(0 + 2).Selected = False
            MyDataGridView.Rows(rows(k - 1)).Cells(0 + 3).Selected = False
        End If
        For j As Integer = 4 To MyDataGridView.ColumnCount - 1 Step 6
            Try
                If MyDataGridView.Rows(rows(k - 1)).Cells(j + 0).Selected = True _
                Or MyDataGridView.Rows(rows(k - 1)).Cells(j + 1).Selected = True _
                Or MyDataGridView.Rows(rows(k - 1)).Cells(j + 2).Selected = True _
                Or MyDataGridView.Rows(rows(k - 1)).Cells(j + 3).Selected = True _
                Or MyDataGridView.Rows(rows(k - 1)).Cells(j + 4).Selected = True _
                Or MyDataGridView.Rows(rows(k - 1)).Cells(j + 5).Selected = True _
                Then
                    MyDataGridView.Rows(rows(k - 1)).Cells(j + 0).Selected = True
                    MyDataGridView.Rows(rows(k - 1)).Cells(j + 1).Selected = True
                    MyDataGridView.Rows(rows(k - 1)).Cells(j + 2).Selected = True
                    MyDataGridView.Rows(rows(k - 1)).Cells(j + 3).Selected = True
                    MyDataGridView.Rows(rows(k - 1)).Cells(j + 4).Selected = True
                    MyDataGridView.Rows(rows(k - 1)).Cells(j + 5).Selected = True
                End If
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        Next
    Next
Catch ex As Exception
    MessageBox.Show(ex.Message)
End Try





资料来源:我的老板。



Source: My boss.


我不想获得选定的单元格而是设置选定的单元格,但是谢谢你我来了。目前只是非常忙碌但可能会在某个时候发布我的解决方案。
I did not want to get selected cells but to set selected cells but thank you I came right. Just very busy at the moment but might post my solution here sometime.


这篇关于在VB.NET中的数据网格视图中多选单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆