Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Getting a databound label to display based on a variable's value
 

Getting a databound label to display based on a variable's value

If I have a label bound to a database displaying only one value (e.g. Venue_Name), how do I get it to conditionally display the value based on a label containing changing variables, such as a label containing "Venue_ID" data? The VenueID label will be constantly changing based on user selection, and I'd like to get the databound label to display its respective Venue_Name based on this label only.
MichaelT123  Sunday, December 07, 2008 1:21 AM
i don't think is very clear, how will your selection change? from a combobox? from a dgv ?
anyway you should bind everything you need to a bindingsource and you can do that with dragdrop from the "data sources" window in visual studio or coding it with
mylabel.DataBindings.Add("Text", MyBindingSource, PropertyName) and then
mybindingsource.datasource = current_venue when the selection is made

another easy way to do it is just to write something like
MyLabel.Text = DirectCast(MyBindingSource.Current, Venue).Venue_Name
in the event handler for MyBindingSource.CurrentChanged

so basically you convert the current object chosen from a grid/combobox/listbox/anything into the object Venue, and then retrieve whatever property you need


if this didn't help, explain better please
Valnuke  Sunday, December 07, 2008 3:23 PM
i don't think is very clear, how will your selection change? from a combobox? from a dgv ?
anyway you should bind everything you need to a bindingsource and you can do that with dragdrop from the "data sources" window in visual studio or coding it with
mylabel.DataBindings.Add("Text", MyBindingSource, PropertyName) and then
mybindingsource.datasource = current_venue when the selection is made

another easy way to do it is just to write something like
MyLabel.Text = DirectCast(MyBindingSource.Current, Venue).Venue_Name
in the event handler for MyBindingSource.CurrentChanged

so basically you convert the current object chosen from a grid/combobox/listbox/anything into the object Venue, and then retrieve whatever property you need


if this didn't help, explain better please
Valnuke  Sunday, December 07, 2008 3:23 PM
Thank you. I might be able to use that in another way, but for this application I need the gridview to display only the venue in Concert_T.Venue_Name that matches the text in a label (titled Label1.Text in the query). I'm trying to get the app to prompt the user for a concert selection in the main form, which passes a variable (Venue_Name or Venue_ID) to a hidden label in this form. I would like for the gridview to recognize this label in the query, and pull corresponding data. Right now it's displaying all venues, and I need only the one corresponding to the label. Thanks for any help!


Me.Concert_TTableAdapter.Fill(Me.CTB_DatabaseDataSet.Concert_T)
myConnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Michael\Desktop\CTB Project\CTB Project\CTB Project\CTB Database.mdb"
Dim myDataAdapter As New OleDbDataAdapter("Select Distinct Venue_Name From Concert_T Where Concert_T.Venue_Name = 'Label1.Text'", myConnection)
Dim myDataSet As New DataSet
myDataAdapter.Fill(myDataSet, "Concert_T")
DataGridView1.DataSource = myDataSet.Tables("Concert_T")
MichaelT123  Sunday, December 07, 2008 4:41 PM

You can use google to search for other answers

Custom Search

More Threads

• Datagrid goes back to default parent view when dataset refreshes
• DataGridViewTextBoxEditingControl adding numeric text inside
• Binding Navigator Delete Item problem
• embedded datagrids
• DataGridView KeyDown event
• Wrrrrr........ saving a "null value" when null NOT allowed ??????????????
• Combobox behavior
• VB.Net: Importing CSV semi-colon delimited through OleDbConnection
• Bug? DataGridView displays data entered in a TextBox before BindingSource.EndEdit() is invoked.
• DataGridViewColumn