![](/img/trans.png)
[英]DataGridView.CellValueChanged not firing on bound DataGridView
[英]DataGridView.CellValueChanged
当datagridview中组合框单元格的值更改时,如何更改单元格的值?
当“ producto”列的值更改时,然后更改“ precio”列
private void DgvDetalleVta_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
DataGridViewComboBoxCell cb = sender as DataGridViewComboBoxCell;
command.Parameters.AddWithValue("@Nombre", cb.Value.ToString());
command = new SqlCommand(
"SELECT Precio FROM Producto WHERE Nombre = @Nombre", conexion.conexion);
adaptador = new SqlDataAdapter(command);
tabla = new DataTable();
adaptador.Fill(tabla);
dgvDetalleVta.SelectedRows[0].Cells["Column5"].Value =
tabla.Rows[0]["Precio"].ToString();
}
编辑结束后,我将使用CellEndEdit事件处理程序来获取组合框值。 您需要将行和单元格强制转换为DataGridViewComboBoxCell。
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex != -1)
{
using (var cbx = (DataGridViewComboBoxCell)dataGridView1.Rows[e.RowIndex].Cells["ColumnCellName"])
{
try
{
// Do something with value .... cbx.Value
}
catch
{ }
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.