简体   繁体   中英

getting the value from gridview

I have a form with Gridview and two buttons one for ADD and other for SHOW IMAGES

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

The above code is working when I used it on the ADD button CLICK event. But when I used it on SHOW IMAGES CLICK event, it is giving error

There is no row at position 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.

the "name" of the CommandName is VERY, but VERY VERY VERY important here.

If you use "Select", or "Edit"? then the grid view WILL FIRE: (GridView1_SelectedIndexChanged)

However, if you use any custom name, then the GridView1_SelectedIndexChanged will NOT fire, but the GridView1_RowCommand WILL fire! And keep in mind that when this event fires with a custom CommandName, then SelectedIndex does NOT fire.

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.

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. 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? The SelectedIndex event fires, and VERY OFTEN we don't care which button - since often it is only one button.

If you start to drop in extra buttons, then shift the code over to GridView1_RowCommand. That way you can pick up the CommandName for custom code.

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.

The row code in C# would be say this:

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

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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