简体   繁体   中英

the combobox show always the first value when i open it from another form not the value from datagrid

I have a form that contains two combobox (cmbSection,cmbGrade) and two textbox(txtName,txtSectionSize) i want to get text from combobox and txtSectionSize and put it in txtName so my code lock like this

 public partial class FRM_Item : Form
 {
    //public string State = "Add";
    BL.CLS_Item prd = new BL.CLS_Item();

    public FRM_Item()
    {
        InitializeComponent();

        cmbSection.DataSource = prd.Get_All_Items();
        cmbSection.DisplayMember = "Name_SectionType";
        cmbSection.ValueMember = "ID_SectionType";

        cmbGrade.DataSource = prd.Get_All_Grade();
        cmbGrade.DisplayMember = "Name_Grade";
        cmbGrade.ValueMember = "ID_Grade";
    }
private void cmbSection_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.txtName.Text = cmbSection.Text + txtSectionSize.Text + "-" + cmbGrade.Text;
    }

    private void cmbGrade_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.txtName.Text = cmbSection.Text + txtSectionSize.Text + "-" + cmbGrade.Text;

    }

    private void txtSectionSize_TextChanged(object sender, EventArgs e)
    {
        this.txtName.Text = cmbSection.Text + txtSectionSize.Text + "-" + cmbGrade.Text;
    }

when i open the form i get System.Data.DataRowView in txtName but when i pick up any text from combobox i get the right value in textbox i solve this problem by moving this code to form load

        private void FRM_Item_Load(object sender, EventArgs e)
    {
        cmbSection.DataSource = prd.Get_All_Items();
        cmbSection.DisplayMember = "Name_SectionType";
        cmbSection.ValueMember = "ID_SectionType";

        cmbGrade.DataSource = prd.Get_All_Grade();
        cmbGrade.DisplayMember = "Name_Grade";
        cmbGrade.ValueMember = "ID_Grade";
    }

the problem that i have now when i open this form from button in another form the combobox always shwo the first value not the value from datagrid

private void btnEdit_Click(object sender, EventArgs e)
    {
        FRM_Item frm = new FRM_Item();
        frm.txtName.Text = this.dataGridView1.CurrentRow.Cells[1].Value.ToString();
        frm.cmbSection.Text = this.dataGridView1.CurrentRow.Cells[2].Value.ToString();
        frm.txtSectionSize.Text = this.dataGridView1.CurrentRow.Cells[3].Value.ToString();
        frm.cmbGrade.Text = this.dataGridView1.CurrentRow.Cells[4].Value.ToString();

        frm.ShowDialog();
    }

how i can solve this problem

Populate the comboboxes before you assign them. Right now, its backwards.

I recommend moving this code to the child form constructor, or to a method that can be called before you try to assign properties in the parent form.

    cmbSection.DataSource = prd.Get_All_Items();
    cmbSection.DisplayMember = "Name_SectionType";
    cmbSection.ValueMember = "ID_SectionType";

    cmbGrade.DataSource = prd.Get_All_Grade();
    cmbGrade.DisplayMember = "Name_Grade";
    cmbGrade.ValueMember = "ID_Grade";

As far as the events if you put the code in the constructor, you could have a boolean property to not run the event code if you are not finished with the constructor OR have not have the events in the designer and do it explicitly in the constructor AFTER you populate the comboboxes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM