删除命令给出错误

编程入门 行业动态 更新时间:2024-10-22 15:36:30
本文介绍了删除命令给出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

大家好, 无论我在哪里尝试删除我的datagridview行,它都给我

Hi all, Wherever i try to delete my datagridview row its giving me

datagridview index was out of range. must be non-negative and less than the size of the collection. parameter name: index exception

我最后一个列的名称是页脚的余额,它显示我的总值,但问题是什么它没有显示我最后的余额值 假设我输入三个值余额--2000,3000,4000所以它显示我只有2000 + 3000总和它不显示最后一行总和我粘贴该代码 我尝试过: 删除代码:

and i have one last column name is balance at at footer its show me total values but the problem is what its not showing me last balance value suppose im enter three values balance --2000, 3000, 4000 so its show me only 2000+3000 sum its not showing last row sum im pasting that code also What I have tried: Delete Code:

private void btndelete_Click(object sender, EventArgs e) { if (dataGridView1.SelectedCells.Count > 0) { if (dataGridView1.Rows.Count > 1 && dataGridView1.SelectedRows[0].Index != dataGridView1.Rows.Count - 1 ) { cmd.CommandText = "Delete from Ledger where AccountNumber=" + dataGridView1.SelectedRows[0].Cells[0].Value.ToString() + ""; con.Open(); cmd.Connection = con; cmd.ExecuteNonQuery(); con.Close(); dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[0].Index); MessageBox.Show("Row Deleted"); Load_data(); } else { MessageBox.Show("Please select a row"); } } }

总和代码:

total Sum code :

private void dataGridView1_DataSourceChanged(object sender, EventArgs e) { double Total = 0; for (int i = 0; i < dataGridView1.Rows.Count - 2; i++) { Total += Convert.ToDouble(dataGridView1.Rows[i].Cells["Balance"].Value); } dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells["Balance"].Value = Total; lblsum.Text =Convert.ToString(Total); }

推荐答案

这里是我得到的解决方案我没有初始化OleDbCommand ();对象,这就是错误发生的原因,对于我写的总和 here is i got solution im not initialized OleDbCommand(); object that's why error occur and for sum i written for (int i = 0; i < dataGridView1.Rows.Count - 2; i++)

此代码但不是2我写的1所以因此我得到了所有错误修复,我粘贴了删除n总和代码 删除代码:

this code but instead of 2 i written 1 so hence i get all bug fixes here im pasting both delete n total sum code delete code:

OleDbCommand cmd=new OleDbCommand ();

private void btnclear_Click(object sender, EventArgs e) { if (dataGridView1.Rows.Count > 1 && dataGridView1.SelectedRows.Count > 0 && dataGridView1.SelectedRows[0].Index != dataGridView1.Rows.Count - 1) { cmd.CommandText = "Delete from Ledger where AccountNumber=" + dataGridView1.SelectedRows[0].Cells["AccountNumber"].Value.ToString() + ""; con.Open(); cmd.Connection = con; cmd.ExecuteNonQuery(); con.Close(); dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[0].Index); MessageBox.Show("Row Deleted"); Load_data(); } else { MessageBox.Show("Please select a row"); } }

总计总和代码:

total sum code :

private void dataGridView1_DataSourceChanged(object sender, EventArgs e) { double Total = 0; for (int i = 0; i < dataGridView1.Rows.Count - 1; i++) { Total += Convert.ToDouble(dataGridView1.Rows[i].Cells["Balance"].Value); } dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells["Balance"].Value = Total; lblsum.Text = Convert.ToString(Total); }

更多推荐

删除命令给出错误

本文发布于:2023-10-28 01:14:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1535066.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:命令   错误

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!