Click here to Skip to main content
15,895,781 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I m implementing a gridview on a web page.here is its code

XML
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false"
           AllowPaging="True" Height="568px" PageSize="25"
           onrowupdating="GridView2_RowUpdating" AutoGenerateEditButton="True"
           onrowediting="GridView2_RowEditing">
       <Columns>
       <asp:TemplateField HeaderText="Name">
       <ItemTemplate>
           <asp:TextBox ID="TextBox2" runat="server" Text='<%#Eval("cstnam") %>' ReadOnly="true"></asp:TextBox>
       </ItemTemplate>
       </asp:TemplateField>

        <asp:TemplateField HeaderText="Date">
            <ItemTemplate >
                <asp:TextBox ID="TextBox13" runat="server" Text='<%#Eval("compdat") %>' ReadOnly="true"></asp:TextBox>
            </ItemTemplate>
            <ItemStyle Width="40px" Font-Bold="true" />
         </asp:TemplateField>

          <asp:TemplateField HeaderText="LogBy">
            <ItemTemplate>
                <asp:TextBox ID="TextBox14" runat="server" Text='<%#Eval("complogby") %>' ReadOnly="true"></asp:TextBox>
            </ItemTemplate>
            <ItemStyle Width="40px" Font-Bold="true" />
         </asp:TemplateField>

          <asp:TemplateField HeaderText="AttendBy">
            <ItemTemplate>
                <asp:TextBox ID="TextBox15" runat="server" Text='<%#Eval("compatdby") %>' ReadOnly="true"></asp:TextBox>
            </ItemTemplate>
            <ItemStyle Width="40px" Font-Bold="true" />
         </asp:TemplateField>

          <asp:TemplateField HeaderText="Status">
            <ItemTemplate>
                <asp:TextBox ID="TextBox16" runat="server" Text='<%#Eval("compsts") %>' ReadOnly="true"></asp:TextBox>
            </ItemTemplate>
            <ItemStyle Width="40px" Font-Bold="true" />
         </asp:TemplateField>

         <asp:TemplateField HeaderText="AttendDate">
            <ItemTemplate>
                <asp:TextBox ID="TextBox17" runat="server" Text='<%#Eval("compatddat") %>' ReadOnly="true"></asp:TextBox>
            </ItemTemplate>
            <ItemStyle Width="40px" Font-Bold="true" />
         </asp:TemplateField>


         <asp:TemplateField HeaderText="Ticket">
            <ItemTemplate>
                <asp:TextBox ID="TextBox18" runat="server" Text='<%#Eval("ticket") %>' ReadOnly="true"></asp:TextBox>
            </ItemTemplate>
            <ItemStyle Width="40px" Font-Bold="true" />
         </asp:TemplateField>


         <asp:TemplateField HeaderText="RepReceived">
            <ItemTemplate>
                <asp:TextBox ID="TextBox19" runat="server" Text='<%#Eval("rptRecieved") %>'></asp:TextBox>
            </ItemTemplate>

            <ItemStyle Width="40px" Font-Bold="true" />
         </asp:TemplateField>


         <asp:TemplateField HeaderText="ReceivedBy">
            <ItemTemplate>
                <asp:TextBox ID="TextBox20" runat="server" Text='<%#Eval("rptRecievedBy") %>' ReadOnly="true" ></asp:TextBox>
            </ItemTemplate>
            <ItemStyle Width="40px" Font-Bold="true" />
         </asp:TemplateField>


         <asp:TemplateField HeaderText="Recieveddate">
            <ItemTemplate>
                <asp:TextBox ID="TextBox10" runat="server" Text='<%#Eval("rptRecievedDate") %>' ReadOnly="true"></asp:TextBox>
            </ItemTemplate>
            <ItemStyle Width="40px" Font-Bold="true" />
         </asp:TemplateField>

         <%--<asp:TemplateField HeaderText="">
            <EditItemTemplate>
               &nbsp;&nbsp;
               <asp:TextBox ID="txtrptreciev" runat="server" Text='<%#Eval("rptRecieved") %>'></asp:TextBox>
                               &nbsp;
                <asp:LinkButton ID="lnkupdt" runat="server" CommandName="Update" Visible="true"
                   Text='<%#Eval("rptRecieved") %>'>LinkButton</asp:LinkButton>
               &nbsp;
           </EditItemTemplate>
         </asp:TemplateField>--%>
         <asp:TemplateField HeaderText="">
         <EditItemTemplate>
             <asp:TextBox ID="txtrptRecieved" Text="" runat="server"></asp:TextBox>
         </EditItemTemplate>
         </asp:TemplateField>
         <asp:CommandField  HeaderText="Update" ShowEditButton="true"/>
       </Columns>
       </asp:GridView>



Now i want to update a single column in this gridview whose code is as follow
C#
protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e)
   {

       GridViewRow myRow = GridView2.Rows[e.RowIndex];

       TextBox rptRecieved = (TextBox)myRow.FindControl("txtrptreciev");

       TextBox cstnam = (TextBox)myRow.FindControl("cstnam");

       SqlDataAdapter adp = new SqlDataAdapter("update tbcomp set rptRecieved=" + rptRecieved + " where cstnam=" + cstnam + "", con);

       DataSet ds = new DataSet();

       adp.Fill(ds);

       GridView2.EditIndex = -1;
   }

   protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
   {
       GridView2.EditIndex = e.NewEditIndex;
   }

but its nt updating the column.Please tell me what to do

thanks in advance
Posted
Updated 25-Jun-12 21:11pm
v2

SqlDataAdapter adp = new SqlDataAdapter("update tbcomp set rptRecieved=" + rptRecieved + " where cstnam=" + cstnam + "", con);


Probably you should wrap the string value in SQL query by ' sign. so your query should be as below to have it properly working.

SqlDataAdapter adp = new SqlDataAdapter("update tbcomp set rptRecieved='" + rptRecieved + "' where cstnam='" + cstnam + "'", con);


Thanks,
Hiren
 
Share this answer
 
Hi,

Where you are re-binding the GRID...???

in Row_Updating event will fire when an Update Command is received, but it will not update by itself. you must bind the grid again.

Just update the datasource used of the grid, or Get the new datasource from database and bind again.

Happy Coding.
 
Share this answer
 
Comments
abhilasha mourya 26-Jun-12 3:57am    
i m binding it on button's click.Will you please elaborate how to bind it again
abhilasha mourya 26-Jun-12 4:07am    
hey

i got what u were saying but still its not updating the value
Karthik Harve 26-Jun-12 4:21am    
can you show your code for updating the datasource of the grid..??
abhilasha mourya 26-Jun-12 4:26am    
sory
I m not updating the datasource. i m just giving value to datasource.
anyways here is code.Please suggest any solution

GridViewRow myRow = GridView2.Rows[e.RowIndex];

TextBox txtrptRecieved = (TextBox)myRow.FindControl("txtrptreciev");

TextBox complogby = (TextBox)myRow.FindControl("TextBox2");

SqlDataAdapter adp = new SqlDataAdapter("update tbcomp set rptRecieved='" + txtrptRecieved + "' where complogby='" + complogby + "'", con);

DataSet ds = new DataSet();

adp.Fill(ds);

GridView2.EditIndex = -1;
GridView2.DataSource=ds.Tables[0];
GridView2.DataBind();

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