Click here to Skip to main content
15,924,935 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I'm trying to extract the whole row value of gridview on a particular row's button click but its saya ---
Additional information: Index was out of range. Must be non-negative and less than the size of the collection


What I have tried:

<asp:GridView ID="gdvCampaign" runat="server" ShowFooter="true" AutoGenerateColumns="false" OnRowCommand="gdvCampaign_RowCommand" class="col-md-12 table-bordered table-striped table-condensed cf" OnRowDeleting="gdvCampaign_RowDeleting">
                                                    <Columns>
                                                        <asp:BoundField DataField="RowNumber" HeaderText="Sl No." />
                                                        <asp:TemplateField HeaderText="Category">
                                                            <ItemTemplate>
                                                                <asp:DropDownList ID="ddlCategory" runat="server" class="form-control" AppendDataBoundItems="true">
                                                                    <asp:ListItem Value="-1">Select</asp:ListItem>
                                                                </asp:DropDownList>
                                                            </ItemTemplate>
                                                            <FooterStyle HorizontalAlign="Center" />
                                                            <FooterTemplate>
                                                                <asp:Button ID="ButtonAdd" runat="server" Text="Add" class="btn btn-info btn-sm" OnClick="ButtonAdd_Click" />
                                                            </FooterTemplate>
                                                        </asp:TemplateField>
                                                        <asp:TemplateField HeaderText="Channel">
                                                            <ItemTemplate>
                                                                <asp:DropDownList ID="ddlChannel" runat="server" class="form-control" AppendDataBoundItems="true">
                                                                    <asp:ListItem Value="-1">Select</asp:ListItem>
                                                                </asp:DropDownList>
                                                            </ItemTemplate>
                                                        </asp:TemplateField>
                                                        <asp:TemplateField HeaderText="Click Action">
                                                            <ItemTemplate>
                                                                <asp:Button ID="btnCollateralPopup" runat="server" CommandArgument='<%#Eval("RowNumber") %>' class="btn btn-info btn-sm" Text="Collateral" />
                                                            </ItemTemplate>
                                                        </asp:TemplateField>
                                                        <asp:CommandField HeaderText="Action Taken" ShowDeleteButton="true" />
                                                    </Columns>
                                                </asp:GridView>




protected void gdvCampaign_RowCommand(object sender, GridViewCommandEventArgs e)
    {
            int rowID = Convert.ToInt32(e.CommandArgument);
    
            campaignModel.tempParentID = rowID;
    
            campaignModel.categoryID = Convert.ToInt32(((DropDownList)gdvCampaign.Rows[rowID].FindControl("ddlCategory")).SelectedValue);  //Error
            campaignModel.categoryName = ((DropDownList)gdvCampaign.Rows[rowID].FindControl("ddlCategory")).SelectedItem.Text;
    
            campaignModel.channelID = Convert.ToInt32(((DropDownList)gdvCampaign.Rows[rowID].FindControl("ddlChannel")).SelectedValue);
    	campaignModel.channelName = ((DropDownList)gdvCampaign.Rows[rowID].FindControl("ddlChannel")).SelectedItem.Text;
           
            dtCampaign.Rows.Add(campaignModel.tempParentID, campaignModel.categoryID, campaignModel.channelID);
    	ViewState["tempCampaignView"] = dtCampaign;
    }
Posted
Updated 12-Jun-17 12:12pm
Comments
ZurdoDev 12-Jun-17 16:41pm    
Very simple.

1. Pay attention to what line of code caused the error.
2. Something in that line where you are trying to access via index) does not exist. It's like trying to access the 10th item in a collection that has 5 items.
Bit2 Developer 12-Jun-17 17:16pm    
Thanks Ryan....!
Your point actually works for me.
ZurdoDev 13-Jun-17 7:19am    
Glad to hear it.

At a guess - and that's all it can be - RowID is not an index value into the gridview - ID values do not always (or indeed often) run from 0 to n-1 where n is the number of values, because good practice means that ID values are not reused.

But we can only guess, because we don't have any access to your data which is fundamental to finding the problem and then fixing it.
So, its going to be up to you to use the debugger and find out what exactly is going on.
Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 
Comments
Bit2 Developer 12-Jun-17 17:15pm    
The concept 0 to (n - 1) works for me....!
Thank You ('_')
OriginalGriff 12-Jun-17 17:42pm    
You're welcome!
With the help of debugger, you will be able to see the value of variables at position of error.

There is a tool that allow you to see what your code is doing, its name is debugger.
It is a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 

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