One row selector in all Datagridview - vb.net

Based on what you see below is the sample of a datagridview that an item has been selected.
Here is my question what if I have a more than 1 Datagridview? I mean 5 Datagridview like this.
All of them contains 1 column only. Based on the 1st image, the row selector or the blue one select an item.
My question is how can I make all of the datagridview only have one row selector?
What happens is when i selected each of them all of it has a row selected 5 selections.
How can I make 1 row selector for all of them.
Thinking of changing the Selection Color but I think that's not applicable.
TYSM for future help.

If you're looking for an alternative, you can also try this approach:
Private Sub DataGridView_CellEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles _
DataGridView1.CellEnter, DataGridView2.CellEnter, DataGridView3.CellEnter, DataGridView4.CellEnter, DataGridView5.CellEnter
Dim MyDataGrids() As DataGridView = {DataGridView1, DataGridView2, DataGridView3, DataGridView4, DataGridView5}
For i = 0 To MyDataGrids.Count - 1
If MyDataGrids(i).Name = sender.Name Then
Continue For
Else
MyDataGrids(i).ClearSelection()
End If
Next
End Sub
MyDataGrids() is an array of DataGridViews. If for example, the controls you need to check increases, just add the name of the DataGridView in this array and it will be included in the checking and clearing of selections. Don't also forget the Handles event. As you can see here, all of the five grids .CellEnter event are included so you don't have to copy-paste it to five separate events.

Try this maybe it is more easy to edit if you add more grid
Private Sub ClearSelectedCells(ByVal Identifier As Integer)
If Identifier = 1 Then 'for datagrid 1
dg2.ClearSelection()
dg3.ClearSelection()
ElseIf Identifier = 2 Then 'for datagrid 2
dg1.ClearSelection()
dg3.ClearSelection()
'and so on
.
.
End If
End Sub
Private Sub dg1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles dg1.CellClick
ClearSelectedCells(1)
End Sub
'and other gridcellclick
.
.

Related

Updating Values in a Data Grid View With Key Press Data Entry in Numeric Up and Down

I am setting up a Data Grid View that contains a list of items, with one column as the Item Number (1, 2, 3) and the second number as the name of the items. I am controlling the input of the Item Number column using a Numeric Up and Down, so when I increase it, the rows are added and the Item Number column is automatically updated (so the user doesn't have to enter this in). However, this only works if I use the numeric up and down by clicking the arrows. If I type in the number (say 4 items), I get an exception (System.ArgumentOutOfRangeException: 'Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index') and the procedure doesn't work. The code is below:
Private Sub numItemNumber_ValueChanged(sender As Object, e As EventArgs) Handles numItemNumber.ValueChanged
While (dgvItems.Rows.Count < numItemNumber.Value)
dgvItems.Rows.Add()
i = numItemNumber.Value
dgvItems.Rows(i).Cells(0).Value = i + 1 'This is where the exception is
End While
End Sub
I added in a KeyPress event to see if that could handle the item entered in but it doesn't.
Private Sub numItemNumber_KeyPress(sender As Object, e As EventArgs) Handles numItemNumber.KeyPress
For i = 0 To numItemNumber.Value - 1
dgvItems.Rows(i).Cells(0).Value = i + 1
Next
End Sub
How can I edit this to include both events (the user using the up and down keys or just entering the number in directly)? Thank you.
Don't you still have to Add the new row before you can enter a value in it?
Private Sub numItemNumber_KeyPress(sender As Object, e As EventArgs) Handles numItemNumber.KeyPress
For i = 0 To numItemNumber.Value - 1
dgvItems.Rows.Add()
dgvItems.Rows(i).Cells(0).Value = i + 1
Next
End Sub
Figured out this works, essentially adding a loop through the rows of the data grid view in the Value Changed event so that it can handle any number > 0. Adding the code here for anyone who possibly needs it:
Private Sub numItemNumber_ValueChanged(sender As Object, e As EventArgs) Handles numItemNumber.ValueChanged
While (dgvItems.Rows.Count < numItemNumber.Value)
For i=0 to numItemNumber.Value-1
dgvItems.Rows.Add()
dgvItems.Rows(i).Cells(0).Value = i + 1
Next
End While
End Sub
The KeyPress Event is not needed.

How to remove a row from datagridview and reset a particular column (vb.net 2005)

I am using VS 2005 edition. I am developing a winform desktop application. Let's say I have a unbound datagridview, with table like below:
Original datagrid before delete:
If I removed Stuff D (Item No 5), the "Item No" column supposed to reset itself accordingly. The expected output should be:
After delete row:
The "Item No" column is not an autonumber, it's just the number I assigned incrementally as the user add in a new row(new Stuff). I tried using the following code in rowremoved event but failed to achieve the expected output. Please help. Thanks.
If the goal is just showing the record index in cell, then you don't need to assign a value to cell and it's enough to set e.Value = e.RowIndex + 1 in CellFormatting event.
But based on your comment it seems you need those cells have values as well. So you need to handle RowsRemoved and RowsAdded event of DataGridView and refresh the cell value with row number:
Public Sub RefreshRowNumbers(g As DataGridView)
For Each r As DataGridViewRow In g.Rows
r.Cells(1).Value = r.Index + 1
Next
End Sub
Private Sub DataGridView1_RowsRemoved(sender As Object, _
e As DataGridViewRowsRemovedEventArgs) Handles DataGridView1.RowsRemoved
RefreshRowNumbers(DirectCast(sender, DataGridView))
End Sub
Private Sub DataGridView1_RowsAdded(sender As Object, _
e As DataGridViewRowsAddedEventArgs) Handles DataGridView1.RowsAdded
RefreshRowNumbers(DirectCast(sender, DataGridView))
End Sub

Silverlight Datagrid New Row Goes to Bottom and Screws Up Multi-Delete Logic

I have a Silverlight 4 application I've been tasked with making some changes to. I'd rather redo it in something supported, but that's not an option at the moment. I have a datagrid and I've added a button to insert a new row into the datagrid and database table using the values from two comboboxes:
Private Sub btnAddAccountGroupLink_Click(sender As Object, e As System.Windows.RoutedEventArgs) Handles btnAddAccountGroupLink.Click
Dim sv_ag As Com_AccountGroup = cbACCT_GROUP.SelectedValue
Dim sv_cf As CS_FUND = cbACCT_CD.SelectedValue
AccountGroupLinkData.DataView.Add(New Com_AccountGroupLink() With {.ACCT_GROUP = sv_ag.ACCT_GROUP, .ACCT_CD = sv_cf.ACCT_CD})
AccountGroupLinkData.SubmitChanges()
End Sub
When the row is added, it gets added to the bottom of the datagrid instead of it's properly sorted location. This also breaks my code that deletes selected indexes from the datagrid:
Private Sub btnRemoveSelected_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles btnRemoveSelected.Click
If MessageBox.Show("Are you sure you want to delete the selected records?", "Confirmation", MessageBoxButton.OKCancel) = MessageBoxResult.OK Then
Dim indexesToDelete As New List(Of Integer)
For i As Integer = 0 To AccountGroupLinkData.DataView.Count - 1
If AccountGroupLinkData.DataView(i).IsSelected Then
indexesToDelete.Add(i)
End If
Next i
For d As Integer = 0 To indexesToDelete.Count - 1
AccountGroupLinkData.DataView.RemoveAt(0)
Next d
AccountGroupLinkData.SubmitChanges()
End If
End Sub
So if I delete the bottom row, it actually deletes the row that's located where this row should be.
How can I force the datagrid to update or show the row in its correct position?
I solved this by adding this code after the SubmitChanges:
NavigationService.Refresh()
This refreshes the Silverlight page and the row pops up to the proper position.

How to adding checked item from checkedlistbox to combobox

I want to adding checked item from checkedlistbox to my combobox, but i have a little problem here. Combobox only show 1 item last checked.
This is my sample code.
If CheckedListBox1.CheckedItems.Count <> 0 Then
For i As Integer = 0 To CheckedListBox1.CheckedItems.Count - 1
cbCheckedItem.Text = CheckedListBox1.CheckedItems(i).ToString
Next i
End If
anyone can help me show all checked item??
thank's for your help...
You aren't adding the items to the combo box, you're only setting its Text property. That's only changing the text currently displayed in the combo box, and only one item can be displayed at a time.
If you want the items to be permanent and selectable, you need to add them to the combo box control's Items collection.
Sample code:
If CheckedListBox1.CheckedItems.Count > 0 Then
For Each checkedItem In CheckedListBox1.CheckedItems
cbCheckedItem.Items.Add(checkedItem.ToString())
Next
End If
Or, better yet, use the AddRange method:
If CheckedListBox1.CheckedItems.Count > 0 Then
Dim checkedItems() As String = CheckedListBox1.CheckedItems.Cast(Of String).ToArray()
cbCheckedItems.Items.AddRange(checkedItems)
End If
Oddly enough the CheckedListBox has a CheckedItems property, which is a collection. As such you can loop through it like you can any other collection, using a For or For Each loop.
then, Each item needs to be added to the Items collection of the ComboBox.
like this sample:
Public Class frmCheckedListBox
Private Sub frmCheckedListBox_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.CheckedListBox1.Items.Clear()
Me.CheckedListBox1.BeginUpdate()
Me.CheckedListBox1.Items.Add("One")
Me.CheckedListBox1.Items.Add("Two")
Me.CheckedListBox1.Items.Add("Three")
Me.CheckedListBox1.Items.Add("Four")
Me.CheckedListBox1.Items.Add("Five")
Me.CheckedListBox1.EndUpdate()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
For Each Item As String In Me.CheckedListBox1.CheckedItems
Me.ComboBox1.Items.Add(Item)
Me.ComboBox1.SelectedIndex = 0
Next
End Sub
End Class
As sample code shows, the CheckedItems collection contains the items that are checked, just as the name suggests. It doesn't contain a Boolean value for each an every item to indicate whether it is checked or not. If an item is checked then that item is in the CheckedItems, and if it isn't then it isn't. You simply need to loop through the collection and get every item in it, because it contains all the items that are checked and none that aren't.
in the end you can put :
Me.Combobox1.items.clear()
because when it would click with sample code it would have the one that clicked then on the next click would return the previous one it had clicked and then the new one all compiled in the combobox selection menu
perhaps my answer can help you solve your problems
Combobox doesn't have a multiselect option. so only one item at a time could be selected.

how to put search functionality in dataGridView in vb.NET

How can I select a single cell from selected row in datagridView and after selecting that I want to put simple search functionality (like we have in our windows folders-typing any characters and search should work)?
I do not really understand your question. If you want to select one cell, you can use the celldoubleclick event for exemple. And the to get the selected cell, use e.rowindex and e.columnindex which will give you the row and the column where the cell is located.
You can try this as a possible solution.
Dim nwData as CustomersDataSet = CustomersDataSet.GetCustomers()
m_CustomersGrid.DataSource = m_CustomersBindingSource
m_CustomersBindingSource.DataSource = nwData.Customers
Then you can sort using the BindingSource.
CustomersBindingSource.Sort = "ContactName ASC"
And you can find using the BindingSource.
Dim index as integer = _
CustomersBindingSource.Find("CompanyName", CompanyNameTextBox.Text)
If index <-1 then 'it was found; move to that position
CustomersBindingSource.Position = index
End If
You could then populate:
CustomersBindingSource.Find("CompanyName", CompanyNameTextBox.Text)
with the keys pressed in the cell by capturing them by utilizing:
Private Sub DataGridView1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles DataGridView1.KeyUp
Dim dgv As DataGridView = TryCast(sender, DataGridView)
If dgv IsNot Nothing Then
'You will need some logic here to determine how long to wait between keyups
'Perhaps a timer that ticks every500 milliseconds and reset on keyup.
e.KeyData
End If
End Sub
I found the original Biding Source Logic at : This Location