错误:DropdownList返回'System.Data.DataRowView'作为所选项的文本值

时间:2013-04-19 12:07:38

标签: c# winforms drop-down-menu datagridview

我有一个下拉列表,其中包含所有银行名称。我想在数据网格视图中显示信用额度。但Dropdown列表返回'System.Data.DataRowView'作为Selected Item的值。

以下是代码:

private void cbxBank_SelectedIndexChanged(object sender, EventArgs e)
{
    int i = cbxBank.SelectedIndex;
    string bank = cbxBank.Text;
    if (i != -1)
    {
        // this Function Returns the Table of CreditLimit According to Bank Name
        DataTable CreditLimit = AM.ReturnAvailableCreditLimit(bank);

        //this function just displays the table in Datat Grid View
        DataGridViewDisplayDetails(CreditLimit);
    }
}

在DropdownList中添加数据的代码

        Bank = DbRdRw.SqlDbRead("Select BankName from BankMaster", "BankMaster");
        cbxBank.DataSource = Bank;
        cbxBank.ValueMember = "BankName";
        cbxBank.DisplayMember = "BankName";
        //ends

2 个答案:

答案 0 :(得分:5)

您需要指定

对于WinForm您需要在定义以下属性后指定DataSource:

Bank = DbRdRw.SqlDbRead("Select BankName from BankMaster", "BankMaster");
cbxBank.ValueMember = "BankName";
cbxBank.DisplayMember = "BankName";
cbxBank.DataSource = Bank; //here

对于ASP.Net

cbxBank.DataSource = ....;//specify your datasource
cbxBank.DataValueField = "BankID";
cbxBank.DataTextField = "BankName";
cbxBank.DataBind();

答案 1 :(得分:0)

您有2个选项。

  • 覆盖对象的.ToString()方法,显示您需要的内容
  • 使用下拉列表的DisplayMemberPath(或等效)属性,显示所需的属性