大家好, 无论我在哪里尝试删除我的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); }
更多推荐
删除命令给出错误
发布评论