我正在尝试从网格视图中删除记录但无法删除
I am trying to delete a record from grid view but unable to delete
我正在尝试从网格视图中删除一条记录,但无法删除。
使用模板字段中的 link 按钮从 gridview 中删除记录时出现这样的错误。
var id = GvTestimony.DataKeys[gvRow.RowIndex].Value.ToString();
错误信息在这里:
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
void TestimoniesGrid()
{
using (var sqlConnection = new SqlConnection(_conn))
{
using (var sqlCommand = new SqlCommand("SP_TestimoniesGridData", sqlConnection))
{
sqlCommand.CommandType = CommandType.StoredProcedure;
using (var sqlDataAdapter = new SqlDataAdapter(sqlCommand))
{
var dataTable = new DataTable();
sqlDataAdapter.Fill(dataTable);
if (dataTable.Rows.Count == 0)
{
GvTestimony.EmptyDataText = "This table has no data.";
}
else
{
GvTestimony.DataSource = dataTable;
GvTestimony.DataBind();
}
}
}
}
}
protected void lnkReject_OnClick(object sender, EventArgs e)
{
var linkButton = sender as LinkButton;
if (linkButton != null)
{
var gvRow = linkButton.NamingContainer as GridViewRow;
var id = GvTestimony.DataKeys[gvRow.RowIndex].Value.ToString();
using (var sqlConnection = new SqlConnection(_conn))
{
using (var sqlCommand = new SqlCommand("SP_DeleteTestimonyById", sqlConnection))
{
sqlCommand.CommandType = CommandType.StoredProcedure;
sqlCommand.Parameters.AddWithValue("@OwnStoryId", id);
sqlConnection.Open();
var result = sqlCommand.ExecuteNonQuery();
sqlConnection.Close();
if (result == 1)
{
TestimoniesGrid();
}
}
}
}
}
(抱歉,这是我的第一个回答)看看您如何定义网格列,以及您使用的是哪个版本的 .NET 可能会很有趣。
在这里你可以找到一个带有按钮的例子:
http://weblogs.asp.net/gurusarkar/get-gridview-rowindex-upon-button-click
我将复制最重要的部分,您可以通过两种不同的方式进行复制:
在 RowCommand 事件中获取 GridView 行和 GridView 行索引
protected void OnRowCommand(object sender, GridViewCommandEventArgs e)
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow gvRow = GridView1.Rows[index];
}
在 TemplateField 中的按钮点击事件中获取 GridView 行和 GridView 行索引
protected void Button1_Click(object sender, EventArgs e)
{
GridViewRow gvRow = (GridViewRow)(sender as Control).Parent.Parent;
int index = gvRow.RowIndex;
}
如果您需要更多信息,请告诉我。
更多示例:
我正在尝试从网格视图中删除一条记录,但无法删除。 使用模板字段中的 link 按钮从 gridview 中删除记录时出现这样的错误。
var id = GvTestimony.DataKeys[gvRow.RowIndex].Value.ToString();
错误信息在这里:
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
void TestimoniesGrid()
{
using (var sqlConnection = new SqlConnection(_conn))
{
using (var sqlCommand = new SqlCommand("SP_TestimoniesGridData", sqlConnection))
{
sqlCommand.CommandType = CommandType.StoredProcedure;
using (var sqlDataAdapter = new SqlDataAdapter(sqlCommand))
{
var dataTable = new DataTable();
sqlDataAdapter.Fill(dataTable);
if (dataTable.Rows.Count == 0)
{
GvTestimony.EmptyDataText = "This table has no data.";
}
else
{
GvTestimony.DataSource = dataTable;
GvTestimony.DataBind();
}
}
}
}
}
protected void lnkReject_OnClick(object sender, EventArgs e)
{
var linkButton = sender as LinkButton;
if (linkButton != null)
{
var gvRow = linkButton.NamingContainer as GridViewRow;
var id = GvTestimony.DataKeys[gvRow.RowIndex].Value.ToString();
using (var sqlConnection = new SqlConnection(_conn))
{
using (var sqlCommand = new SqlCommand("SP_DeleteTestimonyById", sqlConnection))
{
sqlCommand.CommandType = CommandType.StoredProcedure;
sqlCommand.Parameters.AddWithValue("@OwnStoryId", id);
sqlConnection.Open();
var result = sqlCommand.ExecuteNonQuery();
sqlConnection.Close();
if (result == 1)
{
TestimoniesGrid();
}
}
}
}
}
(抱歉,这是我的第一个回答)看看您如何定义网格列,以及您使用的是哪个版本的 .NET 可能会很有趣。
在这里你可以找到一个带有按钮的例子:
http://weblogs.asp.net/gurusarkar/get-gridview-rowindex-upon-button-click
我将复制最重要的部分,您可以通过两种不同的方式进行复制:
在 RowCommand 事件中获取 GridView 行和 GridView 行索引
protected void OnRowCommand(object sender, GridViewCommandEventArgs e) { int index = Convert.ToInt32(e.CommandArgument); GridViewRow gvRow = GridView1.Rows[index]; }
在 TemplateField 中的按钮点击事件中获取 GridView 行和 GridView 行索引
protected void Button1_Click(object sender, EventArgs e) { GridViewRow gvRow = (GridViewRow)(sender as Control).Parent.Parent; int index = gvRow.RowIndex; }
如果您需要更多信息,请告诉我。
更多示例: