如何进行行编辑 [英] how to do row editing

查看:64
本文介绍了如何进行行编辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 protected void GridView1_RowEditing(object sender,GridViewEditEventArgs e)
{
int id = Convert.ToInt16(GridView1.DataKeys [e.NewEditIndex] .Values [0] .ToString ());

cnn.Open();
SqlCommand cmd = new SqlCommand(从call_details中删除,其中id =+ id,cnn);
int result = cmd.ExecuteNonQuery();
cnn.Close();

GridView1.DataBind();





此代码会给我一个错误:索引超出范围。必须是非负数且小于集合的大小。

参数名称:index



i没有使用模板字段。那么什么是解决方案。

解决方案

//请尝试使用此代码以获得帮助....



protected void GridView1_RowEditing(object sender,GridViewEditEventArgs e)

{



GridView1.EditIndex = e.NewEditIndex;



// yourControlName it'TheInstance =

//( yourControlName)//(GridView1.Rows[GridView1.EditIndex].FindControl (yourContr // olId));



//例如



标签lbl =(标签)(GridView1.Rows [GridView1.EditIndex] .FindControl(lblId));



int id = convert.ToInt16(lbl.Text);



cnn.Open();

SqlCommand cmd = new SqlCommand(从call_details删除,其中id =+ id,cnn);

int result = cmd.ExecuteNonQuery();

cnn.Close();



GridView1.DataBind() ;

 


1。在itemtemplate中添加一个按钮。

2.使用CommandArgument传递要删除的记录的id。

3.为gridview添加RowCommand事件。

4.在RowCommand事件中删除记录。

5.重新绑定gridview。


protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
       {
           int id = Convert.ToInt16(GridView1.DataKeys[e.NewEditIndex].Values[0].ToString());

           cnn.Open();
           SqlCommand cmd = new SqlCommand("delete from call_details where id=" + id, cnn);
           int result = cmd.ExecuteNonQuery();
           cnn.Close();

           GridView1.DataBind();



this code will give me error like:Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

i have not use template field.then what is solution.

解决方案

//Please Try this code for your help....

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{

GridView1.EditIndex = e.NewEditIndex;

//yourControlName it''sInstance =
//(yourControlName)//(GridView1.Rows[GridView1.EditIndex].FindControl("yourContr//olId"));

//For Example

Label lbl = (Label)(GridView1.Rows[GridView1.EditIndex].FindControl("lblId"));

int id = convert.ToInt16(lbl.Text);

cnn.Open();
SqlCommand cmd = new SqlCommand("delete from call_details where id=" + id, cnn);
int result = cmd.ExecuteNonQuery();
cnn.Close();

GridView1.DataBind();


1. Add a button inside itemtemplate.
2. Use CommandArgument to pass the id of record to be deleted.
3. Add RowCommand event for the gridview.
4. In RowCommand event delete the record.
5. Re-Bind the gridview.


这篇关于如何进行行编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆