簡體   English   中英

從 gridview 獲取值

[英]getting the value from gridview

我有一個帶有 Gridview 的表單和兩個按鈕,一個用於添加,另一個用於顯示圖像

int Id = Convert.ToInt32(dataGridView1.CurrentRow.Cells[0].Value);

當我在 ADD 按鈕 CLICK 事件上使用它時,上面的代碼正在工作。 但是當我在 SHOW IMAGES CLICK 事件上使用它時,它給出了錯誤

位置 0 處沒有行

當您在 gridview(甚至是 listview)上放置一個按鈕時,內置按鈕,例如添加或編輯等,它們有兩個屬性。

CommandName 的“名稱”非常重要,但在這里非常非常重要。

如果您使用“選擇”或“編輯”? 然后網格視圖將觸發:(GridView1_SelectedIndexChanged)

但是,如果您使用任何自定義名稱,則 GridView1_SelectedIndexChanged 不會觸發,但 GridView1_RowCommand 會觸發! 請記住,當此事件使用自定義 CommandName 觸發時,SelectedIndex 不會觸發。

因此,您可以放入這樣的按鈕:

<asp:GridView ID="GridView1" runat="server">
     <Columns>
       <asp:TemplateField>
           <ItemTemplate>
               <asp:Button runat="server" Text="Select" 
                        CommandName="ViewPictures"
                        CommandArgument='<%# Eval("ID") %>' 
                />
           </ItemTemplate>
        </asp:TemplateField>
      </Columns>
  </asp:GridView>

注意如上所述,使用按鈕(或鏈接按鈕 - 無關緊要),然后因為我們的 CommandName 不是內置的(選擇、編輯等),那么行命令事件將觸發。

所以,我們的代碼可以是這樣的:

Protected Sub GridView1_RowCommand(sender As Object, e As GridViewCommandEventArgs) Handles GridView1.RowCommand

    If e.CommandName = "ViewPictures" Then

        Debug.Print("row pk value = " & e.CommandArgument.ToString)

        ' grid view Selected index event will NOT fire - index will NOT change.
        ' we do have PK above, so we can run code on PK value - do what we want based on PK

        ' Now if we really do need the whole row?
        ' then this from the bottom of the ocean horror story from develoeprs with
        ' damaged brains will get you the row

        Dim gvRow As GridViewRow = CType(CType(e.CommandSource, Control).NamingContainer, GridViewRow)
        ' so now, you can go say:
        Debug.Print("Row from grid seleted index = " & gvRow.RowIndex)


    End if
End Sub

請注意 PK id(或您傳遞的任何內容)應該如何就足夠了。 但是,上面的代碼還包括重擊代碼,如果您願意,它也會為您提供整行。

在許多情況下,我經常只放一個按鈕,設置 commandName="Select",並且行上只有一個按鈕 - 誰在乎? SelectedIndex 事件觸發,我們通常不關心哪個按鈕 - 因為通常它只有一個按鈕。

如果您開始添加額外的按鈕,則將代碼轉移到 GridView1_RowCommand。 這樣您就可以為自定義代碼選擇 CommandName。

請記住,RowCommand 允許您傳遞一個值,如果您確實需要該行,那么示例代碼將允許您在需要更多行時選擇該行,然后說一個傳遞的值。

C# 中的行代碼是這樣的:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "ViewPictures")
{
    Debug.Print("row pk value = " + e.CommandArgument.ToString);

    // grid view Selected index event will NOT fire - index will NOT change.
    // we do have PK above, so we can run code on PK value - do what we want based on PK

    // Now if we really do need the whole row?
    // then this from the bottom of the ocean horror story from develoeprs with
    // damaged brains will get you the row

    GridViewRow gvRow = (GridViewRow)(Control)e.CommandSource.NamingContainer;
    // so now, you can go say:
    Debug.Print("Row from grid seleted index = " + gvRow.RowIndex);
}
}

GridView 的事件

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM