Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted
Updated 19-Mar-18 21:36pm
Comments
Maciej Los 16-Mar-18 9:39am    
You mean DataGrid(WebUI) or DataGridView(WinControl)?

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.

VB
'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.
 
Share this answer
 
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.
 
Share this answer
 
Comments
Richard Deeming 20-Mar-18 10:31am    
If you want to reply to a solution, click the "Have a Question or Comment?" button under that solution.

DO NOT post your comment as a new "solution".

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900