2010-10-19 87 views
0

我正在爲我的大學開發一個項目,我需要將數據庫中的數據綁定到組合框中。我需要在組合框的「value」字段中存儲roll no/enrollment no,並在combobox的「text」屬性中存儲學生的名字。從數據庫動態填充Combobox

我的代碼是:

#區域填充組合框 //填充組合框。 public static void FillCombo(ComboBox _cb,string _sSQL,string _sTable) OleDbDataAdapter _oledbDA = new OleDbDataAdapter(_sSQL,_olbedbCN); DataTable _dtSource = new DataTable(); _oledbDA.Fill(_dtSource); _cb.DataSource = _dtSource; _cb.ValueMember = _dtSource.Columns [0] .ColumnName; _cb.DisplayMember = _dtSource.Columns [1] .ColumnName; }

endregion

位置::

_sSQL = 「選擇rollno,studentname從STUDENT_DATA」

其他代碼我想是:

區域填充組合框

//Fill Combo Box. 
    public static void FillCombo(ComboBox _cb, string _sSQL, string _sTable) 
    { 


     OleDbDataAdapter _oledbDA = new OleDbDataAdapter("select rollno, studentname from student_data", _olbedbCN); 
     DataTable _dtSource = new DataTable(); 
     _oledbDA.Fill(_dtSource); 
     _cb.DataSource=ds.Tables["StudentData"]; 
     _cb.DisplayMember="Studentname"; 
     _cb.ValueMember="rollno"; 
     _cb.SelectedIndex=0;  } 

}

endregion

但問題是,沒有什麼是在組合框中加載....當我運行應用程序,沒有錯誤出現,但沒有在ComboBox中加載中...

請幫助...它的SOS ......

+0

你應該寫cb.DataSource = _dtSource.Tables [ 「STUDENT_DATA」 ]只要你的表名具有_ – 2010-10-19 08:30:58

+0

嘗試調用組合框上的DataBind()方法 – jimplode 2010-10-19 08:32:25

回答

0

我更喜歡手動填充我的組合框從數據庫中檢索數據。爲此,我寫了一堂課,每次都使用MaskedValue
這裏的(從VB.NET轉換)

using System; 
using System.Diagnostics; 
using System.ComponentModel; 

/// <summary>Represents a value masking an underlying value.</summary> 
[DebuggerDisplay("{ToString()}"), DebuggerStepThrough()] 
public class MaskedValue : IComparable<MaskedValue>, IComparer<MaskedValue> 
{ 

    private string _value; 
    public string Value { 
     get { return _value; } 
     set { _value = value; } 
    } 
    private object _underlyingValue; 
    public object UnderlyingValue { 
     get { return _underlyingValue; } 
     set { _underlyingValue = value; } 
    } 

    /// <summary>Creates a new instance of the MaskedValue class.</summary> 
    public MaskedValue() 
    { 
     Value = ""; 
     UnderlyingValue = null; 
    } 

    /// <summary>Creates a new instance of the MaskedValue class.</summary> 
    /// <param name="value">Value to be assigned to the MaskedValue.</param> 
    public MaskedValue(string value) 
    { 
     this.Value = value; 
    } 

    /// <summary>Creates a new instance of the MaskedValue class.</summary> 
    /// <param name="value">Value to be assigned to the MaskedValue.</param> 
    /// <param name="underlyingValue">Underlying value of the MaskedValue.</param> 
    public MaskedValue(string value, object underlyingValue) 
    { 
     this.Value = value; 
     this.UnderlyingValue = underlyingValue; 
    } 

    /// <summary>Gets a value that represents this MaskedValue.</summary> 
    public override string ToString() 
    { 
     return Value; 
    } 

    /// <summary>Compares two instances of MaskedValue.</summary> 
    /// <param name="x" >First MaskedValue to be compared.</param> 
    /// <param name="y">Second MaskedValue to be compared.</param> 
    /// <returns> 
    /// A 32-bit signed integer indicating the lexical relationship between the two comparands. 
    /// </returns> 
    [EditorBrowsable(EditorBrowsableState.Advanced)] 
    public int Compare(MaskedValue x, MaskedValue y) 
    { 
     return x.CompareTo(y); 
    } 

    /// <summary>Compares this MaskedValue to the other.</summary> 
    /// <param name="other">MaskedValue to compare this MaskedValue with.</param> 
    /// <returns> 
    /// A 32-bit signed integer indicating the lexical relationship between the two comparands. 
    /// </returns> 
    [EditorBrowsable(EditorBrowsableState.Advanced)] 
    public int CompareTo(MaskedValue other) 
    { 
     return this.Value.CompareTo(other.Value); 
    } 
} 

要填充組合框類,我寫的代碼像下面

foreach (DataRow dr in _DataTable.Rows) 
{ 
    _ComboBox.Items.Add(New MaskedValue(dr("Name").ToString(), dr("ID"))); 
}