Solved

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

Posted on 2006-06-22
4
706 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
  • 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Developer portfolios can be a bit of an enigma—how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, it’s also a statement of how you work.
FAQ pages provide a simple way for you to supply and for customers to find answers to the most common questions about your company. Here are six reasons why your company website should have a FAQ page
This video teaches users how to migrate an existing Wordpress website to a new domain.
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

777 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