我的组合框如何显示名称列表,但在选择名称时使用与该名称关联的唯一 ID?

我的应用程序使用一个组合框,其中填充了从 API 返回的列表名称。API 还返回与每个名称关联的 ID。我怎样才能让组合框显示名称,但是当用户选择一个名称时,与该名称关联的 ID 存储在一个变量中,或者用于执行某些操作?我可以让组合框填写名称,但名称没有任何实际用途,仅用于显示目的。我不知道如何让组合框显示名称,而是使用 ID 作为其值。


这是 API 返回的 json。


[

{

    "BookId": "13a7dc2b-80da-4407-bd4d-806d7f4adc3b",

    "BookName": "Reckon API",

    "Bookstatus": 3,

    "Country": "AU"

},

{

    "BookId": "32c03594-1ecb-4f97-8453-5b28a03d26d9",

    "BookName": "Payroll Book",

    "Bookstatus": 3,

    "Country": "AU"

},

{

    "BookId": "51d16696-b98a-4b3b-ac67-f36559cff70b",

    "BookName": "Accounting Book",

    "Bookstatus": 3,

    "Country": "AU"

}

]

这是我用来用名称填充组合框的代码:


using (HttpClient httpClient = new HttpClient())

        {

            httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);

            var response = httpClient.GetAsync("https://api.reckon.com/R1/cashbooks").Result;

            string responseText = response.Content.ReadAsStringAsync().Result;

            JArray parsedArray = JArray.Parse(responseText);

            foreach (JObject parsedObject in parsedArray.Children<JObject>())

            {

                foreach (var parsedProperty in parsedObject.Properties())

                {

                    if (parsedProperty.Name == "BookName")

                    {

                        comboBox2.Items.Add(parsedProperty.Value);

                    }

                }

            }

        }

我有一个 MYSQL 数据库,我存储了我的名称和 ID 列表,我认为我可以使用 databsae 作为数据源,显示成员作为名称和值成员作为 id,但是当我运行应用程序时,组合框没有' t populate 但我不知道为什么。我如何完成这项任务对我来说真的无关紧要,我只需要找到一种方法。


茅侃侃
浏览 165回答 2
2回答

ITMISS

你可以试试这个:int selectedId;bool res = int.TryParse(comboBox.SelectedValue.ToString(), out selectedId);if(res)// do something here

摇曳的蔷薇

只需使用comboBox2.SelectedValue编辑:对于您的 API,您可以像这样编辑代码:&nbsp; &nbsp; using (HttpClient httpClient = new HttpClient())&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);&nbsp; &nbsp; &nbsp; &nbsp; var response = httpClient.GetAsync("https://api.reckon.com/R1/cashbooks").Result;&nbsp; &nbsp; &nbsp; &nbsp; string responseText = response.Content.ReadAsStringAsync().Result;`enter code here`&nbsp; &nbsp; &nbsp; &nbsp; JArray parsedArray = JArray.Parse(responseText);&nbsp; &nbsp; &nbsp; &nbsp; DataTable dtSource = new DataTable();dtSource.Columns.AddRange(new DataColumn[]{new DataColumn("BookID"),new DataColumn("BookName")});&nbsp; &nbsp; &nbsp; &nbsp; foreach (JObject parsedObject in parsedArray.Children<JObject>())&nbsp; &nbsp; &nbsp; &nbsp; {string BookID,BookName;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach (var parsedProperty in parsedObject.Properties())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {if (parsedProperty.Name == "BookId")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {BookID= parsedProperty.Value;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }else if (parsedProperty.Name == "BookName")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {BookName= parsedProperty.Value;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }dtSource.Rows.Add(BookID,BookName);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }comboBox2.DataSource = dtSource;comboBox2.DisplayMember = "BookName";comboBox2.ValueMember = "BookID";
打开App,查看更多内容
随时随地看视频慕课网APP