繁体   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