English 中文(简体)
如何填补ComboBox从第二次ComboBox公司进行的甄选变化
原标题:How to fill a ComboBox on the selection change from second ComboBox
  • 时间:2012-01-15 13:38:35
  •  标签:
  • vb.net

我有两只 com子,一个是Combo,第二个是国家Combo。

我想的是,当点击国 com和选择一个国家时,该国的国家就应当居住在国库博。

我已经尝试了许多事情,但没有任何工作。 下面是我关于我国Combo是如何填补的法典。

query="select CountryName from CountryMaster"

if dr.hasRows()
{
  while dr.read()
  {
    countrycombo.items.add(dr(0)
  }
}
最佳回答

请检查以下编码,并在提出问题之前请做一些家务劳动。

    Private Sub countryCombo_SelectedValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedValueChanged
    Dim query As String
    query = "select StateName from StateMaster where CountryId= " & countryCombo.SelectedValue & " "

    if dr.hasRows()
    {
         while dr.read()
          {
              Statecombo.items.add(dr(0))
          }
    }
    End Sub

This will populate your Statecombo on the selection change of countryCombo.

问题回答

该法典:

    Dim dt As New DataTable
    Dim cm1, cm2 As New DataColumn
    cm1.ColumnName = "CountryName"
    cm1.DataType = GetType(String)
    cm2.ColumnName = "CountryID"
    cm2.DataType = GetType(String)  or integer
    dt.Columns.Add(cm1)
    dt.Columns.Add(cm2)
     contry = here select your data

    Dim dr As DataRow
    dr = dt.NewRow
    dr(0) = "Select One"
    dr(1) = "-1"
    dt.Rows.Add(dr)
    For Each item In contry
        dr = dt.NewRow
        dr(0) = item.CountryName
        dr(1) = item.CountryID
        dt.Rows.Add(dr)
    Next

    Drp.DataSource = dt
    Drp.DataTextField = "CountryName"
    Drp.DataValueField = "CountryID"
    Drp.DataBind()

with this code you can fill your dropdown. you can write sub like this and put it on selected change of this dropdown. Have good time!
add this part and make sure you have post back on dropdown list if its web form.

Protected Sub Drpcountry_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Drpcountry.SelectedIndexChanged
    Dim db as new linqdatacontext 
    Dim dt As New DataTable
    Dim cm1, cm2 As New DataColumn
    cm1.ColumnName = "stateName"
    cm1.DataType = GetType(String)
    cm2.ColumnName = "stateID"
    cm2.DataType = GetType(String)  or integer
    dt.Columns.Add(cm1)
    dt.Columns.Add(cm2)
    Dim stateslist = From i in db.tb_states where i.countryid = drpcountry.selectedvalue select i

    Dim dr As DataRow
    dr = dt.NewRow
    dr(0) = "Select One"
    dr(1) = "-1"
    dt.Rows.Add(dr)
    For Each item In stateslist 
        dr = dt.NewRow
        dr(0) = item.stateName
        dr(1) = item.stateID
        dt.Rows.Add(dr)
    Next

    Drpstate.DataSource = dt
    Drpstate.DataTextField = "stateName"
    Drpstate.DataValueField = "stateID"
    Drpstate.DataBind()
End Sub

您所展示的法典是(不完整的)填补您的法典。 CountryCombo : 也请赞同关于新的国家甄选填补国库博的法典,因为那部分是造成你问题的。

Anyway you should avoid manipulating directly ComBoBox items, 以及instead you Should Bind both Combo To ObservableCollection, say to

 Public Property CountryCollection as New ObservableCollection(Of Country) 

以及

 Public Property StateCollection As New ObservableCollection(Of State). 

And for both Country 以及State Object, overload ToString() sub to have Country/State name displayed in the Combos.

Now fill the CountryCollection with your countries, 以及on a SelectionChanged of the CountryCombo, retrieve the list of state ( = state having CountryCombo.SelectedItem.CountryID as CountryID) then populate your StateCollection with this query result (Clear() then add()).

逐步测试:检查贵国/国家问询与“突破点”/“gger”交回良好结果。 如果你对可观察的Collection有约束力,那么现在就应该成为唯一的问题。





相关问题
Is Shared ReadOnly lazyloaded?

I was wondering when I write Shared ReadOnly Variable As DataType = New DataType() Or alternatively Shared ReadOnly Variable As New DataType() Is it lazy loaded or as the instance initializes? ...

Entertaining a baby with VB.NET

I would like to write a little application in VB.NET that will detect a baby s cry. How would I get started with such an application?

Choose Enter Rather than Pressing Ok button

I have many fields in the page and the last field is a dropdown with list of values. When I select an item in a dropdown and press Enter, it doesn t do the "Ok". Instead I have to manually click on Ok ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

Set Select command in code

On button Click I want to Set the Select command of a Gridview. I do this and then databind the grid but it doesn t work. What am i doing wrong? protected void bttnView_Click(object sender, ...

Hover tooltip on specific words in rich text box?

I m trying to create something like a tooltip suddenly hoovering over the mouse pointer when specific words in the richt text box is hovered over. How can this be done?

热门标签