Solved

Get the DataKey values in GridView RowCommand event

Posted on 2010-08-15
2
5,346 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 500 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Technology Resume 7 88
Aspx calendar pop up control 3 37
CSS question 16 62
Unprotect Visio drawing page in C# 4 18
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 …
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Suggested Courses

734 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