在執行我上面的代碼時,并沒有任何以异當,了沒有執行catch塊的內容,只有勢執行完最后一個"}"后,才會彈出錯誤提示,所以我跟蹤了沒找出原因.
問題仍然存在, 因為catch塊中的代碼根本就不會被運行.
try中的操作能順利完成,并且記錄也能順利刪除. 只是在刪除最后一條記錄時,DGV才會在運行完上面代碼中的最后一個"}"后報錯.
另外,因為我的DGV綁定的是BindingSource,如果我使用Filter屬性來篩選記錄,如果篩選后的結果中為0行記錄,則為出現同樣的情況,第一個DGV的RowErrorText中都會有類似的錯誤提示,
例如,如果原先DGV中有5筆記錄,則篩選后,每行的錯誤文本分別為:
"索引0沒有值","索引1沒有值","索引2沒有值","索引3沒有值","索引4沒有值".......
根蹤后發生,DGV中的Rows.Count行為5, 而BindingSource中的Count為0
但是如果原DGV中有5筆記錄,而我篩選后的結果中在有3條記錄,則DGV中的Rows.Count值為3,BindingSource中的Count值為3.不會出錯.
[此贴子已经被作者于2007-10-9 14:38:11编辑过]
問題仍未解決﹐現附上部份代碼﹐請大蝦們幫看看
1﹒綁定數据到DGV中的代碼
private void bindingQuotationList(string strSupplierID)
{
if (ds.Tables["QuotationList"] != null)
{
ds.Tables["QuotationList"].Clear();
}
SqlCommand cmdQuotationList = new SqlCommand();
cmdQuotationList.CommandText = "SELECT * FROM [ViewQuotation] WHERE [SupplierID] = @sid ORDER BY [QuotationID]";
cmdQuotationList.CommandType = CommandType.Text;
cmdQuotationList.Parameters.Add(new SqlParameter("@sid", strSupplierID));
cmdQuotationList.Connection = conn;
sdaQuotationList = new SqlDataAdapter(cmdQuotationList);
sdaQuotationList.Fill(ds, "QuotationList");
bsQuotationList.DataSource = ds.Tables["QuotationList"];
dgvQuotationList.DataSource = bsQuotationList;
}
2.執行刪除的代碼﹕
private void tsbDelete_Click(object sender, EventArgs e)
{
if (dgvQuotationList.Rows.Count == 0)
{
MessageBox.Show("沒有可刪除的記錄!", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (DialogResult.No == MessageBox.Show("确定要刪除料號為[ " + tbPartID.Text.Trim() + " ]的料件單价資料嗎?", "詢問", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
{
return;
}
string strSupplierID = tbSupplierID.Text.Trim();
string strPartID = tbPartID.Text.Trim();
string strMsg = "";
SqlCommand cmd = new SqlCommand();
try
{
if (conn.State != ConnectionState.Open)
{
conn.Open();
}
cmd.Connection = conn;
cmd.CommandText = "Quotation_Delete";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@supplierID", SqlDbType.VarChar, 5);
cmd.Parameters.Add("@partID", SqlDbType.VarChar, 18);
cmd.Parameters.Add("@errorMsg", SqlDbType.VarChar, 200);
cmd.Parameters["@supplierID"].Value = strSupplierID;
cmd.Parameters["@partID"].Value = strPartID;
cmd.Parameters["@errorMsg"].Direction = ParameterDirection.Output;
if (cmd.ExecuteNonQuery() >0)
{
bsQuotationList.RemoveCurrent();
bsQuotationList.EndEdit();
currentState = state.Browse;
ctlState();
}
}
catch
{
strMsg = cmd.Parameters["@errorMsg"].Value.ToString().Trim();
MessageBox.Show(strMsg, "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
if (conn.State != ConnectionState.Closed)
{
conn.Close();
}
}
}