简体   繁体   English

从 gridview 获取值

[英]getting the value from gridview

I have a form with Gridview and two buttons one for ADD and other for SHOW IMAGES我有一个带有 Gridview 的表单和两个按钮,一个用于添加,另一个用于显示图像

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

The above code is working when I used it on the ADD button CLICK event.当我在 ADD 按钮 CLICK 事件上使用它时,上面的代码正在工作。 But when I used it on SHOW IMAGES CLICK event, it is giving error但是当我在 SHOW IMAGES CLICK 事件上使用它时,它给出了错误

There is no row at position 0位置 0 处没有行

When you drop a button on a gridview (or even a listview), the built in buttons such as add, or edit etc. they have two attributes.当您在 gridview(甚至是 listview)上放置一个按钮时,内置按钮,例如添加或编辑等,它们有两个属性。

the "name" of the CommandName is VERY, but VERY VERY VERY important here. CommandName 的“名称”非常重要,但在这里非常非常重要。

If you use "Select", or "Edit"?如果您使用“选择”或“编辑”? then the grid view WILL FIRE: (GridView1_SelectedIndexChanged)然后网格视图将触发:(GridView1_SelectedIndexChanged)

However, if you use any custom name, then the GridView1_SelectedIndexChanged will NOT fire, but the GridView1_RowCommand WILL fire!但是,如果您使用任何自定义名称,则 GridView1_SelectedIndexChanged 不会触发,但 GridView1_RowCommand 会触发! And keep in mind that when this event fires with a custom CommandName, then SelectedIndex does NOT fire.请记住,当此事件使用自定义 CommandName 触发时,SelectedIndex 不会触发。

So, you can drop in a button like this:因此,您可以放入这样的按钮:

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

Note as stated, with a button (or link button - don't matter), then because our CommandName is NOT a built in one (Select, Edit etc.), then the row command event will fire.注意如上所述,使用按钮(或链接按钮 - 无关紧要),然后因为我们的 CommandName 不是内置的(选择、编辑等),那么行命令事件将触发。

So, our code can be this:所以,我们的代码可以是这样的:

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

Note how the PK id (or whatever you pass) should suffice.请注意 PK id(或您传递的任何内容)应该如何就足够了。 However, the above code also includes the whack code that will also give you the whole row if you wish.但是,上面的代码还包括重击代码,如果您愿意,它也会为您提供整行。

In many cases, I often just drop a button, set commandName="Select", and with only one button on the row - who cares?在许多情况下,我经常只放一个按钮,设置 commandName="Select",并且行上只有一个按钮 - 谁在乎? The SelectedIndex event fires, and VERY OFTEN we don't care which button - since often it is only one button. SelectedIndex 事件触发,我们通常不关心哪个按钮 - 因为通常它只有一个按钮。

If you start to drop in extra buttons, then shift the code over to GridView1_RowCommand.如果您开始添加额外的按钮,则将代码转移到 GridView1_RowCommand。 That way you can pick up the CommandName for custom code.这样您就可以为自定义代码选择 CommandName。

Just keep in mind that RowCommand allows you to pass a value, and if you really need the row, then the sample code will allow you to pick up the row if you need more then say one value passed.请记住,RowCommand 允许您传递一个值,如果您确实需要该行,那么示例代码将允许您在需要更多行时选择该行,然后说一个传递的值。

The row code in C# would be say this: 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);
}
}

Events for GridView GridView 的事件

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM