如何在datagridview单元格值更改时调用方法 [英] how to call a method when a datagridview cell value change
问题描述
我的表单上有一个datagridview和一个按钮。当值输入到
datagridview的2个单元格中并按下按钮时,数字将相乘,结果将显示在第三个单元格中。这项工作没问题。我现在想要在不按下按钮的情况下自动完成乘法。也就是说,当单元格的值发生变化时,它应该调用负责计算的函数。
i尝试了这个,但它一直在遗漏错误
类型的未处理异常' System.Data.dll中发生System.StackOverflowException
请帮帮我
代码
< pre lang =cs> public void multiplyrows()
{
< span class =code-keyword> for ( int i = 0 ; i < span class =code-keyword>< dgvSales.Rows.Count - 1 ; i ++)
{
double a = Convert.ToDouble(dgvSales.Rows [i] .Cells [ 已售出数量]。值);
double b = Convert.ToDouble(dgvSales.Rows [i] .Cells [ < span class =code-string>单价]。值);
this .dgvSales.Rows [i] .Cells [ 5 ]。值=(a * b).ToString( 0.00);
}
}
private void dgvSales_CellValueChanged( object sender,DataGridViewCellEventArgs e)
{
multiplyrows();
}
1)您是否在方法中设置断点以查看它是否被击中?
2)您的转换调用非常危险,因为它们会对它们进行编码,因为它们假定引用的每个单元格总是包含有效的double值。更安全的方法是这样的:
double a,b;
if ( double .TryParse(dgvSales.Rows [i] .Cells [ 已售出数量]。值, out a) &&
double .TryParse(dgvSales.Rows [i] .Cells [ 单价]。值, out b))
{
this .dgvSales.Rows [i] .Cells [ 5 ]。值=(a * b).ToString( 0.00);
}
其他
{
此 .dgvSales .Rows [i] .Cells [ 5 ]。值= 一些默认值;
}
如评论中所述,这并未解决根本问题,这是你通过直接更新单元格创建的无限循环。解决这个问题的方法是使用一个基础DataTable,它用作DataGridView控件的DataSource。在单元格值更改处理程序中,更新基础DataTable值,然后对该对象执行计算。完成后,只需重新绑定DataGridView即可。但请使用.TryParse而不是转换以避免转换错误。
感谢大家,这是解决方案
private void dgvSales_CellValueChanged( object sender,DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 2 || e.ColumnIndex == 4 )
{
double a = Convert.ToDouble(dgvSales.Rows [e.RowIndex] .Cells [ Quantity Sold跨度>]值)。
double b = Convert.ToDouble(dgvSales.Rows [e.RowIndex] .Cells [ 单价]。值);
dgvSales.Rows [e.RowIndex] .Cells [ 5 ]。值=(a * b).ToString( 0.00);
}
}
i have a datagridview and a button on my form. when values are entered into the 2 cells of the
datagridview and the button is pressed, the numbers are multiplied and the result is displayed in the third cell. this works alright. i now want the multiplication to be done automaticaly without pressing the button. that is when the value of the cell change it should call the function responsible for the calculation.
i tryed this but it keeps genetrating the error
An unhandled exception of the type 'System.StackOverflowException' occurred in System.Data.dll
please help me out
the codes
public void multiplyrows()
{
for (int i = 0; i < dgvSales.Rows.Count - 1; i++)
{
double a = Convert.ToDouble(dgvSales.Rows[i].Cells["Quantity Sold"].Value);
double b = Convert.ToDouble(dgvSales.Rows[i].Cells["Unit Price"].Value);
this.dgvSales.Rows[i].Cells[5].Value = (a*b).ToString("0.00");
}
}
private void dgvSales_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
multiplyrows();
}
1) Have you set a breakpoint in your method to see if it even gets hit?
2) Your convert calls are very dangerous as you have them coded because they assume that every cell referenced will ALWAYS contain a valid double value. The safer way to do this would be like this:
double a, b; if( double.TryParse( dgvSales.Rows[i].Cells["Quantity Sold"].Value, out a ) && double.TryParse( dgvSales.Rows[i].Cells["Unit Price"].Value, out b ) ) { this.dgvSales.Rows[i].Cells[5].Value = (a*b).ToString("0.00"); } else { this.dgvSales.Rows[i].Cells[5].Value = "Some default value"; }
[Edit]
As noted in the comments, this does not address the root issue at hand which is your infinite loop being created by updating cells directly. The way to solve this is to have an underlying DataTable which is used as a DataSource for the DataGridView control. In the cell value changed handler, update the underlying DataTable value and then perform your calculations on that object. When complete, simply rebind your DataGridView and you should be ok. But please use the .TryParse and not the Convert to avoid conversion errors.
thanks to you all, this is the solution
private void dgvSales_CellValueChanged(object sender, DataGridViewCellEventArgs e) { if (e.ColumnIndex == 2 || e.ColumnIndex == 4) { double a = Convert.ToDouble(dgvSales.Rows[e.RowIndex].Cells["Quantity Sold"].Value); double b = Convert.ToDouble(dgvSales.Rows[e.RowIndex].Cells["Unit Price"].Value); dgvSales.Rows[e.RowIndex].Cells[5].Value = (a*b).ToString("0.00"); } }
这篇关于如何在datagridview单元格值更改时调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!