在Asp.Net中删除Winform上DataGridview的最后一行时出错c# [英] Error on Delete Last row of DataGridview on Winform in Asp.Net c#
问题描述
Hello Every One,
我在asp.net的Window应用程序中工作。我有一个GUI,用户在文本框中输入产品名称和数量。在添加按钮单击我在Datagridview中添加一个新行,并在datagridview列中设置productname和quantity的值。 我没有在数据库中插入记录,我只在Datatable中保存记录以及在Datagridview中添加记录。
问题是当我从datagridview中选择最后一行并按下键盘上的删除按钮时会产生错误
< pre lang =sql> 索引超出 范围。必须是非负和小于 集合。
参数名称: index
关注是我的代码片段..请你可以查看我在哪里做错了
static public DataTable gdt;
private void btnAdd_Click( object sender,EventArgs e)
{
try
{
if (txtItemCode.Text.Trim()== )
{
MessageBox.Show( 输入项目代码);
txtItemCode.Focus();
return ;
}
if (txtQty.Text.Trim()== < span class =code-string>)
{
MessageBox.Show( 输入数量);
txtQty.Focus();
return ;
}
if (Convert.ToInt32(txtQty.Text.Trim())< = 0 )
{
MessageBox.Show( 数量必须大于0);
txtQty.Focus();
return ;
}
if (btnAdd.Text == < span class =code-string> ADD)
{
DataRow [] dr = gdt.Select( Item_Code =' + txtItemCode.Text.Trim()+ '跨度>);
if (dr.Length > 0 )
{
MessageBox.Show( 项目代码已经存在跨度>)。
txtItemCode.Text = ;
txtItemCode.Focus();
return ;
}
tblItemMasterBLL oItem = new tblItemMasterBLL();
int ItemID = 0 ;
DataTable dt = new DataTable();
dt = oItem.getItemDetailByItemCode(txtItemCode.Text.Trim());
if (dt.Rows.Count > 0 )
{
ItemID = Convert.ToInt32(dt.Rows [ 0 ] [ Item_ID]);
gdt.Rows.Add();
gdt.Rows [gdt.Rows.Count - 1 ] [ Item_Code] = txtItemCode.Text.Trim();
gdt.Rows [gdt.Rows.Count - 1 ] [ Item_ID] = ItemID;
gdt.Rows [gdt.Rows.Count - 1 ] [ 数量] = txtQty.Text.Trim();
gdt.Rows [gdt.Rows.Count - 1 ] [ Article_Desc] = Convert.ToString(dt.Rows [ 0 ] [ Article_Desc]);
gdt.Rows [gdt.Rows.Count - 1 ] [ Color_Desc] = Convert.ToString(dt.Rows [ 0 ] [ Color_Desc]);
gdt.Rows [gdt.Rows.Count - 1 ] [ Size_Desc] = Convert.ToString(dt.Rows [ 0 ] [ Size_Desc]);
gdt.Rows [gdt.Rows.Count - 1 ] [ MRP] = Convert.ToString(dt.Rows [ 0 ] [ MRP]);
dgv_Items.DataSource = null ;
dgv_Items.DataSource = gdt;
}
其他
{
MessageBox .Show( 无效的商品代码);
}
txtItemCode.Text = ;
txtQty.Text = ;
}
其他 如果(btnAdd.Text == UPDATE)
{
if (gdt.Rows.Count > 0 )
{
gdt。行[Convert.ToInt32(lblhdnRowIndex.Text)] [ 数量] = txtQty.Text.Trim ();
dgv_Items.Rows [Convert.ToInt32(lblhdnRowIndex.Text)]。单元格[ 数量]。Value = txtQty.Text.Trim();
}
txtItemCode.ReadOnly = false ;
txtItemCode.Text = ;
txtQty.Text = ;
lblhdnItemID.Text = ;
lblhdnItemCode.Text = ;
lblhdnQty.Text = ;
btnAdd.Text = ADD;
lblhdnRowIndex.Text = ;
}
}
catch (例外情况)
{
MessageBox.Show(ex.Message) ;
}
}
私有 void dgv_Items_UserDeletingRow( object sender,DataGridViewRowCancelEventArgs e)
{
try
{
if (MessageBox.Show( 你有吗?想要删除当前行?, 确认删除,
MessageBoxButtons.YesNo,MessageBoxIcon.Question)== DialogResult.Yes)
{
ScrollPosition = 0 ;
ScrollPosition = dgv_Items.FirstDisplayedScrollingRowIndex;
int iIndex = dgv_Items.CurrentRow.Index;
gdt.Rows.RemoveAt(iIndex);
}
}
catch (例外情况)
{
MessageBox.Show( ex.Message);
}
}
private void dgv_Items_UserDeletedRow( object sender,DataGridViewRowEventArgs e)
{
try
{
dgv_Items.DataSource = null ;
dgv_Items.DataSource = gdt;
dgv_Items.Rows [dgv_Items.Rows.Count - 1 ]。Visible = false ;
}
catch (例外情况)
{
MessageBox.Show(ex.Message);
}
}
请帮我提供解决方案...
@coolnavjot,亲爱的你不能在ASP.Net中使用windows应用程序......;)
请参阅以下链接..
使用ASP.NET中的DetailsView控件执行插入,更新和删除操作 [ ^ ]
您正在尝试访问此成员所在的集合的成员不存在。
例如考虑一个数组的大小为5.你试图访问第七项arr [6]。
这将抛出一个错误。
尝试单步调试你的代码,你将能够弄清楚哪个集合引发了这个错误。
嗨大家好,感谢你们所有人参与解决我的问题。实际上这是指数问题。
我找到了解决方案我已经在Databridview的 UserDeletingRow 事件中做了更改。我在 UserDeletingRow eventHich中添加了一个新行,它是粗体字。现在我的代码工作正常。
之前:
private void dgv_Items_UserDeletingRow( object sender, DataGridViewRowCancelEventArgs e)
{
try
{
if (MessageBox.Show( 是否要删除当前行?, 确认删除,
MessageBoxButtons.YesNo,MessageBoxIcon.Question)== DialogResult.Yes)
{
ScrollPosition = 0 ;
ScrollPosition = dgv_Items.FirstDisplayedScrollingRowIndex;
int iIndex = dgv_Items.CurrentRow.Index;
gdt.Rows.RemoveAt(iIndex);
}
}
catch (例外情况)
{
MessageBox.Show( ex.Message);
}
}
之后
< span class =code-keyword> private void dgv_Items_UserDeletingRow( object sender,DataGridViewRowCancelEventArgs e )
{
尝试
{
如果( MessageBox.Show( 是否要删除当前行?, 确认删除,
MessageBoxButtons.YesNo,MessageBoxIcon.Question)== DialogResult.Yes)
{
ScrollPosition = 0 ;
ScrollPosition = dgv_Items.FirstDisplayedScrollingRowIndex;
int iIndex = dgv_Items.CurrentRow.Index;
DataRow dr = gdt.Rows [iIndex];
gdt.Rows.RemoveAt(iIndex);
gdt.Rows.InsertAt(dr,iIndex);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Hello Every One,
I am working in Window application in asp.net. I have a GUI in which user enter a product name and quantity in text boxes. On Add button click i am adding a new row in Datagridview and set the value of productname and quantity in datagridview columns. I am not inserting record in Database and I am only save record in Datatable as well add record in Datagridview.
Problem is that when I select a last row from datagridview and press delete button from keyboard then it generate an error
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
Following is my code snippet.. Please you can check where i am doing wrong
static public DataTable gdt;
private void btnAdd_Click(object sender, EventArgs e)
{
try
{
if (txtItemCode.Text.Trim() == "")
{
MessageBox.Show("Enter Item Code");
txtItemCode.Focus();
return;
}
if (txtQty.Text.Trim() == "")
{
MessageBox.Show("Enter Qty");
txtQty.Focus();
return;
}
if (Convert.ToInt32(txtQty.Text.Trim()) <= 0)
{
MessageBox.Show("Qty must be greater than 0");
txtQty.Focus();
return;
}
if (btnAdd.Text == "ADD")
{
DataRow[] dr = gdt.Select("Item_Code = '" + txtItemCode.Text.Trim() + "'");
if (dr.Length > 0)
{
MessageBox.Show("Item Code Already Exist.");
txtItemCode.Text = "";
txtItemCode.Focus();
return;
}
tblItemMasterBLL oItem = new tblItemMasterBLL();
int ItemID = 0;
DataTable dt = new DataTable();
dt = oItem.getItemDetailByItemCode(txtItemCode.Text.Trim());
if (dt.Rows.Count > 0)
{
ItemID = Convert.ToInt32(dt.Rows[0]["Item_ID"]);
gdt.Rows.Add();
gdt.Rows[gdt.Rows.Count - 1]["Item_Code"] = txtItemCode.Text.Trim();
gdt.Rows[gdt.Rows.Count - 1]["Item_ID"] = ItemID;
gdt.Rows[gdt.Rows.Count - 1]["Qty"] = txtQty.Text.Trim();
gdt.Rows[gdt.Rows.Count - 1]["Article_Desc"] = Convert.ToString(dt.Rows[0]["Article_Desc"]);
gdt.Rows[gdt.Rows.Count - 1]["Color_Desc"] = Convert.ToString(dt.Rows[0]["Color_Desc"]);
gdt.Rows[gdt.Rows.Count - 1]["Size_Desc"] = Convert.ToString(dt.Rows[0]["Size_Desc"]);
gdt.Rows[gdt.Rows.Count - 1]["MRP"] = Convert.ToString(dt.Rows[0]["MRP"]);
dgv_Items.DataSource = null;
dgv_Items.DataSource = gdt;
}
else
{
MessageBox.Show("Invalid Item Code");
}
txtItemCode.Text = "";
txtQty.Text = "";
}
else if (btnAdd.Text == "UPDATE")
{
if (gdt.Rows.Count > 0)
{
gdt.Rows[Convert.ToInt32(lblhdnRowIndex.Text)]["Qty"] = txtQty.Text.Trim();
dgv_Items.Rows[Convert.ToInt32(lblhdnRowIndex.Text)].Cells["Qty"].Value = txtQty.Text.Trim();
}
txtItemCode.ReadOnly = false;
txtItemCode.Text = "";
txtQty.Text = "";
lblhdnItemID.Text = "";
lblhdnItemCode.Text = "";
lblhdnQty.Text = "";
btnAdd.Text = "ADD";
lblhdnRowIndex.Text = "";
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void dgv_Items_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
{
try
{
if (MessageBox.Show("Do you want to delete the current row?", "Confirm deletion",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
ScrollPosition = 0;
ScrollPosition = dgv_Items.FirstDisplayedScrollingRowIndex;
int iIndex = dgv_Items.CurrentRow.Index;
gdt.Rows.RemoveAt(iIndex);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void dgv_Items_UserDeletedRow(object sender, DataGridViewRowEventArgs e)
{
try
{
dgv_Items.DataSource = null;
dgv_Items.DataSource = gdt;
dgv_Items.Rows[dgv_Items.Rows.Count - 1].Visible = false;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Please help me give any solution...
@coolnavjot, Dear you can not windows application in ASP.Net... ;)
refer below link..
Performing Insert, Update and Delete Operations using DetailsView Control in ASP.NET[^]
You are trying to access a member of an collection where this member does not exists.
For e.g. consider an array has a size of 5. You are trying to access the seventh item arr[6].
This will throw an error.
Try stepping through your code and you will be able to figure out which collection is throwing this error.
Hi Guys, thanks all of u participate to solve my problem. Actually this is index problem.
I have find out the solution In which I have done changes in UserDeletingRow event of Datagridview. I have added a new line in UserDeletingRow eventhich is in bold font. Now my code is working fine.
Before:
private void dgv_Items_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e) { try { if (MessageBox.Show("Do you want to delete the current row?", "Confirm deletion", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { ScrollPosition = 0; ScrollPosition = dgv_Items.FirstDisplayedScrollingRowIndex; int iIndex = dgv_Items.CurrentRow.Index; gdt.Rows.RemoveAt(iIndex); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
After
private void dgv_Items_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e) { try { if (MessageBox.Show("Do you want to delete the current row?", "Confirm deletion", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { ScrollPosition = 0; ScrollPosition = dgv_Items.FirstDisplayedScrollingRowIndex; int iIndex = dgv_Items.CurrentRow.Index; DataRow dr = gdt.Rows[iIndex]; gdt.Rows.RemoveAt(iIndex); gdt.Rows.InsertAt(dr, iIndex); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
这篇关于在Asp.Net中删除Winform上DataGridview的最后一行时出错c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!