2

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

这是代码:

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
4

2 回答 2

6

您需要指定

对于 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();
于 2013-04-19T12:11:20.517 回答
0

您有 2 个选项。

  • 覆盖对象的 .ToString() 方法,显示您需要的内容
  • 使用下拉列表的 DisplayMemberPath(或等效)属性,显示所需的属性
于 2013-04-19T12:10:21.613 回答