加载数据时未触发Datagridview组合框和文本框事件

时间:2018-11-16 05:58:25

标签: c# winforms events datagridview

我正在创建一个应用程序,其中datagridview具有一个组合框和多个文本框

在datagridview中,当我手动选择datagridviewcombobox值时,将填充文本框值

但是datagridview由绑定源填充,并且datagridviewcombobox的值被选中,但文本框的值为空

以下示例

用户选择的组合框值,“帐户名称”文本框已填充 enter image description here

由bindingsource(动态)选择的组合框值,“帐户名”文本框为空

enter image description here

下面是我的代码

int journalID = 0;
public Add(int id = 0)
{
     BindControls();
     journalID = id;
}


  private async void BindControls()
  {
     try
     {               
            if (journalID > 0)
            {
                List<JournalAccountViewModel> list = await new JournalModel().GetDetailById(journalID);
                for (int i = 0; i <= list.Count - 1; i++)
                {
                    bindingSource1.Add(list[i]);
                }
            }
            else
            {
                bindingSource1.Add(new JournalAccountViewModel());
                bindingSource1.List.Clear();
            }
            dgvJournal.AutoGenerateColumns = false;
            dgvJournal.AllowUserToAddRows = true;
            dgvJournal.AutoSize = true;
            dgvJournal.DataSource = bindingSource1;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Rule", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }


   private void dgvJournal_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
        try
        {
            if (dgvJournal.CurrentCell.ColumnIndex == dgvJournal.Columns[colCredit.Name].Index) //Desired Column
            {
                TextBox tb = e.Control as TextBox;
                if (tb != null)
                {
                    tb.KeyPress += new KeyPressEventHandler(txtNumeri_KeyPress);
                }
            }

            if (dgvJournal.CurrentCell.ColumnIndex == dgvJournal.Columns[ColDebit.Name].Index) //Desired Column
            {
                TextBox tb = e.Control as TextBox;
                if (tb != null)
                {
                    tb.KeyPress += new KeyPressEventHandler(txtNumeri_KeyPress);
                }
            }


            ComboBox combo = e.Control as ComboBox;
            if (combo != null)
            {
                combo.SelectedIndexChanged -= new EventHandler(ComboBox_SelectedIndexChanged);
                combo.SelectedIndexChanged += new EventHandler(ComboBox_SelectedIndexChanged);
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Rule", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

    }


   private void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            if (((ComboBox)sender).SelectedItem != null)
            {
                AccountViewModel account = (AccountViewModel)((ComboBox)sender).SelectedItem;
                var currentcell = dgvJournal.CurrentCellAddress;
                DataGridViewTextBoxCell cel = (DataGridViewTextBoxCell)dgvJournal.Rows[currentcell.Y].Cells[ColAccountName.Name];
                cel.Value = account.Name;
            }
            else
            {
                ((ComboBox)sender).SelectedIndex = 0;
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Rule", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

  private void txtNumeri_KeyPress(object sender, KeyPressEventArgs e)
    {
        try
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '.')
            {
                e.Handled = true;
            }

            // only allow one decimal point
            if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1)
            {
                e.Handled = true;
            }

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Rule", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

请建议我以编程方式设置组合框值时如何填充文本框值。

2 个答案:

答案 0 :(得分:0)

我将为该DataGridView创建一个 CellValueChanged 事件,并从您的 ComboBox_SelectedIndexChanged 事件复制功能。这样就无需在 dgvJournal_EditingControlShowing 中设置这些事件,也可以解决您的原始问题,因为只要 AccountNumber 字段中的值更改,该事件就会触发。

    private void dgvJournal_CellValueChanged(object sender, DataGridViewCellEventArgs e)
    {
        int accountNumberColIndex = dgvJournal.Columns["AccountNumber"].Index;

        // Checking that this value is from the account number column
        if (e.ColumnIndex == accountNumberColIndex)
        {
            AccountViewModel account = (AccountViewModel) dgvJournal.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
            int accountNameColIndex = dgvJournal.Columns["AccountName"].Index;

            DataGridViewTextBoxCell accountNameCell = (DataGridViewTextBoxCell) dgvJournal.Rows[e.RowIndex].Cells[accountNameColIndex];
            accountNameCell.Value = account.Name;
        }
    }

答案 1 :(得分:0)

    private void dgvJournal_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
    {
        if (e.ListChangedType == ListChangedType.Reset)
        {
            var a = (DataGridView)sender;
            var b = a.DataSource;
            var c = (BindingSource)b;
            var d = c.List;
            int sumDebit = 0, sumCredit = 0;
            int cnt = 0;
            foreach (JournalAccountViewModel account in d)
            {
                dgvJournal.Rows[cnt].Cells[ColAccountName.Name].Value = accountList.Where(acc => acc.ID == account.AccountID).FirstOrDefault().Name;
                enableCell(((DataGridViewCell)dgvJournal.Rows[cnt].Cells[ColDebit.Name]), (account.Debit > 0));
                enableCell(((DataGridViewCell)dgvJournal.Rows[cnt].Cells[colCredit.Name]), (account.Credit > 0));
                cnt++;
            }
        }
    }