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:
I am having a problem pulling the values from my GridView in the OnRowUpdating method. Here is my GridView1:

ASP.NET
<asp:gridview id="GridView1" runat="server" allowpaging="True" 
="" allowsorting="True" autogenerateeditbutton="True" autogeneratedeletebutton="True" alternatingrowstyle-backcolor="#99CCFF" datasourceid="LinqDataSource1" autogeneratecolumns="False" datakeynames="Month_End_Date" onrowdatabound="OnRowDataBound" onrowupdating="OnRowUpdating">
                 <alternatingrowstyle backcolor="#99CCFF">
                 <columns>
                     <asp:boundfield datafield="Month_End_Date" headertext="Month_End_Date" 
="" readonly="True" sortexpression="Month_End_Date">
                     <asp:boundfield datafield="User_Id" headertext="User_Id" readonly="True" 
="" sortexpression="User_Id">
                     <asp:boundfield datafield="Lock_Modified" headertext="Lock_Modified" 
="" readonly="True" sortexpression="Lock_Modified">
                     <asp:boundfield datafield="Lock" headertext="Lock" readonly="True" 
="" sortexpression="Lock">
                    <asp:templatefield headertext="LockYes">
                        <itemtemplate>
                            <asp:label id="lblLockYes" runat="server" text="<%# Bind("Lock") %>">
                        
                        <edititemtemplate>
                            <asp:label id="lblLockYes" runat="server" text="<%# Bind("Lock")%>" visible="true">
                            <asp:dropdownlist 
="" id="ddlLockYes" runat="server" appenddatabounditems="true" datatextfield="Lock" datavaluefield="Lock">


Here is my code-behind. I need to pull the values for Month_End_Date and Lock.
I get this error message.
Unable to cast object of type 'System.Web.UI.WebControls.DataControlLinkButton' to type 'System.Web.UI.WebControls.TextBox'.

C#
protected void OnRowUpdating(object sender, GridViewUpdateEventArgs e)
        {

  
            DateTime monthend = DateTime.Parse(((TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text);
            string drop = dropDownListSelItem;
            //int age = int.Parse(((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text);

            //SqlConnection Conn = new SqlConnection(MyGlobals.ConnString);
            //Conn.Open();

            //SqlCommand cmd = new SqlCommand("UPDATE ct_lock SET  lock=@drop where month_end_date=@monthend ", Conn);
            //cmd.Parameters.Add(new SqlParameter("@monthend", monthend));
            //cmd.Parameters.Add(new SqlParameter("@drop", drop));
            //cmd.Parameters.Add(new SqlParameter("@age", age));

            //Conn.Open();
            //cmd.ExecuteNonQuery();
            //Conn.Close();

            //GridView1.EditIndex = -1;
            //fill();





            GridView1.EditIndex = -1;
            GridView1.DataBind();
        }


What I have tried:

I need to pull the values for Month_End_Date and Lock.
I get this error message. Unable to cast object of type 'System.Web.UI.WebControls.DataControlLinkButton' to type 'System.Web.UI.WebControls.TextBox'.
Posted
Updated 7-Oct-19 13:49pm
v2

GridView1.Rows[e.RowIndex].Cells[0].Controls[0] does not reference a TextBox, as you think it should, it references a DataControlLinkButton instead.

Put a breakpoint on the line DateTime monthend = ..., press F5, and watch for the structure of the datagridview; then correct cells/controls indices accordingly.
 
Share this answer
 
Comments
Jeff Franken 7-Oct-19 14:46pm    
I am somewhat of a newbie to this. I put the breakpoint in. Run it. Hover over Cells[0]. It offers the Cells. I expand. It offers SyncRoot and Non-public members. How do I see the values in the cells? and the values in the Controls?
phil.o 7-Oct-19 15:29pm    
I have searched for some images of the debug view of an asp gridview, but could not find anything useful.
So I will just stick with general remarks.
Cells[0] is just the first cell of the current row. You shoud also have Cells[1], Cells[2], etc..., in fact as much cells as you have defined columns for the control.
For each of the cells, you should have a Controls collection which references the controls in specific cell.
I'm afraid I wont be able to be of much help here; the only thing I can say now, is that you should find the right index into the Controls or Cells collection: maybe the textbox is in Cells[0].Controls[1] (just an example).
got it going with the following code:

protected void OnRowUpdating(object sender, GridViewUpdateEventArgs e)
{


DateTime monthend = DateTime.Parse(GridView1.Rows[e.RowIndex].Cells[1].Text.ToString());

string drop = dropDownListSelItem;


SqlConnection Conn = new SqlConnection(MyGlobals.ConnString);
Conn.Open();

SqlCommand cmd = new SqlCommand("UPDATE ct_lock SET lock=@drop where month_end_date=@monthend ", Conn);
cmd.Parameters.Add(new SqlParameter("@monthend", monthend));
cmd.Parameters.Add(new SqlParameter("@drop", drop));

//Conn.Open();
cmd.ExecuteNonQuery();
Conn.Close();

//GridView1.EditIndex = -1;
//fill();





GridView1.EditIndex = -1;
GridView1.DataBind();
}

was able to find the correct cells by putting the expression into the watch area while debugging
 
Share this answer
 
Comments
phil.o 8-Oct-19 1:41am    
Glad you managed to solve it.

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