| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2748 人关注过本帖
标题:[求助]在DGV中拋出异常"索引0沒有值"
取消只看楼主 加入收藏
C_B_Lu
Rank: 1
等 级:新手上路
威 望:1
帖 子:453
专家分:0
注 册:2006-1-10
收藏
 问题点数:0 回复次数:10 
[求助]在DGV中拋出异常"索引0沒有值"
我的DGV的數据源是一個BindingSource對象,當我用Filter篩選數据,當篩選的結果為空(即沒有滿足Filter中條件的記錄)時,就會拋出一個异常,"索引0沒有值",該怎么解決?
搜索更多相关主题的帖子: 索引 DGV Filter 解決 
2007-10-05 15:00
C_B_Lu
Rank: 1
等 级:新手上路
威 望:1
帖 子:453
专家分:0
注 册:2006-1-10
收藏
得分:0 

本想嘗試在DGV的DataError事件中寫一些代碼,但真不知該怎么處理....


自已再頂一下,


帮助那些真正需要帮助的人,是对帮助你的人最好的回报!
2007-10-05 16:30
C_B_Lu
Rank: 1
等 级:新手上路
威 望:1
帖 子:453
专家分:0
注 册:2006-1-10
收藏
得分:0 
問題仍然存在,現附上我的刪除代碼及錯誤圖片.
图片附件: 游客没有浏览图片的权限,请 登录注册

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();
}
}
}

帮助那些真正需要帮助的人,是对帮助你的人最好的回报!
2007-10-08 09:48
C_B_Lu
Rank: 1
等 级:新手上路
威 望:1
帖 子:453
专家分:0
注 册:2006-1-10
收藏
得分:0 
以下是引用jacklee在2007-10-8 9:53:28的发言:
用TRY去除掉你的错误吧

在執行我上面的代碼時,并沒有任何以异當,了沒有執行catch塊的內容,只有勢執行完最后一個"}"后,才會彈出錯誤提示,所以我跟蹤了沒找出原因.


帮助那些真正需要帮助的人,是对帮助你的人最好的回报!
2007-10-08 16:16
C_B_Lu
Rank: 1
等 级:新手上路
威 望:1
帖 子:453
专家分:0
注 册:2006-1-10
收藏
得分:0 
以下是引用jacklee在2007-10-8 16:43:10的发言:
strMsg = cmd.Parameters["@errorMsg"].Value.ToString().Trim();
MessageBox.Show(strMsg, "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
上面两句改写成MSGBOX("KK");
还会不会有错?不妨试一下!

問題仍然存在, 因為catch塊中的代碼根本就不會被運行.
try中的操作能順利完成,并且記錄也能順利刪除. 只是在刪除最后一條記錄時,DGV才會在運行完上面代碼中的最后一個"}"后報錯.


帮助那些真正需要帮助的人,是对帮助你的人最好的回报!
2007-10-09 08:29
C_B_Lu
Rank: 1
等 级:新手上路
威 望:1
帖 子:453
专家分:0
注 册:2006-1-10
收藏
得分:0 

另外,因為我的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编辑过]


帮助那些真正需要帮助的人,是对帮助你的人最好的回报!
2007-10-09 14:17
C_B_Lu
Rank: 1
等 级:新手上路
威 望:1
帖 子:453
专家分:0
注 册:2006-1-10
收藏
得分:0 
问题仍未解决,再顶一下...

帮助那些真正需要帮助的人,是对帮助你的人最好的回报!
2007-10-10 08:37
C_B_Lu
Rank: 1
等 级:新手上路
威 望:1
帖 子:453
专家分:0
注 册:2006-1-10
收藏
得分:0 
问题仍未解决,再顶一下...

帮助那些真正需要帮助的人,是对帮助你的人最好的回报!
2007-10-13 09:52
C_B_Lu
Rank: 1
等 级:新手上路
威 望:1
帖 子:453
专家分:0
注 册:2006-1-10
收藏
得分:0 

問題仍未解決﹐現附上部份代碼﹐請大蝦們幫看看
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();
}
}
}


帮助那些真正需要帮助的人,是对帮助你的人最好的回报!
2007-10-13 09:56
C_B_Lu
Rank: 1
等 级:新手上路
威 望:1
帖 子:453
专家分:0
注 册:2006-1-10
收藏
得分:0 
再顶一下....

帮助那些真正需要帮助的人,是对帮助你的人最好的回报!
2007-10-17 07:54
快速回复:[求助]在DGV中拋出异常"索引0沒有值"
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.013392 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved