datagridview和sqlite数据库的问题
Problem with datagridview and sqlite database
这是我的问题。我有一个 datagridview,通过选择我删除相应行并更新数据库的框。
如果我 dataEmployed.CurrentCell.RowIndex > 0 然后我不删除最后一行,如果我把 dataEmployed.CurrentCell.RowIndex >= 0 它给我一个异常错误。
我哪里错了?
private void DeleterowBtn_Click(object sender, EventArgs e)
{
if (dataEmployed.SelectedRows.Count == 0)
{
MessageBox.Show("Il database è vuoto", "Attenzione", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
string dbConnection = "Data Source=" + path + ";Version=3";
using (SQLiteConnection conn = new SQLiteConnection(dbConnection))
{
SQLiteCommand cmd = new SQLiteCommand("DELETE FROM Staff WHERE Id = @rowID", conn);
conn.Open();
if (dataEmployed.CurrentCell.RowIndex > 0)
{
int selectIndex = dataEmployed.SelectedRows[0].Index;
int rowID = Convert.ToInt32(dataEmployed[0, selectIndex].Value);
cmd.Parameters.Add("@rowID", DbType.Int32).Value = rowID;
cmd.ExecuteNonQuery();
dataEmployed.Rows.RemoveAt(selectIndex);
}
}
}
}
错误听起来像……“无法删除未提交的新行。”,……这个错误来自 DataGridView.
它抱怨代码专门尝试到“delete/remove”网格“新”行。这很容易修复,如下所示。
从某种意义上说,当前代码有些混乱,if
语句…
if (dataEmployed.CurrentCell.RowIndex > 0) { …
… 正在检查网格 CurrentCell.RowIndex
是否大于零 (0)。然后,当代码开始实际删除该行时,该代码使用与在前一个 if
语句中检查的行索引“不同”的行索引。
int selectIndex = dataEmployed.SelectedRows[0].Index;
dataEmployed.Rows.RemoveAt(selectIndex)
此代码使用 SelectedRows[0].Index
删除行。这里的问题是网格 CurrentCell.RowIndex
不一定是与当前“选定”行相同的行索引。因此,代码正在检查 Y 并使用 X。
因此,当代码尝试从“不同”索引 selectIndex
中删除行时,如果这两个索引不同,则不足为奇。该代码需要 if
语句来检查网格 SelectedRows[0].Index.
具体来说,我们要检查“选定”行索引是否是“新”行索引。并且……幸运的是,网格有一行 属性 专门为此称为 IsNewRow
并且如果该行是“新”行,则 return 为真。 …
if (!dataEmployed.Rows[selectIndex].IsNewRow) { …
我相信这些小改动应该可以消除您遇到的错误。
private void DeleterowBtn_Click(object sender, EventArgs e) {
if (dataEmployed.SelectedRows.Count == 0) {
MessageBox.Show("No rows are selected", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
int selectIndex = dataEmployed.SelectedRows[0].Index;
if (!dataEmployed.Rows[selectIndex].IsNewRow) {
string dbConnection = "Data Source=" + path + ";Version=3";
using (SQLiteConnection conn = new SQLiteConnection(dbConnection)) {
SQLiteCommand cmd = new SQLiteCommand("DELETE FROM Staff WHERE Id = @rowID", conn);
conn.Open();
int rowID = Convert.ToInt32(dataEmployed[0, selectIndex].Value);
cmd.Parameters.Add("@rowID", DbType.Int32).Value = rowID;
cmd.ExecuteNonQuery();
dataEmployed.Rows.RemoveAt(selectIndex);
}
}
}
最后,不知道代码最初是如何填充网格的 and/or 如果它使用数据源。我强烈建议您使用 BindingSource
或 BindingList
作为网格的数据源。这会让事情变得更容易。在大多数情况下,使用代码 rare/frowned 从网格中“直接”删除行。在大多数情况下,您希望从网格数据源中删除该行并让网格自动更新。这只是一个建议,是另外一个故事。
我希望这是有道理的。祝你好运。
这是我的问题。我有一个 datagridview,通过选择我删除相应行并更新数据库的框。 如果我 dataEmployed.CurrentCell.RowIndex > 0 然后我不删除最后一行,如果我把 dataEmployed.CurrentCell.RowIndex >= 0 它给我一个异常错误。 我哪里错了?
private void DeleterowBtn_Click(object sender, EventArgs e)
{
if (dataEmployed.SelectedRows.Count == 0)
{
MessageBox.Show("Il database è vuoto", "Attenzione", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
string dbConnection = "Data Source=" + path + ";Version=3";
using (SQLiteConnection conn = new SQLiteConnection(dbConnection))
{
SQLiteCommand cmd = new SQLiteCommand("DELETE FROM Staff WHERE Id = @rowID", conn);
conn.Open();
if (dataEmployed.CurrentCell.RowIndex > 0)
{
int selectIndex = dataEmployed.SelectedRows[0].Index;
int rowID = Convert.ToInt32(dataEmployed[0, selectIndex].Value);
cmd.Parameters.Add("@rowID", DbType.Int32).Value = rowID;
cmd.ExecuteNonQuery();
dataEmployed.Rows.RemoveAt(selectIndex);
}
}
}
}
错误听起来像……“无法删除未提交的新行。”,……这个错误来自 DataGridView.
它抱怨代码专门尝试到“delete/remove”网格“新”行。这很容易修复,如下所示。
从某种意义上说,当前代码有些混乱,if
语句…
if (dataEmployed.CurrentCell.RowIndex > 0) { …
… 正在检查网格 CurrentCell.RowIndex
是否大于零 (0)。然后,当代码开始实际删除该行时,该代码使用与在前一个 if
语句中检查的行索引“不同”的行索引。
int selectIndex = dataEmployed.SelectedRows[0].Index;
dataEmployed.Rows.RemoveAt(selectIndex)
此代码使用 SelectedRows[0].Index
删除行。这里的问题是网格 CurrentCell.RowIndex
不一定是与当前“选定”行相同的行索引。因此,代码正在检查 Y 并使用 X。
因此,当代码尝试从“不同”索引 selectIndex
中删除行时,如果这两个索引不同,则不足为奇。该代码需要 if
语句来检查网格 SelectedRows[0].Index.
具体来说,我们要检查“选定”行索引是否是“新”行索引。并且……幸运的是,网格有一行 属性 专门为此称为 IsNewRow
并且如果该行是“新”行,则 return 为真。 …
if (!dataEmployed.Rows[selectIndex].IsNewRow) { …
我相信这些小改动应该可以消除您遇到的错误。
private void DeleterowBtn_Click(object sender, EventArgs e) {
if (dataEmployed.SelectedRows.Count == 0) {
MessageBox.Show("No rows are selected", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
int selectIndex = dataEmployed.SelectedRows[0].Index;
if (!dataEmployed.Rows[selectIndex].IsNewRow) {
string dbConnection = "Data Source=" + path + ";Version=3";
using (SQLiteConnection conn = new SQLiteConnection(dbConnection)) {
SQLiteCommand cmd = new SQLiteCommand("DELETE FROM Staff WHERE Id = @rowID", conn);
conn.Open();
int rowID = Convert.ToInt32(dataEmployed[0, selectIndex].Value);
cmd.Parameters.Add("@rowID", DbType.Int32).Value = rowID;
cmd.ExecuteNonQuery();
dataEmployed.Rows.RemoveAt(selectIndex);
}
}
}
最后,不知道代码最初是如何填充网格的 and/or 如果它使用数据源。我强烈建议您使用 BindingSource
或 BindingList
作为网格的数据源。这会让事情变得更容易。在大多数情况下,使用代码 rare/frowned 从网格中“直接”删除行。在大多数情况下,您希望从网格数据源中删除该行并让网格自动更新。这只是一个建议,是另外一个故事。
我希望这是有道理的。祝你好运。