Datagridview menuitem - get data of datagridview row - vb.net

I am using a Datagridview, and if I right click on a row in the Datagridview I am catching the Datagridview MouseClick handler. Inside this handler I create a menuItem, if I click one entry in this menuItem I want to execute some code, and this code should get some data from the row I have selected in the Datagridview. How can I do this?
I am using this kind of code to create menuItem entrys:
Dim firefox As New MenuItem("Firefox", AddressOf contextMenu_ItemClicked)
firefox.Tag = "firefox"
Another question, do I need to add this tag, or am I also able to read out the name of the menuitem in the contextMenu_ItemClicked function?
Thanks!

Related

VB.NET - Unable to click data within datagridview

I have a datagridview in a form and it opens within a panel. When it opens you can't click any of the cells in table. You can click the cells when the form opens not in the panel. I'm guessing that it's something to do with the way the form opens within the panel, But i'm not sure.
I'm able to reorder the table and resize rows.
People.WindowState = FormWindowState.Maximized
People.FormBorderStyle = Windows.Forms.FormBorderStyle.None
People.Visible = True
Panel2.Controls.Add(People)
Image of table within panel
If you cannot or will not extract your datagridview from your people form (and throw the peopleform away), add this to the People form:
Public Readonly Property TheGrid As DataGridView
Get
Return Me.DataGridViewNameHere
End Get
End Property
Then add the grid to the panel, not the whole form:
Panel2.Controls.Add(People.TheGrid)

Unbound Data Repeater Scroll Issue

I Dragged a DataRepeater into my form.
Added a TextBox to the DataRepeaterItem.
Added A button to the form.
Wrote these 2 Lines of Code :
Private Sub Button1_Click(..) Handles Button1.Click
DataRepeater1.VirtualMode = True
DataRepeater1.AddNew()
End Sub
Run Project
Press Add Button
in the textBox Write "1"
Press Add Button
in the textBox Write "2"
Press Add Button
in the textBox Write "3"
Press Add Button
in the textBox Write "4"
Till Here Every Thing is Fine.
Then Scroll data repeater Up
"1" Changes to default TextBox1
Why Does it happen. How can I prevent it from happening.
Thanks in Advance.
The repeater control isn't going to hold all the values by itself. In virtual mode you don't have to use a datasource, but you have to use something. In this example, they used a simple Integer array: VB.NET Repeater Simple Data Binding Without Datasource

debugging shows that all items in checkbox list as unchecked when there are 3checked

so im dynamically populating a checkbox list. I have confirmed that my text and values are correct for each checkbox but when I check a few and click my event button when I loop through the items they are all set to select=false...
Dim resource As ListItem
Dim SelectedHashTable As New Hashtable
For Each resource In chkResources.Items
If resource.Selected = True Then
SelectedHashTable.Add(resource.Text, resource.Value)
End If
Next
set checkpoint at line 5 to view contents of hash table but it is never triggered. Even when I check all boxes. Anyone any idea?
Where are you dynamically populating the checkboxlist? If it's any time after the OnInit event, then the control's viewstate is not getting saved properly and your selections will be overridden on every postback. Try dynamically populating your list in the OnInit handler.

listview in childform

I have 3 forms, one frmMain - main form, second is frmUserType- childform, and the last frmCreateUserType. In the main form I have a menu item to open my frmUserType, In this form I have a button to open another form which is my frmCreateUserType, In this form I have a button to add records then update the listview in frmUserType. The problem is the listview will not access with my add button control in frmCreateUserType. I tried not to used mdiparent declaration for my frmMain and frmUserType as children and it works, so meaning that the problem is showing my frmUserType as childform?I am using vb.net 2008
Any suggestion would greatly appreciated
Thans in advance
Code to open my second form (frmUserType)
Dim frmChildUserType As New frmUserType
frmChildUserType.MdiParent = Me
frmChildUserType.WindowState = FormWindowState.Maximized
frmChildUserType.Show()
Code for my add button to update the listview in frmUserType
frmUserType.lsvUserType.Items.Clear()
FillListViewUserType("SELECT * FROM pr_user_type", frmUserType.lsvUserType)
You're creating a new instance as:
Dim frmChildUserType As New frmUserType
But in your code:
frmUserType.lsvUserType.Items.Clear()
FillListViewUserType("SELECT * FROM pr_user_type", frmUserType.lsvUserType)
You're not accessing that instance but is instead using the default instance of frmUserType. So I think you're updating a different instance of the ListView than what you think.
If you change your code to instead be:
frmChildUserType.lsvUserType.Items.Clear()
FillListViewUserType("SELECT * FROM pr_user_type", frmChildUserType.lsvUserType)
I think it would work as you expect.
If you don't know what a default instance is, you can find a blog about them here (I think they're a bad idea).

Referencing cell values on DataGridView from another form

This is bugging me. I have a "main" form named frmMain with a DataGridView object named objDataGrid.
When a user double clicks on a row or clicks another button, I hide the main form, open a new form and want to reference the values in the row selected by the user but I keep getting an error when I try to access some, but not all, of the datagridview's properties.
For example here is the code that opens the form:
On Error Resume Next
Me.Hide()
frmGenerate.Show()
In the frmGenerate's load event I do the following:
Dim frmMain As frmMain = Nothing
frmMain = New frmMain
Any time I try to grab a value from the grid I get various errors.
frmMain.objDataGrid.Item(1, 2).Value
frmMain.objDataGrid.Rows(2).Cells(4).Value
frmMain.objDataGrid.SelectedRows(0).Index
frmMain.objDataGrid.Item(1, frmMain.objDataGrid.SelectedRows(0).Index).Value
These return index out of range errors even though I know for sure the indexes are correct.
Any ideas?
Thanks.
Why are you creating a new instance of frmMain? You need to refer to the existing one where they've selected a row.
You could pass just the row of the datagrid or PK field values instead of referencing the other form.