2013-04-04 30 views
1

我已经使用gridview中的templete字段删除按钮,所以当我点击gridview中的任何删除按钮以删除一行时,它会使用该选定行的cells[0]值给删除行索引。我怎么能做任何人有一个想法....?如何使用删除按钮获得所选行索引和所选行的单元值

protected void GridView3_RowCommand(object sender, GridViewCommandEventArgs e) 
{ 
     int index = Convert.ToInt32(e.CommandArgument); 
     int grdid = Int32.Parse(GridView3.Rows[index].Cells[0].Text); 

     IndiesProductDataContext ip = new IndiesProductDataContext(); 
     ip.iLinks.DeleteAllOnSubmit(ip.iLinks.Where(c => c.ProductID == grdid)); 
     ip.SubmitChanges(); 
} 

我在GridView中删除行使用行命令歌厅指数,但它不工作

回答

0

你不能干这种方式,但你将不得不使用的按钮单击事件选择ID,然后使用该ID作为第二个不同的删除查询的参数。

+0

但我要删除同一选定行,也希望删除的另一个表值形式选择中行的主单元格值... – dzsinh 2013-04-04 11:06:26

+0

是的,你可以写2在不同的sql命令变量的同一个sql连接下为你的2个不同的记录从不同的表中删除查询。 – 2013-04-04 11:10:00

5

您可以设置按钮

在.aspx的的CommandArgument属性:

<asp:Button ID="btnDeleteContact" CommandName="DeleteContact" 
CommandArgument="<%# Eval("ContactID") %>" runat="server" >Delete</asp:Button> 

在GridView控件的_RowCommand事件:

If e.CommandName = "DeleteContact" Then 
    Dim objContacts As New CContacts 

    objContacts.ContactID = e.CommandArgument 
    objContacts.DomainID = Session("DomainID") 
    Dim strError As String = objContacts.DelContact() 

End If 

希望它有助于

+1

Chintan @可能他正在使用C#和asp.net – 2013-04-04 11:53:05

1

我找到了完美的解决方案这个问题的...

protected void GridView3_RowCommand(object sender, GridViewCommandEventArgs e) 
    { 

     GridViewRow gvr = (GridViewRow)(((Button)e.CommandSource).NamingContainer); 
     int RowIndex = gvr.RowIndex; 
     int ci = Int32.Parse(GridView3.Rows[RowIndex].Cells[1].Text); 

     ProductDataContext ip = new ProductDataContext(); 
     ip.iLinks.DeleteAllOnSubmit(ip.iLinks.Where(c => c.PID == ci)); 
     ip.SubmitChanges(); 

    } 

这个代码我的模块中工作正常...

相关问题