0

this is my Code

    private void comboBoxSelectEmployee_SelectionChangeCommitted(object sender, System.EventArgs e)
    {
        this.EmployeeModel.EmployeeId = comboBoxSelectEmployee.SelectedValue.ToString();
        var GetEmployee = EmployeeInfoCollection.GetWhereEmployeeId(this.EmployeeModel.EmployeeId);
        List<EmployeeModel> EmployeeModel = (List<EmployeeModel>)GetEmployee;
        this.CbxEmployeeDataTableValue = ConvertExtension.ToDataTable(GetEmployee);


        if (this.CbxEmployeeDataTableValue != null)
        {
            textBoxLicensePlate1.Text = this.CbxEmployeeDataTableValue.Rows[0]["LicensePlate1"].ToString();
            textBoxLicensePlate2.Text = this.CbxEmployeeDataTableValue.Rows[0]["LicensePlate2"].ToString();
            textBoxFirstname.Text = this.CbxEmployeeDataTableValue.Rows[0]["Firstname"].ToString();
            textBoxLastname.Text = this.CbxEmployeeDataTableValue.Rows[0]["Lastname"].ToString();
            textBoxNickname.Text = this.CbxEmployeeDataTableValue.Rows[0]["Nickname"].ToString();
            textBoxPhoneNumber.Text = this.CbxEmployeeDataTableValue.Rows[0]["PhoneNumber"].ToString();
            textBoxBank.Text = this.CbxEmployeeDataTableValue.Rows[0]["Bank"].ToString();
            textBoxAccountNumber.Text = this.CbxEmployeeDataTableValue.Rows[0]["AccountNumber"].ToString();
            textBoxPromptpay.Text = this.CbxEmployeeDataTableValue.Rows[0]["Promptpay"].ToString();
        }
    }

Of course it can be used like this

            textBoxLicensePlate1.Text = this.CbxEmployeeDataTableValue.Rows[0]["LicensePlate1"].ToString();
            textBoxLicensePlate2.Text = this.CbxEmployeeDataTableValue.Rows[0]["LicensePlate2"].ToString();
            textBoxFirstname.Text = this.CbxEmployeeDataTableValue.Rows[0]["Firstname"].ToString();
            textBoxLastname.Text = this.CbxEmployeeDataTableValue.Rows[0]["Lastname"].ToString();
            textBoxNickname.Text = this.CbxEmployeeDataTableValue.Rows[0]["Nickname"].ToString();
            textBoxPhoneNumber.Text = this.CbxEmployeeDataTableValue.Rows[0]["PhoneNumber"].ToString();
            textBoxBank.Text = this.CbxEmployeeDataTableValue.Rows[0]["Bank"].ToString();
            textBoxAccountNumber.Text = this.CbxEmployeeDataTableValue.Rows[0]["AccountNumber"].ToString();
            textBoxPromptpay.Text = this.CbxEmployeeDataTableValue.Rows[0]["Promptpay"].ToString();

But I want it to be set to the class model and run it like this.

        textBoxLicensePlate1.Text = EmployeeModel.LicensePlate1;
        textBoxLicensePlate2.Text = EmployeeModel.LicensePlate2;
        textBoxFirstname.Text = EmployeeModel.Firstname;
        textBoxLastname.Text = EmployeeModel.Lastname;
        textBoxNickname.Text = EmployeeModel.Nickname;
        textBoxPhoneNumber.Text = EmployeeModel.PhoneNumber;
        textBoxBank.Text = EmployeeModel.Bank;
        textBoxAccountNumber.Text = EmployeeModel.AccountNumber;
        textBoxPromptpay.Text = EmployeeModel.Promptpay;

Can it be done?

More examples But this one I'm using in DataRow, not DataRow[], of course I want it to be like this.

                var row = ConvertExtension.GetItem<EmployeeModel>(EmployeeRowValue);
                comboBoxSelectEmployee.SelectedValue = row.EmployeeId;
                this.EmployeeModel.EmployeeId = comboBoxSelectEmployee.SelectedValue.ToString();

                textBoxLicensePlate1.Text = row.LicensePlate1;
                textBoxLicensePlate2.Text = row.LicensePlate2;
                textBoxFirstname.Text = row.Firstname;
                textBoxLastname.Text = row.Lastname;
                textBoxNickname.Text = row.Nickname;
                textBoxPhoneNumber.Text = row.PhoneNumber;
                textBoxBank.Text = (string)row.Bank;
                textBoxAccountNumber.Text = row.AccountNumber;
                textBoxPromptpay.Text = row.Promptpay;
  • 1
    DataSet / DataTable is kinda an old technology, if you want to stick with it try using typed DataSets / DataTables. However I suggest to use Entity Framework or Dapper. (google it) – gsharp Oct 30 '21 at 07:27
  • you can use also an auto mapper for more detail go through this link:-https://stackoverflow.com/questions/35414228/using-automapper-to-map-a-datatable-to-an-object-dto – Sunny Jangid Oct 30 '21 at 09:46
  • What are you binding to the `ComboBox`? I am guessing it is a `DataTable` and if this is the case, have you considered binding a `BindingList` to the combo box? With this approach, you should be able to do something like... `EmployeeModel selectedEmployee = (EmployeeModel)comboBoxSelectEmployee.SelectedItem;` – JohnG Oct 30 '21 at 23:55

0 Answers0