Click here to Skip to main content
15,897,226 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi all,

Kindly bear with my poor programming as I am new to .Net world.

Following is a aspx code for GridView which displays results of search done in the database by user.
XML
<div>
    <asp:GridView ID="gvwKeyList" runat="server" AutoGenerateColumns="False" EmptyDataText="No data available."
        onrowcommand="GridView_RowCommand"
        CssClass="table table-striped table-condensed" BorderStyle="None" AlternatingRowStyle-BackColor="#C0DC3B">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:Button ID="b1" runat="server" Text="Attach?" CommandName="Select" class="btn btn-primary" />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="key_id" HeaderText="Key Id" />
            <asp:BoundField DataField="key_no" HeaderText="Key No" />
            <asp:BoundField DataField="hook" HeaderText="Hook" />
            <asp:BoundField DataField="access_description" HeaderText="Accessible?" />
            <asp:BoundField DataField="key_type_description" HeaderText="Key Type" />
            <asp:BoundField DataField="key_location" HeaderText="Key Location" />
        </Columns>
        <SelectedRowStyle BackColor="LightCyan" ForeColor="DarkBlue" Font-Bold="true" />
    </asp:GridView>

This is working fine and I can easily display it. Here is a code for it.

protected void search_keys(object sender, EventArgs e)
{
    string key = txt_key_no.Text;
    string hook = txt_hook.Text;

    List<Key> lstKey = new List<Key>();

    key_dal objDAL = new key_dal();
    lstKey = objDAL.GetKeyListByKeyHook(key, hook);

    gvwKeyList.DataSource = lstKey;
    gvwKeyList.GridLines = GridLines.None;

    gvwKeyList.DataBind();
}

My problem starts when I want to get the value of "Key_id" when user clicks on "Attach?" button. No matter what method I have tried I am always getting an error. I cannot get the RowIndex. Here is the code for "GridView_RowCommand" method

protected void GridView_RowCommand(Object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "Select")
    {
        // Convert the row index stored in the CommandArgument
        // property to an Integer.
        int index = Convert.ToInt32(e.CommandArgument);

        // Get the last name of the selected author from the appropriate
        // cell in the GridView control.
        GridViewRow selectedRow = gvwKeyList.Rows[index];
        TableCell contactName = selectedRow.Cells[1];
        string contact = contactName.Text;

        // Display the selected text.
        Message.Text = "You selected " + contact + ".";
    }

}


Can somebody please please guide me my mistake. I don't mind if I have to abandon this way and take up something new and better if available.

Many thanks in advance

Sorry. Somebody pointed out correctly that what do I mean by "I am always getting an error". It was my last 4 hours of frustration which prevented me from assuming things. These are the
various things I tried but always got a null Exception error.

SQL
//int rowindex = gvwKeyList.SelectedRow.RowIndex;
    //string stVal = gvwKeyList.Rows[rowindex].Cells[1].ToString();

OR
    //int selectedrowindex = gvwKeyList.SelectedRow.RowIndex;
    //GridViewRow selectedRow = gvwKeyList.Rows[selectedrowindex];
    //string stVal = Convert.ToString(selectedRow.Cells[1].ToString());
OR
    //GridViewRow row = gvwKeyList.SelectedRow;

    //GridViewRow clik = ((Button)sender).NamingContainer as GridViewRow;
    //string stVal = clik.FindControl("key_id").ToString();
OR
    // Determine the index of the selected row.
    //int index = gvwKeyList.SelectedIndex;
    //int selected_key_id = Convert.ToInt32(gvwKeyList.DataKeys[index].Value);
    //int selected_key_id = Convert.ToInt16(gvwKeyList.SelectedRow.Cells[1].Text);

Message.Text = "The primary key value of the selected row is " + stVal + ".";


I finally abandoned them and went for the "protected void GridView_RowCommand" method which gave me a "Input string was not in a correct format" error. All I want is a value from the row where I click the button
Posted
Updated 4-Nov-13 20:42pm
v2
Comments
thatraja 5-Nov-13 2:10am    
/*I am always getting an error*/

what's that? always include that in your questions.
Member 8430279 5-Nov-13 4:34am    
Hi,

I have updated my question with the error. Sorry about earlier message
JoCodes 5-Nov-13 2:17am    
Can you specify whats the error your are getting?
Member 8430279 5-Nov-13 4:34am    
Hi,

I have updated my question with the error. Sorry about earlier message

1 solution

So it seems that your main problem is "Failed to get the Row index in OnRow Command Event of the Gridview" .. Right?

if it is so, than following links can surely solve your problem.

Do have a look at .


Get Row Index on Asp.net Rowcommand event

AND

row index in gridview rowCommand

i hope you will be easily able to get the key_id after getting the row index, Though incase of any further issue, Do update your question, and Leave a comment Please.


:)
 
Share this answer
 
Comments
Member 8430279 5-Nov-13 4:46am    
Thank you Thank you Thank you Thank you Thank you so much.

You have save much frustration for me
VICK 5-Nov-13 4:54am    
Glad to see your problem solved. :)
thatraja 5-Nov-13 5:14am    
I remember you have posted answer before OP's update(question). Don't guess always, sometimes your answer will be silly. In past I did the same bunch of times :)

Still 5!
VICK 5-Nov-13 5:58am    
U r right.. But some times being silly(having assumption), becomes beneficial for some one. as in this case. :)

Any ways Thanks for encouraging and guiding. :)
thatraja 5-Nov-13 6:05am    
Here I did the same mistake :D

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900