problems with GridView SelectedRow

Last post 06-27-2009 6:16 AM by b0oNz. 4 replies.

Sort Posts:

  • problems with GridView SelectedRow

    06-27-2009, 4:44 AM
    • Member
      6 point Member
    • b0oNz
    • Member since 06-26-2009, 4:45 AM
    • Posts 7

    Hi I'm not really good at programming thus I need some guidance from you experts.

    I'm using Visual Studio 2005, asp.net, C#.

    I wanted to add things into my database when the user clicks the add button that is created in the item template field of the GridView control. This is how my item template field looks like:

    <asp:TemplateField ShowHeader="False">
                                <ItemTemplate>
                                    <asp:Button ID="AddBT" runat="server" CausesValidation="False"
                                        OnClick="AddBT_Click" Text="Add to Cart" CommandName="Delete" />
                                </ItemTemplate>
                                <ControlStyle Width="80px" />
                            </asp:TemplateField>

    <asp:TemplateField ShowHeader="False">

    <ItemTemplate>

    <asp:Button ID="AddBT" runat="server" CausesValidation="False" OnClick="AddBT_Click" Text="Add to Cart" />

    </ItemTemplate>

    <ControlStyle Width="80px" />

    </asp:TemplateField>

    So every line in the gridview will have the Add button.

    So on click it will run the codes that is on my .cs file and this is when I recieve this error "Object reference not set to an instance of an object." 

    I slowly elimate the code and found out that the source of the problem is this:

    string eID = GridView1.SelectedRow.Cells[2].Text;

    Apparently SelectedRow, SelectedIndex and SelectedDataKey etc will give me the same error message.

    Can anyone help me with this? Couldn't find my answer after Googling it for hours.

  • Re: problems with GridView SelectedRow

    06-27-2009, 5:23 AM
    • Star
      10,796 point Star
    • chintanpshah
    • Member since 11-19-2008, 5:39 AM
    • Ahmedabad
    • Posts 1,930
  • Re: problems with GridView SelectedRow

    06-27-2009, 5:32 AM
    Answer
    • Contributor
      2,367 point Contributor
    • akhhttar
    • Member since 02-14-2007, 8:17 AM
    • Pakistan - Lahore
    • Posts 352

    Hi,


    You are getting this error because SelectedRow property of GridView is NULL. If purpose is to get the 2nd Cell value of the grid view row for which Add button is clicked then use following code


    ((GridViewRow)((Button)sender).Parent.Parent).Cells[2].Text


    Thanks

    Muhammad Akhtar Shiekh

    Lets resolve the problem together.

    Please remember to mark the appropriate replies as answer after your question is solved, thanks

    My Blog
  • Re: problems with GridView SelectedRow

    06-27-2009, 5:40 AM
    Answer
    • All-Star
      60,891 point All-Star
    • anas
    • Member since 09-21-2006, 8:31 AM
    • Palestinian Territory, Occupied
    • Posts 6,865
    • Moderator

    b0oNz:
    Apparently SelectedRow, SelectedIndex and SelectedDataKey etc will give me the same error message.

    Because there is no row selected.The mentioned button doesn't select the row because it's command name is not set to "Select".

    I think the solution is to get the row in that holds the currently clicked button .

    In the AddBT_click event handler , you can get the Row like this:

            GridViewRow row = (sender as Button).NamingContainer as GridViewRow;
            // get the text of the 3'rd cell.
            // Note: if the 3'rd column in the GridView is set as hidden (visisble=false),any cell in that column will have an empty text.
            string eID = row.Cells[2].Text;


    Regards,

    Anas Ghanem | Blog

  • Re: problems with GridView SelectedRow

    06-27-2009, 6:16 AM
    • Member
      6 point Member
    • b0oNz
    • Member since 06-26-2009, 4:45 AM
    • Posts 7

     Hi

    Thanks for all the help, I had solved the problem thanks all to your experts help. Really appreciated.

    Stay Happy
    Boonz

Page 1 of 1 (5 items)