?
Solved

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

Posted on 2006-06-22
4
Medium Priority
?
713 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 2000 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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

CTAs encourage people to do something specific to show interest in your company, product or service. Keep reading to learn why CTAs should always be thought of as extremely important, albeit small, sections of websites.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

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