Solved

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

Posted on 2006-06-22
4
704 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you so much for your help.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Read about why website design really matters in today's demanding market.
The viewer will learn how to dynamically set the form action using jQuery.
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now