• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5509
  • Last Modified:

Get the DataKey values in GridView RowCommand event

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
calypsoworld
Asked:
calypsoworld
1 Solution
 
Ramkisan JagtapLead DeveloperCommented:
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
 
calypsoworldAuthor Commented:
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!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now