简体   繁体   English

当我从其他表单中打开它时,组合框始终显示第一个值,而不是datagrid中的值

[英]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 我有一个包含两个组合框(cmbSection,cmbGrade)和两个文本框(txtName,txtSectionSize)的表单,我想从combobox和txtSectionSize中获取文本并将其放在txtName中,所以我的代码锁定像这样

 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 当我打开表单时,我在txtName中得到System.Data.DataRowView,但是当我从组合框中选取任何文本时,我在文本框中获得了正确的值,我通过将这段代码移到表单加载中解决了这个问题

        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 当我从另一个窗体的按钮中打开此窗体时出现的问题,组合框始终显示第一个值,而不是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. 至于将代码放入构造函数中的事件,如果您还没有完成构造函数,或者没有在设计器中拥有事件并在构造函数中明确地进行操作,则可以具有布尔属性以不运行事件代码填充组合框之后。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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