当" SelectedItem"时,如何更新我的第二个组合框?改变了

时间:2015-10-05 11:31:52

标签: c# arrays combobox datatable dataset

我在一个表格中有两个组合框,第一个组合框显示每个"标记"卡车和第二个可以显示每个"标记"的模型。例如,如果标记是沃尔沃,则第二组合框可以显示" FH,FH6 ..."否则如果标记是" MAN"第二个组合框可以显示" TGX,TGX 38" ......

我每次尝试做一个循环来重新组装我的第二个组合框,以便更换第一个组合框。

  while (comboBox1.SelectedIndex == -1)
            {
                comboBox2.Items.Clear();
                if (comboBox1.SelectedIndex == 1)
                {


                    comboBox2.DisplayMember = "Modelo";
                    comboBox2.DataSource = d.Tables["Modelos-MAN"].DefaultView;


                }
                if (comboBox1.SelectedIndex == 2)
                {

                    comboBox2.DisplayMember = "ModeloV";
                    comboBox2.DataSource = d.Tables["Modelos-VOLVO"].DefaultView;
                    comboBox2.Refresh();
                }
            }

            if (comboBox1.SelectedIndex==1)
            {


                comboBox2.DisplayMember = "Modelo";
                comboBox2.DataSource = d.Tables["Modelos-MAN"].DefaultView;


            }
            if(comboBox1.SelectedIndex == 2)
            {

                comboBox2.DisplayMember = "ModeloV";
                comboBox2.DataSource = d.Tables["Modelos-VOLVO"].DefaultView;
                comboBox2.Refresh();
            }

发生如果我选择" MAN",它会显示" MAN"模型,然后如果我改变,并选择"沃尔沃",第二个组合框显示我" System.Data.DataRowView"。

发生了什么?我怎么解决这个问题?

注意:我也是一种生成卡车"标记"和"模特":

  public DataSet LoadMarkAndModel()
        {


            var marcas = new string[6];
            marcas[0] = "Marca ...";
            marcas[1] = "MAN";
            marcas[2] = "Volvo";//Linha 2 coluna 2
            marcas[3] = "DAF";
            marcas[4] = "Renault";//Linha 4 coluna 2
            marcas[5] = "Mercedes-Benz";
            var modelosMAN = new string[7];
            var modelosVolvo = new string[5];

            modelosMAN[0] = "TGX";
            modelosMAN[1] = "TGX D38";
            modelosMAN[2] = "TGX EfficientLine 2";
            modelosMAN[3] = "TGS";
            modelosMAN[4] = "TGA";
            modelosMAN[5] = "TGA-WW";
            modelosMAN[6] = "TGM";


            modelosVolvo[0] = "FH16";
            modelosVolvo[1] = "FH";
            modelosVolvo[2] = "FM";
            modelosVolvo[3] = "FE";
            modelosVolvo[4] = "FL";


            DataSet data = new DataSet("DataSetOfTrucks");
            DataTable table = new DataTable("Marcas");
            DataColumn colunaID = new DataColumn("ID", typeof(int));
            DataColumn colunaMarca = new DataColumn("Marca", typeof(string));
            //table.Columns.Add("Description", typeof(string));
            table.Columns.Add(colunaID);
            table.Columns.Add(colunaMarca);



            for (int i = 0; i < marcas.Length; i++)
            {

                DataRow manyMark = table.NewRow();
                manyMark["ID"] = i + 1;
                manyMark["Marca"] = marcas[i];
                table.Rows.Add(manyMark);


            }
            data.Tables.Add(table);


            DataTable modelsTable = new DataTable("Modelos-MAN");
            DataColumn modelsIDParent = new DataColumn("IDP", typeof(int));
            DataColumn modelo = new DataColumn("Modelo", typeof(string));
            modelsTable.Columns.Add(modelsIDParent);
            modelsTable.Columns.Add(modelo);

            int m = 0;


            while (m<modelosMAN.Length)
            {
                DataRow manyModelsMAN = modelsTable.NewRow();
                manyModelsMAN["IDP"] = 1;
                manyModelsMAN["Modelo"] = modelosMAN[m];
                modelsTable.Rows.Add(manyModelsMAN);
                m++;
                //if (m > modelosMAN.Length)
                //    break;

            }
            data.Tables.Add(modelsTable);



            ////vOLVO
            DataTable modelsTableVOLVO = new DataTable("Modelos-VOLVO");
            DataColumn modelsIDParentVOLVO = new DataColumn("IDPVolvo", typeof(int));
            DataColumn modeloVolvo = new DataColumn("ModeloV", typeof(string));

            modelsTableVOLVO.Columns.Add(modelsIDParentVOLVO);
            modelsTableVOLVO.Columns.Add(modeloVolvo);

            int count = 0;

            while (count < modelosVolvo.Length)
            {
                DataRow manyModelsVolvo = modelsTableVOLVO.NewRow();
                manyModelsVolvo["IDPVolvo"] = 2;
                manyModelsVolvo["ModeloV"] = modelosVolvo[count];
                modelsTableVOLVO.Rows.Add(manyModelsVolvo);
                count++;


            }
            data.Tables.Add(modelsTableVOLVO);


            return data;
        }

1 个答案:

答案 0 :(得分:0)

comboBox1_SelectedIndexChanged()
{
 comboBox2.DataSource =null;

 if (comboBox1.SelectedIndex==1)
            {
                comboBox2.DataSource = d.Tables["Modelos-MAN"];
                comboBox2.ValueMember="Modelo";
                comboBox2.DisplayMember = "Modelo";
            }
 if(comboBox1.SelectedIndex == 2)
            {
                comboBox2.DataSource = d.Tables["Modelos-VOLVO"];
                comboBox2.DisplayMember = "ModeloV";
                comboBox2.ValueMember = "ModeloV";

            }
}