none
手動新增ComboBox的Items後,如果設定其個別ValueMember RRS feed

  • 問題

  •  

    各位前輩:

     

    VISTA

    VS2008

    C#

    Access 2003

     

    介面上有二個comboBox

    當comboBox1被SelectedIndexChanged時

    我會去取得資料庫內的值

    並把它們的Name1顯示在comboBox2內

     

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                comboBox2.Items.Clear();

                cmdString = "SELECT DISTINCT Parentid FROM DbTable WHERE (Level = '" + comboBox1.Text + "')";
                //連線並開啟資料庫
                ConnStringMethod();
                da = new OleDbDataAdapter(cmdString, conn);
                da.Fill(ds);

                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    Parentid = ds.Tables[0].RowsIdea[0].ToString();

                    cmdString = "SELECT Name1,Parentid FROM DbTable WHERE (Id = '" + Parentid + "')";
                    OleDbDataAdapter da1 = new OleDbDataAdapter(cmdString, conn);
                    DataSet ds1 = new DataSet();
                    da1.Fill(ds1);
                    conn.Close();


                    Name1= ds1.Tables[0].Rows[0][0].ToString();
                    comboBox2.Items.Add(Name1);   //將Name1加入comboBox2.Items內
                    comboBox2.ValueMember = ds.Tables[0].RowsIdea[0].ToString();   //取得Parentid

                   //這邊應該要怎麼寫才會將正確的ValueMember 對應到comboBox2的Items

                  //不然在equivalent()裡只會取得最後一個的ValueMember 
                }
            }

     

     

    private void equivalent()
            {
                cmdString = "SELECT Name1, Email FROM DbTable WHERE (Parentid = '" + comboBox2.ValueMember + "')";

                 //還是我cmdString 這邊的語法錯誤,請前輩指教,謝謝


                //連線並開啟資料庫
                ConnStringMethod();

                da = new OleDbDataAdapter(cmdString, conn);
                da.Fill(ds);
                conn.Close();

                dataGridView1.DataSource = ds.Tables[0];

                dataGridView1.Columns[0].ReadOnly = true;
                dataGridView1.Columns[1].ReadOnly = true;
            }

    2008年7月9日 下午 03:16

解答

  • HI,

     

    您可以查詢得到的內容填入到一個自己建立的DataTable, 再將DataTable交給第二個ComboBox控制項顯示, 就可以設定DisplayMember和ValueMember了

    建立DataTable的做法如下:

     

    private DataTable CreateDataTable()
    {

    DataTable myDataTable = new DataTable();

    DataColumn myDataColumn;

    myDataColumn = new DataColumn();
    myDataColumn.DataType = Type.GetType("System.String");
    myDataColumn.ColumnName = "id";
    myDataTable.Columns.Add(myDataColumn);

    myDataColumn = new DataColumn();
    myDataColumn.DataType = Type.GetType("System.String");
    myDataColumn.ColumnName = "username";
    myDataTable.Columns.Add(myDataColumn);

    myDataColumn = new DataColumn();
    myDataColumn.DataType = Type.GetType("System.String");
    myDataColumn.ColumnName = "firstname";
    myDataTable.Columns.Add(myDataColumn);

    myDataColumn = new DataColumn();
    myDataColumn.DataType = Type.GetType("System.String");
    myDataColumn.ColumnName = "lastname";
    myDataTable.Columns.Add(myDataColumn);

    return myDataTable;

    }

     

    tihs

     

    2008年7月10日 上午 03:16