Solved

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

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Link to post behind image slider 1 51
API Soap Calls 4 87
Stupid git question 2 58
Accurate Analytics (Not Google) 2 5
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
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.
This video teaches users how to migrate an existing Wordpress website to a new domain.
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…

919 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

18 Experts available now in Live!

Get 1:1 Help Now