Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The following is an excerpt from my row event handler for a GridView control. Ignore the insecure SQL; I'll parameterize it before it goes live.

if (e.CommandName == "cmdEdit")
            {
                // Convert the row index of the row being edited into an int.
                Int32 index = Convert.ToInt32(e.CommandArgument);
                
                // Set the gridview's EditIndex to the one being edited. 
                gvStudents.EditIndex = index;

                Int32 id = Convert.ToInt32(gvStudents.DataKeys[index].Value);
                
                dsfname.SelectCommand = @"SELECT firstname FROM [a server] WHERE firstname IS NOT NULL AND idnum =" + id;
                dslname.SelectCommand = @"SELECT lastname FROM [a server] WHERE lastname IS NOT NULL AND idnum =" + id;
                dshours.SelectCommand = @"SELECT hours FROM [a server] WHERE hours IS NOT NULL AND idnum =" + id;
                dsemail.SelectCommand = @"SELECT email FROM [a server] WHERE email IS NOT NULL AND idnum =" + id;

                dsfname.DataBind();
                dslname.DataBind();
                dshours.DataBind();
                dsemail.DataBind();
                dsStudents.DataBind();

                lblWarning.Text = ((GridViewRow)gvStudents.Rows[gvStudents.EditIndex]).RowState.ToString();
                lblWarning.Visible = true;
                lblWarning.Text += " " + index;
                
                showGrid();

                //((DropDownList)gvStudents.Rows[gvStudents.EditIndex].FindControl("ddlFirstName")).Items.Insert(0, new ListItem("", ""));

            }


This runs when the user clicks on a button to begin an edit. As is, I'm displaying a label for testing purposes, lblWarning, and populating it with the row state of the selected row and its index. The row state is "normal", which seems to mean that the GridView's EditIndex hasn't been updated. I don't know why.

The commented out code at the bottom attempts to add an item to one of the DropDownLists. It's commented out because it causes an exception that says I'm referencing an object that doesn't exist. That is, it can't find the DropDownList.

I'm new to ASP, and don't really understand data binding, but it seems this issue is related to it. What do I need to do for that change of the EditIndex to take effect?

What I have tried:

Tried using validation. The code in the inner if block never fires.

if (gvStudents.Rows[gvStudents.EditIndex].RowType == DataControlRowType.DataRow)
                {
                    if ((gvStudents.Rows[gvStudents.EditIndex].RowState & DataControlRowState.Edit) > 0)
                    {
                        ((DropDownList)gvStudents.Rows[gvStudents.EditIndex].FindControl("ddlFirstName")).Items.Add("test item");
                    }
                    
                }
Posted
Comments
Richard Deeming 29-Jun-18 12:26pm    
Setting the EditIndex doesn't have any effect until you re-bind the grid. Presumably that's what your showGrid function is doing?

Try moving the code that updates the label after the call to showGrid.
OlivesForDinner 29-Jun-18 12:35pm    
Whoops, I should've explained showGrid(). Currently all that does is set the GridView's datasource control's SelectCommand to what's in the search box. I'm sure that's the wrong way of going about this, but that's the only obvious way I found to populate the GridView.

If I bind the GridView immediately after setting the EditIndex I get:

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

That seems weird because as established before, index at that point is 0, which should be fine.
Richard Deeming 29-Jun-18 12:41pm    
In that case, you'll probably need a call to gvStudents.DataBind() immediately after the dsStudents.DataBind() call.
OlivesForDinner 29-Jun-18 12:43pm    
Got the same error. It seems like something's fundamentally wrong in how I've done the binding.
Richard Deeming 29-Jun-18 12:47pm    
That's odd. Which line throws the exception?

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