Solved

Get datakey of selected row for function invoked by  buttonfield in a gridview

Posted on 2006-06-22
4
707 Views
Last Modified: 2010-08-05
I want to run a sub when clicking a button in my grid. I want to use the datakey in the function.
I'm doing this...

Protected Sub grvOutGoingTradeStatus_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)
        If e.CommandName = "SendEmailAcknow" Then
            Dim row As GridViewRow = grvOutGoingTradeStatus.Rows(CInt(e.CommandArgument))
            Dim id As Integer = grvOutGoingTradeStatus.DataKeys(row.RowIndex).Value
        End If

When it gets to Dim id as integer .... it throws an error saying 'index out of range. Must be non negative and less
than the size of the collection. Parameter name: Index'
 
How do I get the value of the datakey for the selected row, to use in my sub ??
0
Comment
Question by:gusdogs
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 1

Accepted Solution

by:
ZaDDaZ earned 500 total points
ID: 16966186
First, for the data keys to work, you must have ViewState enabled for the GridView.

It looks like you're just putting a button in a template column or using a ButtonField in the grid and not using the CommandField to show buttons like delete/edit/cancel, each of wich have their own event arg objects that all include the row index being addressed, you will need to set the command argument yourself.  You can do that in the .aspx page using binding (<asp:button runat="server" id="btnHitMe" CommandName="SendEmailAcknow" CommandArgument="<%# Container.DataItemIndex %>" Text="Send Email Ack"/>) or do it in codebehind if you would like in the OnDataBind event for the grid.  This will get you the index into the CommandArgument property.  However, if you would like to not use ViewState and make your page smaller, you could data bind the actual ID if you don't mind users being able to see them (<%# Eval("ItemId") %>).
0
 

Author Comment

by:gusdogs
ID: 16970570
Thanks for your thorough response, I really appreciate it.
Sorted my problem out.

PS: If you have a minute, can you briefly explain how to hide my button under certain conditions.
0
 
LVL 1

Expert Comment

by:ZaDDaZ
ID: 16970650
I typically do this in the RowDataBound event.  Forgive the C#, but I have not done VB in so long, I don't want to get this wrong.  It should be quite similar in VB:

private void gridView1_RowDataBound (object sender, GridViewRowEventArgs e)
{
  if(e.Row.RowType == DataControlRowType.DataRow)
  {
    DataRowView drv = (DataRowView) e.Row.DataItem;
    Button btn = e.Row.FindControl("btnName");
    btn.Visible = drv["ShouldBeVisible"];
  }
}

I assume you want to do it based on some data in the row that is more complicated than what I put, but you should get the picture.
0
 

Author Comment

by:gusdogs
ID: 16970702
Thank you so much for your help.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Any business that wants to seriously grow needs to keep the needs and desires of an international audience of their websites in mind. Making a website friendly to international users isn’t prohibitively expensive and can provide an incredible return…
An enjoyable and seamless user experience can go a long way on an eCommerce site. While a cohesive layout and engaging copy play roles in creating a positive user experience, some sites neglect aspects that seem marginal but in actuality prove very …
This video teaches users how to migrate an existing Wordpress website to a new domain.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question