2011-03-18 59 views
3

我想在Gridview中刪除一個特定的行。所以我需要ID,我怎樣才能得到在GridView_RowDeleting事件行的ID?如何使用C#在GridView_RowDeleting事件中從GridView中獲取ID?

這裏是在GridView的列清單,

<Columns> 
    <asp:TemplateField HeaderText="S.No." ControlStyle-Width="100%" ItemStyle-Width="30px"> 
    <ItemTemplate> 
     <asp:Label ID="lblSrno" runat="server" Text='' 
     <%# Container.DataItemIndex + 1 %>'></asp:Label> 
    </ItemTemplate> 
    <ControlStyle Width="100%" /> 
    <ItemStyle Width="30px" /> 
    </asp:TemplateField> 
    <asp:TemplateField Visible="false"> 
    <ItemTemplate> 
     <asp:Label runat="server" ID="lblDrawingID" Text='' 
     <%# Bind("DrawingID")%>'></asp:Label> 
    </ItemTemplate> 
    </asp:TemplateField> 
    <asp:BoundField DataField="CustDrawingNbr" HeaderText="Customer Drawing No." /> 
    <asp:TemplateField HeaderText="Status" ControlStyle-Width="100px" ItemStyle-Width="100px"> 
    <ItemTemplate> 
     <asp:DropDownList ID="ddlStatus" runat="server" Width="100px" Enabled="false"> 
     </asp:DropDownList> 
    </ItemTemplate> 
    <ControlStyle Width="100px" /> 
    <ItemStyle Width="100px" /> 
    </asp:TemplateField> 

    <asp:TemplateField Visible="false"> 
    <ItemTemplate> 
     <asp:Label runat="server" ID="lblDrawingPGMAStatusID" Text='' 
     <%# Bind("StatusID")%>'></asp:Label> 
    </ItemTemplate> 
    </asp:TemplateField> 
    <asp:TemplateField Visible="false"> 
    <ItemTemplate> 
     <asp:Label runat="server" ID="lblDrawingPGMAID" Text='' 
     <%# Bind("PGMAID")%>'></asp:Label> 
    </ItemTemplate> 
    </asp:TemplateField> 
    <asp:CommandField ShowDeleteButton="true" HeaderText="Action" ItemStyle-HorizontalAlign="Center" 
     ItemStyle-VerticalAlign="Middle"> 
    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" /> 
    </asp:CommandField> 
</Columns> 

這裏我填一個數據表與此GridView控件。 DataTable列是DrawingID,CustDrawingNbr,JobOrderID,StatusID,PGMAID,SeqNo。 所以我想單擊刪除按鈕時獲取DrawingID。

到目前爲止, 我試過了以下的東西。但沒有任何結果。

  1. 標籤lblDraw =(標籤)gvApplicablePGMASearch.Rows [e.RowIndex] .Cells [2] .FindControl( 「lblDrawingID」); int drawid = Convert.ToInt32(lblDraw.Text);

  2. 串drawid = gvApplicablePGMASearch.DataKeys [e.RowIndex] .Value.ToString();

  3. string id = gvApplicablePGMASearch.SelectedDataKey.Value.ToString();

但是以上都沒有給出預期的結果。

+0

在我看來,身份證應該與'刪除'事件來.. – 2011-03-18 11:10:43

+0

@Bugai。我不能得到你。請精心準備。 – thevan 2011-03-18 11:15:14

+0

爲什麼在TemplateFields中使用所有這些標籤,爲什麼不使用asp:BoundField並設置DataField?如果您只是將它們用於隱藏ID,您可能更願意僅在DataKeyNames中列出ID,例如的DataKeyNames = 「DrawingID,StatusID,PGMAID」。 – bgs264 2011-03-18 11:20:22

回答

3

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdeleting.aspx

您可以確定被單擊的行的索引。獲得該索引後,訪問該項目的DataKey。

DrawingID = gvResults.DataKeys[e.RowIndex].Value 

編輯如果你有多個的DataKeyNames那麼它會是這樣

DrawingID = gv.DataKeys[e.RowIndex].Item("DrawingID").ToString() 

只要確保你已經設置在GridView上設置的DataKeyNames,這應該工作。

<asp:GridView ID="gvResults" runat="server" AutoGenerateColumns="false" DataKeyNames="DrawingID"> 
+0

非常感謝你。它的工作正常。 – thevan 2011-03-18 11:37:32

+0

@thevan不客氣:D – bgs264 2011-03-18 11:49:36

+1

DataKeys不是函數。我認爲你需要方括號.. – NickG 2015-02-02 12:39:21

相關問題