从动态填充数据库中的Combobox

时间:2010-10-19 08:22:16

标签: c# database data-binding dynamic combobox

我正在为我的大学开展一个项目,我需要将数据库中的数据绑定到组合框中。我需要将组合框的“值”字段中的roll no / enrollment no和组合框的“text”属性中的学生姓名存储起来。

我的代码是:

#region填充组合框         //填充组合框         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 =“选择来自student_data的rollno,studentname”

我试过的其他代码是:

区域填写组合框

    //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

但问题是,组合框中没有加载任何内容....当我运行应用程序时,没有错误,但组合框中没有加载...

请帮助......它的SOS ......

1 个答案:

答案 0 :(得分: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")));
}