C# 从DataGridViewComboBox中选择项时如何填充文本框值
当从DataGridView中选择产品时,我想在文本框中显示产品余额 选择产品并按下tab按钮后,文本框中应显示产品余额 所有数据都是从Microsoft Access数据库加载的。我怎样才能做到这一点C# 从DataGridViewComboBox中选择项时如何填充文本框值,c#,.net,ms-access-2007,C#,.net,Ms Access 2007,当从DataGridView中选择产品时,我想在文本框中显示产品余额 选择产品并按下tab按钮后,文本框中应显示产品余额 所有数据都是从Microsoft Access数据库加载的。我怎样才能做到这一点 private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { e.Control.KeyPress -= new KeyPre
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();
}
}
我会将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;
}
据我所知,根据您在组合框中选择的值,getSum将从数据库中检索总值。如果是这种情况,则添加SelectedIndexChanged事件,但将DataGridViewComboBoxCell强制转换为组合框,如下面的代码所示
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
}
}
}
经过很长一段时间,这是做了…我张贴这对其他用户太! 我的问题是 **当从DataGridView中选择产品时,我想在文本框中显示产品余额。选择产品并按下tab按钮后,文本框中应显示产品余额。所有数据都是从Microsoft Access数据库加载的。我怎样才能做到这一点。 我认为主题是如何在选择datagridview combobox值时显示textbox值
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();
伙计们,是我干的。但这可能是好的,也可能是坏的。但我的目标实现了 你试过什么办法来解决这个问题吗?向我们展示你的努力?我是c语言的新手,请指导我使用这个方法。getSum做什么?DataGridView中有一个SelectionChanged事件,每次单击或通过键盘更改不同的行时都会触发该事件。你们看过了吗?正如前面提到的getSum方法给出db表的列的和这里是我的表单布局和我想要的。。。感谢Bango…但我希望根据datagridview产品列中选择的值将db中的值分配给textbox1。。正如上面的URL所提到的:@wajahatdaar2:上面的URL是faultyyes bro,链接掉了……我已经编辑了主要问题,并在那里发布了图片