Click here to Skip to main content
15,897,291 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a two buttons edit and submit in my gridview.What i want is to place a textbox on second column quantity when i clicked edit so that i can change the value in the textbox.However it is not working out.Below is my html and C# code for the same.Any help would be appreciated.

<asp:Panel ID="pnlOrderLineInfo"
style="Z-INDEX: 101; LEFT: 29px; POSITION: absolute; TOP: 243px"
runat="server" Height="102px" Width="327px">



<asp:Image ID="Image5" runat="server" Height="30px"
ImageUrl="~/Images/scanitem.png" />
<asp:TextBox id="txtOrderLineNo" Runat="server"
ontextchanged="txtOrderLineNo_TextChanged" >
<asp:ImageButton ID="pnlbtnsubmit" runat="server" Height="30px"
ImageUrl="~/Images/submit.png" onclick="pnlbtnsubmit_Click" />
<asp:ImageButton ID="pnlbtncancel" runat="server" Height="30px"
ImageUrl="~/Images/cancel.png" onclick="pnlbtncancel_Click" />
<asp:Label ID="lblmsgordlines" runat="server" Text="">



<asp:GridView ID="gvorderdetail" runat="server"
AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333"
GridLines="None"
onrowcommand="gvorderdetail_RowCommand"
onrowdatabound="gvorderdetail_RowDataBound">
<alternatingrowstyle backcolor="White" forecolor="#284775">
<columns>
<asp:TemplateField HeaderText="ItemCode">
<itemtemplate>
<asp:Label ID="lblitemcode" runat="server" Text='<%# Eval("ItemCode")%>'>




<asp:TemplateField HeaderText="Quantity">
<itemtemplate>
<asp:Label ID="lblitemqty" runat="server" Text='<%# Eval("ItemQty")%>'>

<edititemtemplate>

<asp:TextBox ID="txtitemqty" runat="server" Text='<%# Bind("ItemQty") %>'>




<asp:TemplateField HeaderText="LineVal">
<itemtemplate>
<asp:Label ID="lblitemval" runat="server" Text='<%# Eval("LineVal")%>'>



<asp:TemplateField HeaderText="Date">
<itemtemplate>
<asp:Label ID="lblitemdate" runat="server" Text='<%# Eval("UpdDate","{0:u}")%>'>



<asp:TemplateField>
<itemtemplate>
<asp:Button ID="btnchange" runat="server" Text="Edit" OnClick="btnchange_Click" CommandName="change" />


<asp:TemplateField>
<itemtemplate>
<asp:Button ID="btnupdate" runat="server" Text="Submit" OnClick="btnupdate_Click" CommandName="update" />



<editrowstyle backcolor="#999999">
<footerstyle backcolor="#5D7B9D" font-bold="True" forecolor="White">
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<pagerstyle backcolor="#284775" forecolor="White" horizontalalign="Center">
<rowstyle backcolor="#F7F6F3" forecolor="#333333">
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<sortedascendingcellstyle backcolor="#E9E7E2">
<sortedascendingheaderstyle backcolor="#506C8C">
<sorteddescendingcellstyle backcolor="#FFFDF8">
<sorteddescendingheaderstyle backcolor="#6F8DAE">




This is the C# code:
C#
protected void gvorderdetail_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "change")
    {
        TextBox tbx = (TextBox)gvorderdetail.Rows[0].FindControl("txtitemqty");

    }
}


protected void btnchange_Click(object sender, EventArgs e)
   {

       Button btn = (Button)sender;
       GridViewRow gvr = (GridViewRow)btn.NamingContainer;
       int getindex = gvr.RowIndex;




       gvorderdetail.Rows[getindex].Cells[3].Style.Value = "red";
       gvorderdetail.Rows[getindex].Cells[0].Attributes["style"] = "background-color:Red";
       gvorderdetail.Rows[getindex].Cells[1].Attributes["style"] = "background-color:Red";
       gvorderdetail.Rows[getindex].Cells[2].Attributes["style"] = "background-color:Red";
       TextBox tbx = (TextBox)gvorderdetail.Rows[0].FindControl("txtitemqty");
   }

But the code above doesnt place the textbox in the quantity column.
Posted

Find the best suitable one from here

GridView Edit Update in asp.net c#[^]

Regards..
 
Share this answer
 
Try using like this.
C#
protected void gvorderdetail_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "change")
    {
        int index = Convert.ToInt32(e.CommandArgument);

      // Retrieve the row that contains the button clicked      
      GridViewRow row = gvorderdetail.Rows[index];
       
      //Assign Value to the Text Box.  
      TextBox tbx = (TextBox)row.FindControl("txtitemqty");
      tbx.Text = "Some Text";     

      // Update the row.
      gvorderdetail.UpdateRow(index, false);
    }
}
 
Share this answer
 
Comments
Amit Karyekar 24-Sep-13 5:33am    
It gives error on the first line
int index = Convert.ToInt32(e.CommandArgument);
Input string was not in correct format.
Thomas ktg 24-Sep-13 5:54am    
include CommandArgument like this in the button.
<asp:TemplateField>
<itemtemplate>
<asp:Button ID="btnchange" runat="server" Text="Edit" OnClick="btnchange_Click" CommandName="change" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" />

Amit Karyekar 24-Sep-13 7:11am    
Doesnt works out it gives the following error.
The GridView 'gvorderdetail' fired event RowUpdating which wasn't handled.
Thomas ktg 24-Sep-13 7:31am    
comment the row and run else add this

protected void GridView1_RowUpdating(object sender, GridViewEditEventArgs e)
{
// Write here code for edit Rows
}
Amit Karyekar 25-Sep-13 1:22am    
IS it something to do with the panel.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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