从数据库动态填充组合框

发布于 2024-09-27 17:19:12 字数 1334 浏览 1 评论 0原文

我正在为我的大学开发一个项目,我需要将数据库中的数据绑定到组合框中。我需要将卷号/注册号存储在组合框的“值”字段中,并将学生姓名存储在组合框的“文本”属性中。

我的代码是:

#region 填充组合框 //填充组合框。 公共静态无效FillCombo(ComboBox _cb,字符串_sSQL,字符串_sTable) { OleDbDataAdapter _oledbDA = 新 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 = "select rollno, Studentname from Student_data"

我尝试过的其他代码是:

region Fill Combo Box

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

I am working on a project for my college where I need to bind data from database into the combobox. I need to store the roll no / enrollment no in the "value" field of combobox and name of the student in the "text" property of the combobox.

My code is :

#region Fill Combo Box
//Fill Combo Box.
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

here::

_sSQL = "select rollno, studentname from student_data"

Other code i tried was :

region Fill Combo Box

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

but the problem is, nothing is been loaded in the combo box.... when i run the application, no error comes, but nothing is loaded in the combobox...

Please help... its SOS...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

音栖息无 2024-10-04 17:19:12

我更喜欢使用从数据库检索的数据手动填充组合框。为此,我编写了一个类 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")));
}

I prefer to manually populate my comboboxes with data retrieved from the database. For this purpose, I wrote a class, MaskedValue which I use every time.
Here's the class (converted from 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);
    }
}

To populate a combo box, I write code like below

foreach (DataRow dr in _DataTable.Rows)
{
    _ComboBox.Items.Add(New MaskedValue(dr("Name").ToString(), dr("ID")));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文