1

i bind a combobox with this code

   List<KeyValuePair<string, string>> data = new List<KeyValuePair<string, string>>();

        // Add data to the List
        data.Add(new KeyValuePair<string, string>( "01","Bag"));
        data.Add(new KeyValuePair<string, string>("02", "Box"));
        data.Add(new KeyValuePair<string, string>("03", "Carton/Piece"));
        data.Add(new KeyValuePair<string, string>("04", "Crate"));
        data.Add(new KeyValuePair<string, string>("05", "Drum"));
        data.Add(new KeyValuePair<string, string>("06","Pallet/Skid" ));
        data.Add(new KeyValuePair<string, string>("07","Roll"));
        data.Add(new KeyValuePair<string, string>("08", "Tube"));
        // Clear the combobox
        ddlPackageCode.DataSource = null;
        ddlPackageCode.Items.Clear();

        // Bind the combobox
        ddlPackageCode.DataSource = new BindingSource(data, null);
        ddlPackageCode.DisplayMember = "Value";
        ddlPackageCode.ValueMember = "Key";

I can find the selected value from the combobox from this code

 KeyValuePair<string, string> selectedPair1 = (KeyValuePair<string, string>)ddlServiceCode.SelectedItem;

        ShipmentConfirmReq.Shipment.Service.Code = selectedPair1.Key;
        ShipmentConfirmReq.Shipment.Service.Description = selectedPair1.Value;

But how can i set the selected value in combobox . I try this but i get error NullReferenceException . Below is the code i set the selected value in combobox and getting error

 KeyValuePair<string, string> selectedPair1 = (KeyValuePair<string, string>)cmbfield2.SelectedItem;

            cmbfield2.SelectedValue = _commonEntity.Field2Value;
Piyush Parashar
  • 866
  • 8
  • 20
A.Goutam
  • 3,422
  • 9
  • 42
  • 90

0 Answers0