-1

请有人帮我解释为什么我在我的文本框中得到“System.Data.DataRowView”而不是“44”,这是我期望看到的?如果从下拉列表中再次选择它,那么它会为我填充该字段。我试过阅读很多论坛。

再次谢谢你

private void E164Convertor_Load(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();


            DataColumn dc1 = new DataColumn("CountryName");
            DataColumn dc2 = new DataColumn("CountryCode");



            dt.Columns.Add(dc1);
            dt.Columns.Add(dc2);

            dt.Rows.Add("United Kingdom" ,"44");
            dt.Rows.Add("United States" , "1");

            cbxctry.DataSource = dt;

            cbxctry.DisplayMember = "CountryName";
            cbxctry.ValueMember = "CountryCode";

        }

        private void cbxctry_SelectedIndexChanged(object sender, EventArgs e)
        {

         txtctrycode.Text = cbxctry.SelectedValue.ToString();

        }
4

2 回答 2

0

这看起来可能是您的问题:

SelectedValue 铸造问题

如果要获取值,请将 ComboBox.SelectedValue 类型更改为 System.Data.DataRowView。

System.Data.DataRowView selectedObj = cmb_Companies.SelectedValue as System.Data.DataRowView;
string str_AccountNo = selectedObj != null? selectedObj["Account_Number"].ToString():String.Empty;
于 2013-05-01T13:54:50.973 回答
0

只换

cbxctry.DataSource = dt;

cbxctry.DisplayMember = "CountryName";
cbxctry.ValueMember = "CountryCode";

像这样

cbxctry.DisplayMember = "CountryName";
cbxctry.ValueMember = "CountryCode";

cbxctry.DataSource = dt;

这应该有帮助

于 2013-05-01T14:04:58.290 回答