Sam
Sam

Reputation: 766

Error: DropdownList Returning 'System.Data.DataRowView' as Text Value of the Item Selected

I have a Dropdown list in which I have all the banks names. I want to display the credit limit in a Data Grid View. But the Dropdown list is returning 'System.Data.DataRowView' as value of Selected Item.

Here is the code:

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);
    }
}

Code to Add Data in DropdownList

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

Upvotes: 2

Views: 6452

Answers (2)

Habib
Habib

Reputation: 223207

You need to specify

For WinForm You need to specify the DataSource after defining the properties like:

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

For ASP.Net

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

Upvotes: 6

JMan
JMan

Reputation: 2629

You have 2 options.

  • Override the .ToString() method of your object, displaying what you need
  • Use the DisplayMemberPath(or equivalent) property of your dropdown list, displayiong the required property

Upvotes: 0

Related Questions