?
Solved

Get the DataKey values in GridView RowCommand event

Posted on 2010-08-15
2
Medium Priority
?
5,419 Views
Last Modified: 2012-05-10
Hi!

I have a GridView with a Delete button (not default delete button, I'm using command name).

GridView_RowCommand {

if (e.CommandName.CompareTo("apagarNoticia") == 0)    {
newsid = GridView3.DataKeys[Convert.ToInt32(e.CommandArgument)]["NewsID"].ToString();
lang = GridView3.DataKeys[Convert.ToInt32(e.CommandArgument)]["LanguageID"].ToString();
}

Open in new window


<asp:Button ID="ButtonApagarNoticia" runat="server"
CausesValidation="false" CommandName="apagarNoticia" Text="Apagar" />

Open in new window


This delete button has a javascript confirmation.

        protected void GridView3_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Button b = (Button)e.Row.FindControl("ButtonApagarNoticia");
                b.Attributes.Add("onclick", "javascript:return " + "confirm('Are you sure you want to delete the news " + DataBinder.Eval(e.Row.DataItem, "NewsID") + " ?')");
            }
        }

Open in new window

Everything worked well until I changed the delete button (not the default, I'm using command name) to template and add javascript confirmation.

Error Input string was not in a correct format on this line:
newsid = GridView3.DataKeys[Convert.ToInt32(e.CommandArgument)]["NewsID"].ToString();

In other pages, I just used the CommandArgument
<asp:Button ID="ButtonDeleteSim" runat="server"
CausesValidation="false" CommandName="apagarSimulacao"
CommandArgument='<%# Bind("NewsID") %>' Text="Apagar" />

Open in new window


string newsid = e.CommandArgument.ToString();

Open in new window


But I need two fields this time.

newsid = GridView3.DataKeys[Convert.ToInt32(e.CommandArgument)]["NewsID"].ToString();
lang = GridView3.DataKeys[Convert.ToInt32(e.CommandArgument)]["LanguageID"].ToString();

Open in new window


How can I solve this?

Thanks in advance!
0
Comment
Question by:calypsoworld
[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 Comments
 
LVL 12

Accepted Solution

by:
Ramkisan Jagtap earned 2000 total points
ID: 33443550
try the following code....

protected void GridView3_RowDataBound(object sender, GridViewRowEventArgs e)
{
      if (e.Row.RowType != DataControlRowType.Header && e.Row.RowType != DataControlRowType.Footer && e.Row.RowType !=DataControlRowType.Pager)
      {
            Button btnDeleteSim =new Button();
            btnDeleteSim = (Button)e.Row.FindControl("ButtonDeleteSim");
            btnDeleteSim.CommandArgument= e.Row.RowIndex.ToString();
      }
}

Then get the row index in

protected void GridView3_RowCommand(object sender, GridViewCommandEventArgs e)
{

      int RowIndex_ = int.Parse(e.CommandArgument.ToString());
      newsid = GridView3.DataKeys[RowIndex_]["NewsID"].ToString();
      lang = GridView3.DataKeys[RowIndex_]["LanguageID"].ToString();

}
0
 

Author Closing Comment

by:calypsoworld
ID: 33444715
Awesome!
Thank you so much!
0

Featured Post

Independent Software Vendors: 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

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses
Course of the Month9 days, 19 hours left to enroll

762 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