读书人

dataGridView1 两列相乘解决办法

发布时间: 2012-05-22 18:18:54 作者: rapoo

dataGridView1 两列相乘
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (string.IsNullOrEmpty(dataGridView1.Rows[e.RowIndex].Cells[4].Value.ToString()) || string.IsNullOrEmpty(dataGridView1.Rows[e.RowIndex].Cells[5].Value.ToString()))
return;//没有输入数值时,不要进行计算
// 总数量=数量*串数
if (e.ColumnIndex == 4 || e.ColumnIndex == 5)
{
try
{
dataGridView1.Rows[e.RowIndex].Cells[6].Value = (Convert.ToDecimal(dataGridView1.Rows[e.RowIndex].Cells[4].Value) * Convert.ToDecimal(dataGridView1.Rows[e.RowIndex].Cells[5].Value)).ToString();
}
catch (Exception)
{
MessageBox.Show("请输入数字");
}
}

}

提示错误:
索引超出范围。必须为非负值并小于集合大小。
参数名: index
调试时查看 index的值为-1

已将 AllowUserToAddRows =false;


[解决办法]

C# code
 if (e.RowIndex != -1 && (e.ColumnIndex == 4 || e.ColumnIndex == 5)) 

读书人网 >C#

热点推荐