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

本文关键字:默认值 Data DataRowView 文本 System | 更新日期: 2023-09-27 18:11:44

请有人帮助我,为什么我得到"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();
        }

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

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

SelectedValue Casting Issue

如果想要获取值,请更改ComboBox。

选择值类型为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;

Just swap

cbxctry.DataSource = dt;
cbxctry.DisplayMember = "CountryName";
cbxctry.ValueMember = "CountryCode";
这样的

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