如何从DataGridViewComboBox中选择项目时填充TextBox值

时间:2014-08-11 10:41:00

标签: c# .net ms-access-2007

当从DataGridView中选择产品时,我想在TextBox中显示产品余额。

选择产品并按下标签按钮后,产品余额应显示在文本框中。

所有数据都是从Microsoft Access数据库加载的。我怎样才能做到这一点。

private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
    e.Control.KeyPress -= new KeyPressEventHandler(dataGridView1_KeyPress);
    if (dataGridView1.CurrentCell.ColumnIndex == 1) //Desired Column
    {
        //getSum() function gives sum of a db table column
        textBox1.Text = getSum();
    }
}

enter image description here

3 个答案:

答案 0 :(得分:0)

我会将KeyDown和KeyUp事件处理程序添加到DataGridView或您的表单或任何可以关注时间的内容;)。 当按下Tab键时,我会将成员变量设置为true或false。

这是我的意思的简短例子。

public partial class Form1 : Form
{
    bool isTabPressed = false;
    public Form1()
    {
        InitializeComponent();
        button1.KeyDown += button1_KeyDown;
        button1.KeyUp += button1_KeyUp;

    }

    void button1_KeyUp(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Tab) 
        {
            isTabPressed = true;
        }
    }

    void button1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Tab) 
        {
            isTabPressed = false;
        }
    }
}

您可以在您的方法中执行的所有其他操作,您当前正在编写并检查isTabPressed是否为真。

希望这会有所帮助。 如果没有发表评论;)

编辑:

这样的事情? 目前它的作用是:你选择一些东西,当你按Tab键时,它会将它写入文本框:

private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
    editControlWasOpened = true;
}

private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
    if (dataGridView1 != null && dataGridView1.CurrentCell != null && dataGridView1.CurrentCell.Value != null && editControlWasOpened )
    {
        textBox1.Text = dataGridView1.CurrentCell.Value.ToString();
    }
    valueischanged = false;
}

答案 1 :(得分:0)

根据我的理解,根据您在ComboBox中选择的值,getSum()将从数据库中检索总值。如果是这种情况,那么添加一个SelectedIndexChanged事件,但将DataGridViewComboBoxCell转换为ComboBox,如下面的代码。

private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
    var combobox = e.Control as ComboBox;
    if (combobox != null)
    {
        combobox.SelectedIndexChanged -= ComboBox_SelectedIndexChanged;
        combobox.SelectedIndexChanged += ComboBox_SelectedIndexChanged;
    }
}

private void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
    var combobox = sender as ComboBox;
    if (combobox != null)
    {
        var item = combobox.SelectedItem.ToString();
        if (!string.IsNullOrEmtpy(item))
        {
            textBox1.Text = getSum();
            // Adjust the line above as per your requirement. 
            // I don't fully understand what getSum() does as you haven't posted it
        }
    }
}

答案 2 :(得分:0)

经过很长一段时间,这已经完成.......而且我也在为其他用户发布这个! 我的问题是 " **我想在从DataGridView中选择产品时在TextBox中显示产品余额。选择产品并按下选项卡按钮后,产品余额应显示在TextBox中。所有数据都从Microsoft Access数据库加载。我怎样才能做到这一点。" 我认为主题是"如何在选择datagridview组合框值时显示文本框值"

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {
        if (dataGridView1.CurrentCell.ColumnIndex == 0) //Desired Column
        {
            myCon.Open();
            OleDbCommand cmd1 = new OleDbCommand("SELECT sum_stock_purchase_quantity, sum_stock_purchase_quantity_thaan from productPurchaseQuery where stock_product_id=" + this.dataGridView1.CurrentCell.Value.ToString() + "");

            cmd1.Connection = myCon;
            OleDbDataReader reader = null;
            reader = cmd1.ExecuteReader();
            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    textBox9.Text = Convert.ToString(reader["sum_stock_purchase_quantity"]);
                    textBox2.Text = Convert.ToString(reader["sum_stock_purchase_quantity_thaan"]);
                }
            }
            else
            {
                textBox9.Text = "0.0";
                textBox2.Text = "0.0";
            }
            myCon.Close();

伙计们我这样做了。但这可能是好事还是坏事。但我的目标是实现的。

相关问题