GridView1.SelectedRow returning nothing - vb.net

My problem is that in the below code, the SelectedRow property returns nothing. I manually bind items to GridView at runtime and autogeneratecolumns and autogenerateselectbutton properties are true.
I think the problem is about having a PostBack when the Select command is clicked.
Thanks a lot.
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
Session("ContactID") = GridView1.SelectedRow.Cells(0).Text()
Response.Redirect("~/ContactAddress.aspx")
End Sub

The row you want is accessible via e (the GridViewCommandEventArgs parameter), specifically the value of e.CommandArgument which will have the row index :
From here :
To determine the index of the row that
raised the event, use the
CommandArgument property of the event
argument that is passed to the event.
The ButtonField class automatically
populates the CommandArgument property
with the appropriate index value. For
other command buttons, you must
manually set the CommandArgument
property of the command button. For
example, you can set the
CommandArgument to <%#
Container.DataItemIndex %> when the
GridView control has no paging
enabled.

The selected row is in the GridViewCommandEventArgs parameter.

Besides adding the CommandArgument, you need to change your code to the following.
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
Dim rowNumber As Integer = e.CommandArgument
Dim ContactId As Integer = GridView1.Rows(rowNumber).Cells(0).Text
Session("ContactID") = ContactId
Response.Redirect("~/ContactAddress.aspx")
End Sub

Related

How to access data from calling object in vb.net

I have a Window-Form 'caller' in vb.net containing a datagridview with a small overview table of certain objects, each with its own ID in the first column. Now, if a row is double clicked, i want to show a dialog 'edit', where one can edit many details of that row which i do not want in the overview table.
My approach is as follows: In the caller form i wrote this to call 'edit':
Private Sub dgdata_dbclick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dg_data.CellMouseDoubleClick
Dim f_edit As New edit
f_edit.ShowDialog(Me)
End Sub
That works fine.
However, in the called Form "edit" i need to check, which ID was selected and load this data from the database to edit it. I can access some data from the calling form 'caller' using e.g.
MsgBox(CType(Me.Owner, caller).Text)
to show the window title of 'caller'. However, i want to extract the currently selected ID in the datagridview or at least some variabhle containing it. In the caller form, this could be easily done by evaluating
dg_data.Item(0, selectedRow).Value.ToString
but i cannot access any relevant information in 'caller'. I have a public class with some global variables there but i cannot access them as well.
Probably my strategy to solve this problem is not the most clever approach? Basically, i want to open a very detailed edit window when someone clicks on a line in an overviewtable but simultaniously blocking the rest of the application as long as the edit window is open.
Thanks!
The idea is to pass the data to the second form. When you create an instance of the second form (my class is called Form2, yours is called edit) with the New keyword the Sub New is called on Form2.
Private Sub OpenEditDialog()
Dim f_edit As New Form2(32) '32 is the number you retrieve from your DataGridView
f_edit.ShowDialog(Me)
f_edit.Dispose()
End Sub
You pass the ID to Form2 and set a variable at Form level. You can then use the variable anywhere in Form2.
Public Class Form2
Private ID As Long
Public Sub New(SelectedID As Long)
InitializeComponent()
ID = SelectedID
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
MessageBox.Show(ID.ToString)
End Sub
End Class
You need to call InitializeComponent() so the controls will show up.
How do you usually get data into objects? You set a property or pass an argument to a method or constructor? Why should this be any different? Decide which you want to use and then write that code in your form. If it's required data, I would suggest a constructor. Just write this code in your form:
Public Sub New
and hit Enter. That will generate a little extra code automatically. You can then add a field to store the value, a parameter to the constructor and then assign the parameter to the field inside.
Thank you for pointing me to the correct route.
I solved it like this (which works fine and which is hopefully acceptable):
In the calling form:
Private Sub dgdata_dbclick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dg_data.CellMouseDoubleClick
Dim selectedRow As Integer = dg_data.CurrentCell.RowIndex
Dim f_edit As New edit
f_edit.edit(dg_data.Item(0, selectedRow).Value.ToString)
f_edit.ShowDialog(Me)
f_edit.Dispose()
End Sub
In the called form:
Public Sub edit(ByVal id As Long) 'Handles MyBase.Load
'Enter commands to prepare your form
End Sub

vb.net ComboBox Text Changing When Left

I'm having an issue with my ComboBoxes whereby if I type into it to get a value & then tab out the Text changes to the first item in the list with the first letter typed.
I have:
AutoCompleteMode set to SuggestAppend
AutoCompleteSource set to ListItems
DropDownStyle set to DropDownList
I add the items for the ComboBox in the Load event of the Form the ComboBox is on.
e.g. the below is code from a Load event where I populate a ComboBox that I have set up as below.
`Me.ComboBox1.Text = ""
Me.ComboBox1.Items.Add("a")
Me.ComboBox1.Items.Add("aaa")
Me.ComboBox1.Items.Add("combo")
Me.ComboBox1.Items.Add("combobox")
Me.ComboBox1.Items.Add("combobox test")
Me.ComboBox1.Items.Add("common")
Me.ComboBox1.Items.Add("common dialog")`
After running the code, if I select the ComboBox1 & type in common - common is selected in ComboBox1 but if I leave ComboBox1 the Text reverts to combo.
It gets a bit stranger as if I user the below code in the ComboBox1_Leave event procedure it throws common:
MsgBox(ComboBox1.Text)
I've also tried assigning the value of Text to a string in the ComboBox1_KeyUp event procedure & then assign that to ComboBox1.Text in the ComboBox1_Leave event procedure but that doesn't do anything.
If I put a the above MsgBox code before assigning the strings value to ComboBox1.Text then the Text value does revert to Common but this is isn't a practical solution.
I've also noticed that if I hit Enter before hitting tab it retains the correct value but again I'm don't think this is a particularly practical solution.
Does anyone have any idea what's going on here & how I can fix it?
It is absolutely necessary to have the DropDownStyle set to DropDownList?
Because if you set DropDownStyle to DropDown the selected value will be retained when you press tab or lose the focus.
If it's absolutely necessary to have it that way, you could try this.
Public Class Form2
Dim selectedTextForCombo As String = ""
Private Sub Form2_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Me.ComboBox1.Text = ""
Me.ComboBox1.Items.Add("a")
Me.ComboBox1.Items.Add("aaa")
Me.ComboBox1.Items.Add("combo")
Me.ComboBox1.Items.Add("combobox")
Me.ComboBox1.Items.Add("combobox test")
Me.ComboBox1.Items.Add("common")
Me.ComboBox1.Items.Add("common dialog")
End Sub
Private Sub ComboBox1_LostFocus(sender As Object, e As System.EventArgs) Handles ComboBox1.LostFocus
ComboBox1.SelectedItem = selectedTextForCombo
'This is just for a visualization of your issue
'Label1.Text = selectedTextForCombo
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
selectedTextForCombo = ComboBox1.Text
'This is just for a visualization of your issue
'Label1.Text = selectedTextForCombo
End Sub
End Class
Warning:
This example works with the tab action.
If the users writes something that doesn't exist like "commun" the
selected value will end up being the visually selected value, in this
case: "common"

Basic solution for adding list item to combo box via code on load

What I need:
I was a basic equivalent of a select box i.e. a combobox of dropdown list style (that preferably doesn't allow text input).
I need to add the list items by code rather than the property box.
What I have:
Private Sub Form_Load ()
ComboStaffMember.AddItem "John Murphy"
End Sub
...produces "...add item is not a member of system.windows.forms.comboxbox".
Private Sub Form_Load ()
ComboStaffMember.Items.Add("John Murphy")
End Sub
...produces no result.
My question:
Why is the item not adding? The form name is FrmStaffLogIn and it's in Form1.vb. Should Form_Load correspond to either of these or is my code incorrect elsewhere?
Try to put combo add statement in following format in form load event :
Private Sub Form_Load ()
Me.ComboStaffMember.Items.Add(New DictionaryEntry("Text to be displayed", 1))
End Sub
Are you sure your code line ComboStaffMember.Items.Add("John Murphy") doesn't work? it should work just fine.
The Add() method On Item collection expects object parameter and string as well can be passed as argument to it. Like below [C# code sample]:
this.comboBox1.Items.AddRange(
new string[] {"SomeText","SomeOtherText","LastText"});
Also, you probably don't see any item cause you haven't set a default selected item. Just expand the dropdown and you will see the items. To set the default selected item
this.comboBox1.SelectedIndex = 0;
Working code:
Private Sub FrmIdentCust_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ComboStaffMember.Items.Add("John Murphy")
End Sub
I was missing (sender As Object, e As EventArgs) Handles MyBase.Load.

Value of global variable won't change

I have a variable whose scope needs to be global, because it needs to be called in a function as well as in a button press. So I declared the variable in a Module so it would be global.
The problem is that the value of this variable needs to be equal to the value of the text property of a textbox in the form.
Here you can download the VB.net demonstration of my problem: http://db.tt/DDxQJDXl
Below is an explanation of what happens
You enter a string into the textbox, in this case I entered "Hello". Then you click the button and it displays what you wrote.
You click OK in that message box and change the value in the textbox. In this case I changed it to "Goodbye". Then I hit the button again, but the variable did not change values and the messagebox displays "Hello" again.
Here is the entire source code:
Module Module1
Public strDataValue = frmTest.txtDataValue.Text
End Module
Public Class frmTest
Private Sub btnTest_Click(sender As System.Object, e As System.EventArgs) Handles btnTest.Click
MsgBox(strDataValue)
End Sub
End Class
Note: This is just a demonstration of a problem I'm having in a much larger program so the variable does have to be global.
You need to set the value of the field to the new value in the TextBox:
Private Sub btnTest_Click(sender As System.Object, e As System.EventArgs) Handles btnTest.Click
strDataValue = txtDataValue.Text
MsgBox(strDataValue)
End Sub
The field will not change values by itself.
The value of strDataValue won't automatically change when txtDataValue.Text changes. You need to update strDataValue manually, either when the textbox loses focus, or when you click the Test button.
You could also have a public property, which automatically returns the actual value as long as the form is open.
Public ReadOnly Property DataValue() As String
Get
Return frmTest.txtDataValue.Text
End Get
End Property
You need to assign value of txtDataValue.Text to your variable strDataValue in better performance
Using of Timer object or TextChenged event not recommended
Any change of txtDataValue.Text can assign after end of editing action
for me best solution: Leave or LostFocus events
Private Sub txtDataValue_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtDataValue.Leave
strDataValue = txtDataValue.Text
End Sub

ASPxGridview Change Default value in inline EditForm

I'm trying to change the value in an editform before it becomes visible to the user. However the aspxgridview is always overwriting this.
This is my solution:
Protected Sub ASPxGridView_ItemList_HtmlRowCreated(ByVal sender As Object, ByVal e As DevExpress.Web.ASPxGridView.ASPxGridViewTableRowEventArgs) Handles ASPxGridView_Items.HtmlRowCreated
If (e.RowType = GridViewRowType.EditForm) Then
Dim ASPxTextbox_Number As ASPxTextBox = TryCast(TryCast(sender, ASPxGridView).FindEditFormTemplateControl("ASPxTextbox_Number"), ASPxTextBox)
ASPxTextbox_Number.Text = "99"
End If
End Sub
Please refer to the How to update an ASPxGridView DataSource if the initial values are set programmatically in the HtmlRowCreated event example in this regard.