2013-04-04 31 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(); 

    } 

這個代碼我的模塊中工作正常...

相關問題