get index from a datagridview condition - vb.net

I am trying to get the index of the position of my columns and rows of a datagridview, applying a condition.
The condition is this if the column (2) its row has content that captures the indices of all the cells on its right
with this code I get the index when selecting each cell
Dim index As New List(Of String)
For Each cell As DataGridViewCell In Me.DGVDETASIGNACION.SelectedCells
index.Add(CStr(cell.ColumnIndex & "," & cell.RowIndex))
Next
MessageBox.Show(String.Join("; ", index))
Which is correct but how would I apply the condition if the column (2) of my datagridview has data tell me to capture all the indices of the cells on the right which I show in the image

Related

Reference another cell in a different row having calculated the minimum value in a datatable column

Reference another cell in the same row having calculated the minimum value in a datatable column
The above answered my question as to how to reference another cell in the same row having calculated the minimum value in a datatable column. But how do I reference a cell in another row still using that original cell?
In other words, if the min value in a column is in row 5, I now know how to get the values of cells in all other columns within row 5, but how do I get the values from columns within other rows (eg row 6) using the original cell as a starting point?
Dim Data() As DataRow = datatable.Select("sourcecolumn = " & test.ToString())
Dim column2 = Data(0)(1)
was the answer to my first question, how would I amend this to get to another row? I tried:
dim column2 = Data(1)(1)
but it returns an array error. I think the numbers in brackets are absolute, but I need to use relative ones.
Thanks in advance again.

Adding Rows in DataGridView

I have a DGV that would change in columns, how would i modify the code to dynamically add all the cells what are present in the row, instead of hardcoding each cell - i could see a problem if i added 31 cells and then a month with 28 would throw an error. I'd miss the first column becuase thats a name, but all the rest are numbers.
For Each SelectedRow as DataGridViewRow in dgv_service_centers.SelectedRows()
Total = SelectedRow.Cells(1).value + SelectedRow.Cells(2).value + SelectedRow Cells(3).value
Next
Cheers,
Pete
LINQ is your friend in this case.
Dim sum = dgv_service_centers.SelectedRows.
Cast(Of DataGridViewRow)().
Sum(Function(row) row.Cells.
Cast(Of DataGridViewCell)().
Sum(Function(cell) CInt(cell.Value)))
The inner expression sums the Integer values from each cell in the current row and the outer expression sums all those sums for the selected rows. If the values are some type other than Integer, replace the Cint as appropriate.

Data Validation of a Filtered table

I have a Data table with an Auto Filter (shown Below).
Sub Tariff_Filter()
Dim columnNumber, tableRow, tableColumn, tableWidth As Integer
Dim tableName, columnName As String
tableName = "Tariff_Table"
columnName = ActiveSheet.Range("A1").Value
'This clears the existing filter
ActiveSheet.ListObjects(tableName).Range.AutoFilter
'Assign some numbers we need to know about the table to check the headers
tableRow = ActiveSheet.ListObjects(tableName).Range.Row
tableColumn = ActiveSheet.ListObjects(tableName).Range.Column
tableWidth = ActiveSheet.ListObjects(tableName).Range.Columns.Count
'If a column title with the specified value does not exist VBA throws an error which we need to catch
On Error GoTo ErrorHandler
'Search through the table column header row to find the specified column and assign the number to columnNumber
columnNumber = Application.WorksheetFunction.Match(columnName, Range(Cells(tableRow, tableColumn), Cells(tableRow, tableColumn + tableWidth)), 0)
'Apply the filter "1" to the found columnNumber
ActiveSheet.ListObjects(tableName).Range.AutoFilter field:=columnNumber, Criteria1:="1"
'Exit the sub otherwise the "error handling" will be provoked
Exit Sub
ErrorHandler:
MsgBox columnName & "Please Specify Required Channel"
End Sub
As i cant seem to figure out how to get my combo-box's to show only the visible cells after filtering the table i was wondering if there is a way i can create a a validation box to show the visible cells or copy the visible data into a seperate table underneath. I can then use the validation box/ secondary table as a focus point for the combo-box's on the user-form.
Thanks in advance
If I'm understanding your question correctly, you would like to have a data-validation drop-down list that updates as the table is filtered and only displays visible items for a given column.
You can do this by using the following formula in Data Validation (I'm assuming your table header row starts in A1 and it's col A you need to display):
=OFFSET($A$2,,,SUBTOTAL(103,TableName[column name]))
This formula expands from the starting cell (A2) by a specified height in number of rows. We are defining the height using SUBTOTAL with function number 103 - this means that the height is defined using COUNTA, but only on visible cells, so it will expand and collapse as the table is filtered.
Be aware: since the height is defined using a counta function, it will only count cells containing data, therefore if you have blanks in your table, the range will not be defined correctly. Also if you have any repeated data, these will be repeated in your drop-down box, this method will not condense them into a neat, unique list.
Hope this is helpful.
D

How to have values from 3 columns shown in combobox VBA (active x control)

In combobox I set columnCount to 3, so when I click dropdown arrow I can see 3 columns that I need, but when I choose one row that I need, there is only value from first column shown. Combobox is wide enough for all three columns. Is there a way to see all 3 when I select my choice?
You need to change the ListFillRange to all the columns in your list:
Sheet1!$A$5:C20
Also you need to have a single cell referenced in the LinkedCell property: Sheet1!$A$1
The bound column must be a value between 1 and 3. You can only return a single value from the list - this will be from the bound column.
Your column count must be 3.
Your column widths must be either blank or a value >0 (0 will hide the column):
85.05 pt;85.05 pt;85.05 pt
With those in place you should be seeing three columns of values in the list box - you can only return a value from one of those columns though.
If you want to return more than one I'd suggest using a hidden (column width of 0) column to contain a unique identifier and then use a look-up on the sheet to fill in the blank columns.
To get to all three columns in VBA use code similar to:
Private Sub ComboBox1_Change()
With Me.ComboBox1
MsgBox .Column(0) & vbCr & .Column(1) & vbCr & .Column(2)
End With
End Sub

Get value of one columns in DataGridView with multi select vb.net

I want get value of one columns in DataGridView with multi select in vb.net
when I use
For Each cell In DGNo.SelectedCells
If cell Is Nothing Then
Exit Sub
End If
If Not FirstValue Then
str += ","
End If
str += cell.Value.ToString
FirstValue = False
Next
it take all values DataGridView. How get value of column(0)?
You can get the individual cell values using the DataGridView.Item property which provides an indexer to get or set cell value in the given location.
DataGridView(ColumnNumber,RowNumber)
In your case, use it like below to get cell value from first column in first row.
DGNo(0,0).Value
To get the value of only column 0 in the selected cells,
For Each cell In DGNo.SelectedCells
If cell.ColumnIndex =0 Then
' do work
End If
Next