reflect values to combo box from datagrid on Selection Changed event - wpf

I have a combobox and textbox using which values are filled into DataGridViewfor display purpose.
The datagriduses SelectionChanged event.
I am Trying to reflect the same data (when the user moves keys up and down or on mouse click) onto the combobox and textbox on SelectionChanged event of datagrid
Private Sub DataGridView1_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridView1.SelectionChanged
combobox.Text = DataGridView1.CurrentRow.Cells("DatabaseTable_ColumnName1").Value.ToString()
textbox.Text = DataGridView1.CurrentRow.Cells("DatabaseTable_ColumnName2").Value.ToString()
End Sub
Above code works fine for textbox but not for combobox.
In case of combobox, it reflects data of initially selected row only and not the rest.
How do I fix this?

i guess you have to use this code for combobox
combobox1.items.add('your string here')
in your situation would be like this :
Private Sub DataGridView1_SelectionChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles DataGridView1.SelectionChanged
combobox1.items.add(DataGridView1.CurrentRow.Cells("DatabaseTable_ColumnName1").Value.ToString())
textbox.Text = DataGridView1.CurrentRow.Cells("DatabaseTable_ColumnName2").Value.ToString()
End Sub

Related

How to check which Gridview is selected, if form having more than one Gridview?

In form i created three dynamic Gridview , I want to perform some specific operations on selection of particular Gridview, can any one help me how to know which gridview got seletec?
There is a .GotFocus() event on the DataGridView you can use to store something in a form scope (or global) variable. (sorry for the VB...)
Dim LastGirdWithFocus As String = ""
Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
MsgBox(LastGirdWithFocus)
End Sub
Private Sub dgv1_GotFocus(sender As Object, e As System.EventArgs) _
Handles dgv1.GotFocus, dgv2.GotFocus
LastGirdWithFocus = sender.name
End Sub

SelectedIndexChanged event of dropdownlist in Listview not happening. in a usercontrol

I have a ListView and DropDownList(primary) and tow more and a text box, inside every Row of that ListView. All on a user control ,and using the this user control on the other usercontol(here the operations to be performed.)(we are using DNN) .
Now I would like to add event handler for dropdownlist which would react on SelectedIndexChanged. I wanted to show and hide the other dropdowns and textbox depending on a specific value of the DropDrownList(primary) selected.
I am nowhere to do the same..despite the several attempts.
Screening Outcome:&nbsp
Comments:
Subset Used:&nbsp
TAR Status:&nbsp
code behind like this
Public Sub ddlFindingsScreeningOutcome_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim ddl As DropDownList = CType(sender, DropDownList)
Dim item As ListViewItem = CType(ddl.NamingContainer, ListViewItem)
Dim txtFindingsComments As TextBox = CType(item.FindControl("txtFindingsComments"), TextBox)
Dim ddlFindingsTarStatus As DropDownList = CType(item.FindControl("ddlFindingsTarStatus"), DropDownList)
If Not ddl Is Nothing Then
If Not ddl.SelectedItem.Text = "Not Screened" Then
txtFindingsComments.Visible = True
ddlFindingsTarStatus.Visible = True
End If
Else
txtFindingsComments.Visible = False
ddlFindingsTarStatus.Visible = False
End If
End Sub

How to fire an event when making a selection of an editable combobox?

I have a ComboBox that is databound to an ObservableCollection of strings. The ComboBox is also editable, so you can either enter in your own value or select one from the list. The issue I'm running into is the index of SelectedItem seems to be the index of the last item you selected when you've entered in your own value in the ComboBox, though it's -1 when you have IsTextSearchEnabled set to true.
The problem is, if someone entered their own value and then decide to instead select the item on the ComboBox that had been selected before, the index doesn't change so the SelectionChange event doesn't fire. How could I get an event to fire in this situation?
Test this...
I hope this helps:
Dim oldSEL As String = ""
'always checking while you move your mouse over the combobox (when altering selection) and using the keyboard to (alter selection)
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.MouseMove, ComboBox1.KeyPress
Dim currentSEL As String = ComboBox1.SelectedText
If Not (oldSEL = "" And currentSEL = oldSEL) Then
fire()
oldSEL = currentSEL
End If
End Sub
Private Sub fire()
Trace.Write("text selected changed")
End Sub
You should change all the Combobox1 to your liking.

Delaying opening of sub-menus in a menustrip

Our environment: Visual Studio 2010, c#, .net 4 client profile.
We have a Winforms application that contains a menustrip in its main form. The items of the menustrip contain both an image (64x64) and text. The main form also has a TabControl which contains 5 tabs. In the OnLoad() method of the main form, we hide the TabControl headers so that they are not visible and therefore not clickable. Instead, when the user clicks on an item in the menustrip, we switch the active tab.
However, our menus have many sub-menu items, and since we use the main menustrip to select the active tab, we would like the sub-menu items to appear only after the user clicks the menu item for a period of time, not instantaneously. Otherwise, whenever the user changes his/her active view (by selecting a tabPage), the sub-menus appear on the screen since he/she clicked a menustrip item that contains sub menus.
Is this possible?
I don't completely understand the rationale, but you can delay the display of a submenu using the MouseDown handler and sleep function, like this:
Private Sub FileToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FileToolStripMenuItem.MouseDown
System.Threading.Thread.Sleep(2000) ' wait two seconds
End Sub
======================
(Edit: Added second solution)
You can do this with a timer control and ShowDropDown/HideDropDown:
Private Sub FileToolStripMenuItem_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles FileToolStripMenuItem.MouseDown
' show tab here'
FileToolStripMenuItem.HideDropDown()
Timer1.Interval = 500
Timer1.Start()
End Sub
Private Sub FileToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FileToolStripMenuItem.Click
FileToolStripMenuItem.HideDropDown()
End Sub
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Timer1.Stop()
FileToolStripMenuItem.ShowDropDown()
End Sub

Apply and validate a bound DataGridViewComboBoxCell directly upon selection change

I have a windows forms DataGridView that contains some DataGridViewComboBoxCells that are bound to a source collection using DataSource, DisplayMember and ValueMember properties. Currently the the combobox cell commits the changes (i.e. DataGridView.CellValueChanged is raised) only after I click on another cell and the combobox cell loses focus.
How would I ideally commit the change directly after a new value was selected in the combobox.
This behaviour is written into the implementation of the DataGridViewComboBoxEditingControl. Thankfully, it can be overridden. First, you must create a subclass of the aforementioned editing control, overriding the OnSelectedIndexChanged method:
protected override void OnSelectedIndexChanged(EventArgs e) {
base.OnSelectedIndexChanged(e);
EditingControlValueChanged = true;
EditingControlDataGridView.NotifyCurrentCellDirty(true);
EditingControlDataGridView.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
This will ensure that the DataGridView is properly notified of the change in item selection in the combo box when it takes place.
You then need to subclass DataGridViewComboBoxCell and override the EditType property to return the editing control subclass from above (e.g. return typeof(MyEditingControl);). This will ensure that the correct editing control is created when the cell goes into edit mode.
Finally, you can set the CellTemplate property of your DataGridViewComboBoxColumn to an instance of the cell subclass (e.g. myDataGridViewColumn.CellTemplate = new MyCell();). This will ensure that the correct type of cell is used for each row in the grid.
I tried using Bradley's suggestion, but it was sensitive to when you attached the cell template. It seemed like I couldn't allow the design view to wire up the column, I had to do it myself.
Instead, I used the binding source's PositionChanged event, and triggered updates from that. It's a little bit odd, because the control is still in edit mode, and the databound object doesn't get the selected value yet. I just updated the bound object myself.
private void bindingSource_PositionChanged(object sender, EventArgs e)
{
(MyBoundType)bindingSource.Current.MyBoundProperty =
((MyChoiceType)comboBindingSource.Current).MyChoiceProperty;
}
A better way to achieve this that I am using successfully rather than subclassing or the somewhat inelegant binding source method above, is the following (sorry it's VB but if you can't translate from VB to C# you have bigger problems :)
Private _currentCombo As ComboBox
Private Sub grdMain_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles grdMain.EditingControlShowing
If TypeOf e.Control Is ComboBox Then
_currentCombo = CType(e.Control, ComboBox)
AddHandler _currentCombo.SelectedIndexChanged, AddressOf SelectionChangedHandler
End If
End Sub
Private Sub grdMain_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grdMain.CellEndEdit
If Not _currentCombo Is Nothing Then
RemoveHandler _currentCombo.SelectedIndexChanged, AddressOf SelectionChangedHandler
_currentCombo = Nothing
End If
End Sub
Private Sub SelectionChangedHandler(ByVal sender As Object, ByVal e As System.EventArgs)
Dim myCombo As ComboBox = CType(sender, ComboBox)
Dim newInd As Integer = myCombo.SelectedIndex
//do whatever you want with the new value
grdMain.NotifyCurrentCellDirty(True)
grdMain.CommitEdit(DataGridViewDataErrorContexts.Commit)
End Sub
That's it.

Resources