文本框中的System.Data.DataRowView,而不是默认值

时间:2013-05-01 13:48:00

标签: c#

请有人帮我解释为什么我在文本框中输入“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();

        }

2 个答案:

答案 0 :(得分:0)

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

SelectedValue Casting Issue

  

如果要获取值,请将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;

答案 1 :(得分:0)

只需交换

cbxctry.DataSource = dt;

cbxctry.DisplayMember = "CountryName";
cbxctry.ValueMember = "CountryCode";
像这样

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

cbxctry.DataSource = dt;

这应该有帮助