VB.NET ComboBox, setting the right "value" - vb.net

I'm working on a hobby project where I will store Sail Boats, Skipper, Clubs, Races and Results. A Skipper is a person that is a member in a club.
So I have table with persons (skippers) and a table with clubs.
I have created an DataGridView where I have the person and the id to the club. I have also added a ComboBox with clubs. When I click on a row I want to load the person into several TextBoxes and also set the right club in the ComboBox, but I can't find how.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim myTable As DataTable = New DataTable
Dim MyRow As DataRow
myTable = FillCluTable() <== Get clubs into a DataTable
MyRow = myTable.NewRow
MyRow(0) = 0
MyRow(1) = "Sel"
MyRow(2) = "Select Club"
myTable.Rows.InsertAt(MyRow, 0)
cbxClubs.DataSource = myTable
cbxClubs.DisplayMember = myTable.Columns("ClubName").ToString
cbxClubs.ValueMember = myTable.Columns("Id").ToString
cbxClubs.SelectedIndex = 0
End Sub

Try with:
cbxClubs.DisplayMember = "ClubName"
cbxClubs.ValueMember = "Id"
Delete the part with MyRow and build the Table header from the designer page

Related

When I name colums i Can no longer edit cells in gridview in winforms

I am using vb.net winforms to display as csv file in a grid. I import into sql a normalized table using entity framework, then retrieve my deliveries table obj in the query below.
Private Sub frmConfirmDeliverys_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim bs As New BindingSource
bs.DataSource = (From u In threeContext.dbContext.skechersDeliveries Where u.isprocessed = False Select New With {.OrderNumber = u.ordernumber, .BarCode = u.tagbarcode, .Qty = u.qty, .Location = u.location})
bs.ResetBindings(True)
dgDeliverys.DataSource = bs
dgDeliverys.BeginEdit(True)
Can you clarify your question ? Anyway if your problem is you can't change your datagridview column header cause of the Datasource, you can replace the column header
example:
"SELECT ID_No AS [ID Number] FROM DBStudent"
Instead of display ID_No it will display ID Number

VB DataGridView 1 Row selection

I have a problem . When I load my form with my datagridview , I want the data to be transferred to the text fields when I click on the > at the most left of the datagridview.
Now, I have to select the content in the rows itself to select a row. How do I set it so it selects row based on the most left row selector? instead of having me click on the contents of the row ? dtguser_CellContentClick makes me select based on the content click right? What should it be if I want to select by entire row?
Private Sub dtguser_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dtguser.CellContentClick
If GroupBox3.Enabled = False Then
Else
'this code will simply pass the value from the specific row selected by the user
lblid.Text = dtguser.CurrentRow.Cells(0).Value
serial.Text = dtguser.CurrentRow.Cells(0).Value
fname.Text = dtguser.CurrentRow.Cells(1).Value
lname.Text = dtguser.CurrentRow.Cells(2).Value
gender.Text = dtguser.CurrentRow.Cells(3).Value
address.Text = dtguser.CurrentRow.Cells(4).Value
contact.Text = dtguser.CurrentRow.Cells(5).Value
course.Text = dtguser.CurrentRow.Cells(6).Value
datestart.Text = dtguser.CurrentRow.Cells(7).Value
datecomplete.Text = dtguser.CurrentRow.Cells(8).Value
datecertify.Text = dtguser.CurrentRow.Cells(9).Value
nationality.Text = dtguser.CurrentRow.Cells(10).Value
email.Text = dtguser.CurrentRow.Cells(11).Value
certification.Text = dtguser.CurrentRow.Cells(12).Value
End If
End Sub
If you want to select the entire row set the selection mode to FullRowSelect.
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
If you want to get the data when you click the RowHeader (> button) then subscribe to the RowHeaderMouseClick event.
dataGridView1.RowHeaderMouseClick += new DataGridViewCellMouseEventHandler(dataGridView1_RowHeaderMouseClick);
Private Sub dataGridView1_RowHeaderMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs)
// put your code for copying row data into the textboxes.
End Sub

Btn.PostBackUrl / Nested with DropDownList INSIDE a Datalist

in VB.net I have a DATALIST in which, among others I have a DropDL CTRL which is Bound with LINQ something like this:
Public Sub DLCategorias_ItemDataBound(sender As Object, e As System.Web.UI.WebControls.DataListItemEventArgs) Handles DLCategorias.ItemDataBound
If e.Item.ItemType = ListItemType.AlternatingItem Or e.Item.ItemType = ListItemType.Item Then
Dim DC As New MyDataContext
Dim cat = (From c In FF.ProductosCategorias Select c Where c.idCategoria = (CTypeDLCategorias.DataKeys(e.Item.ItemIndex), Integer))).Single
Dim thisId As Integer = cat.idCategoria
Dim subcat = From d In dc.ProductosSubCategorias Where d.idCategoria = thisId Select d.idSubCategoria, d.NombreSubCategoria
Dim ddlSubCat As DropDownList = CType(e.Item.FindControl("ddlSubCat"), DropDownList)
Dim BTNVER As Button = CType(e.Item.FindControl("BtnVerSubCat"), Button)
ddlSubCat.DataTextField = "NombreSubCategoria"
ddlSubCat.DataValueField = "idSubCategoria"
ddlSubCat.DataSource = subcat
ddlSubCat.DataBind()
ddlSubCat.AutoPostBack = True
BTNVER.PostBackUrl = "SubCat.aspx?idSubCategoria=" & ddlSubCat.SelectedValue.ToString
What I am trying to achieve is that IF ddlSubCat has its value changed (cause some user selected another SubCat to see) the POSTBACK get working fine.
What it does (up to here) is getting the FIRST value of DDLSubCat (first index of DDL)
I need to SOMEHOW "refresh" (but can't) de DATABOUND or whatever for the Button Works fine.
Tried everything, did ONCLICK (but the ddlSubCat does not appear in codebehing since its in DL)
Tried to EVAL in Design view... BUT CAN'T!
SOLVED !
Thanks to som other answer from Tim Schmelter, I realice that I could be handled by Codebehind
in the BTN click event like this:
Protected Sub BtnVerSubCat_Click(sender As Object, e As EventArgs)
Dim container = DirectCast(DirectCast(sender, Control).NamingContainer, DataListItem)
Dim idSubCat = DirectCast(container.FindControl("ddlSubCat"), DropDownList)
Dim Xid As String = idSubCat.SelectedValue
Response.Redirect("SubCat.aspx?idSubCatgoria=" & Xid)
End Sub
Thanks anyway

Adding all entries to combobox in linq to sql

All i want to do is add each entry of my specific database table to a combo box on form load. This is what i have so far.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
dc = New DataworldDataContext.DataworldDataContext
Dim countStates = (From z In dc.tblstates Select z).Count
Dim listStates = (From z In dc.tblstates Select z).ToList
For i = 0 To countStates - 1
cmbDealerState.Items.Add(listStates)
i = i + 1
Next
End Sub
Im getting the error "Items collection cannot be modified when the DataSource property is set." How can i fix this
For your 2nd problem, the combo box will have 2 properties called DisplayMember and ValueMember. The display one is the text you wish to display and the value the (usually numeric) value you return when an item is selected. Simply set them:
cmbDealerState.DisplayMember = "myDisplay"
cmbDealerState.ValueMember = "myValue"
I am presuming you are using WinForms here. If its WPF then its a bit more tricky.
Edit
dc = New DataworldDataContext.DataworldDataContext
Dim countStates = (From z In dc.tblstates Select z.StateCode).Count
Dim listStates = (From z In dc.tblstates Select z.StateCode).ToList
cmbDealerState.DisplayMember = "StateCode"
cmbDealerState.ValueMember = "StateCode"
This still gives me nothing
The problem you have is that you are trying to set DisplayMember and ValueMember to be a ToString representation of your list. In your database you will have columns that identify the data name, for example
StateID StateName
1 New York
2 Washington
The values you need to set therefore would be:
cmbDealerState.DisplayMember = "StateName"
cmbDealerState.ValueMember = "StateID"
Obviously these names are made up, but you get the idea.
When I tried this (in C#) I needed to access cmbDealerState.SelectedItem to get the values that had been selected.
Still Nothing
dc = New DataworldDataContext.DataworldDataContext
Dim listStates = (From z In dc.tblstates Select z.StateCode).ToList()
cmbDealerState.DataSource = listStates
This worked

VB.NET-Keep the dropdownlist status without puting the code in "Not IsPostBack"

I have 2 dropdownlists "countries" and "cities", with 2 datasources:
If a user chooses the first index in the ddl countries, he can see all the cities in the world in the ddl cities (uses datasource1).
If a user chooses a country, he can see the cities correspond to this country chosen (uses datasource2).
I put the code for changing datasource in the code behind using vb.net, in Page_Load, but after the user chose a city, and click the submit button, the dropdownlist cities can't keep the status, it goes to the first index of this ddl.
I tried to put this code in the If Not IdPostBack, but like this, it doesn't change datasource while it can keep status of the dropdownlist.
So does anyone have an idea about this problem?
I put the code here as reference:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim ddlCities As DropDownList
Dim ddlCountries As DropDownList
ddlCities = CType(PN_Search.FindControl("DropDownList_Cities"), System.Web.UI.WebControls.DropDownList)
ddlCountries = CType(PN_Search.FindControl("DropDownList_Countries"), System.Web.UI.WebControls.DropDownList)
Dim countrySelect As String
countrySelect = ddlCountries.SelectedValue
Dim rechercheCitiesNull As String = "SELECT * FROM Cities WHERE id_city=1"
Dim rechercheCitiesNotNull As String = "SELECT * FROM [View_Country_City] Where id_country=" & countrySelect
If countrySelect = "" Then
Me.RechercheCitiesDS.SelectCommand = rechercheCitiesNull
ddlCities.EnableViewState = True
ddlCountries.EnableViewState = True
ddlCities.DataBind()
ElseIf countrySelect <> "" Then
Me.RechercheCitiesDS.SelectCommand = rechercheCitiesNotNull
ddlCities.DataBind()
End If
End Sub
Thanks in advance!
Ziliu
EnableViewState and it should fix your issue. Keep your binding withing a not page.ispostback.