Dipa Ahuja

Dipa Ahuja

  • NA
  • 3.1k
  • 715.3k

edit field in gridview control

Oct 16 2009 8:25 AM


hello.. i am using gridview control and as a datasource i use sql datasource...


here is the code of cart.aspx:
 

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
 AutoGenerateColumns="False"
BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px"
CellPadding="3" DataKeyNames="code" DataSourceID="SqlDataSource1"
OnRowCancelingEdit="GridView1_RowCancelingEdit" 
OnRowEditing="GridView1_RowEditing" 
OnRowUpdating="GridView1_RowUpdating" 
GridLines="Vertical"
OnRowDeleting="GridView1_RowDeleting">
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />
<Columns>
    <asp:TemplateField HeaderText="item">
        <ItemTemplate>
            <asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("purl", "items/{0}") %>' />
        </ItemTemplate>
        <ControlStyle Height="100px" Width="100px" />
    </asp:TemplateField>
    <asp:TemplateField HeaderText="code" SortExpression="code">
       <%-- <EditItemTemplate>
            <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("code") %>'></asp:TextBox>
        </EditItemTemplate>--%>
        <ItemTemplate>
            <asp:Label ID="lblcode" runat="server" Text='<%# Bind("code") %>'></asp:Label>
        </ItemTemplate>
        <ItemStyle ForeColor="Black" />
    </asp:TemplateField>
    <asp:TemplateField HeaderText="pname" SortExpression="pname">
        <EditItemTemplate>
            <asp:Label ID="lblpname" runat="server" Text='<%# Eval("pname") %>'></asp:Label>
        </EditItemTemplate>
        <ItemTemplate>
            <asp:Label ID="lblpn" runat="server" Text='<%# Bind("pname") %>'></asp:Label>
        </ItemTemplate>
        <ItemStyle ForeColor="Black" />
    </asp:TemplateField>
    <asp:TemplateField HeaderText="qty" SortExpression="qty">
        <EditItemTemplate>
            <asp:TextBox ID="txtqty" runat="server" Text='<%# Bind("qty") %>'></asp:TextBox>
        </EditItemTemplate>
        <ItemTemplate>
            <asp:Label ID="oldqty" runat="server" Text='<%# Bind("qty") %>'/>
         </ItemTemplate>
         <ItemStyle ForeColor="Black" />
    </asp:TemplateField>
    <asp:TemplateField HeaderText="price" SortExpression="price">
      <EditItemTemplate>
        <asp:TextBox ID="txtprice" ReadOnly="true" runat="server" Text='<%# Bind("price") %>'></asp:TextBox>
        </EditItemTemplate>
        <ItemTemplate>
            <asp:Label ID="Lblp" runat="server" Text='<%# Bind("price") %>'></asp:Label>
        </ItemTemplate>
        <ItemStyle ForeColor="Black" />
    </asp:TemplateField>
    <asp:CommandField ShowEditButton="True" />
    <asp:CommandField ButtonType="Button" ShowDeleteButton="True" ShowEditButton="True" />
    <asp:CommandField ButtonType="Button" ShowDeleteButton="True" />
</Columns>
<PagerStyle ForeColor="Black" HorizontalAlign="Center" BackColor="#999999" />
<SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="Gainsboro" />
</asp:GridView>
<br />
<asp:Button ID="final" runat="server" Text="Final Purchase" OnClick="final_Click" />

</center>


<asp:Label ID="Label1" runat="server"></asp:Label>
<br />
<br />
<br />
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
    SelectCommand="SELECT * FROM [tmp]">


    <asp:TemplateField HeaderText="item">
        <ItemTemplate>
            <asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("purl", "items/{0}") %>' />
        </ItemTemplate>
        <ControlStyle Height="100px" Width="100px" />
    </asp:TemplateField>
    <asp:TemplateField HeaderText="code" SortExpression="code">
       <%-- <EditItemTemplate>
            <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("code") %>'></asp:TextBox>
        </EditItemTemplate>--%>
        <ItemTemplate>
            <asp:Label ID="Label1" runat="server" Text='<%# Bind("code") %>'></asp:Label>
        </ItemTemplate>
        <ItemStyle ForeColor="Black" />
    </asp:TemplateField>
    <asp:TemplateField HeaderText="pname" SortExpression="pname">
        <EditItemTemplate>
            <asp:Label ID="lblpname" runat="server" Text='<%# Eval("pname") %>'></asp:Label>
        </EditItemTemplate>
        <ItemTemplate>
            <asp:Label ID="lblpn" runat="server" Text='<%# Bind("pname") %>'></asp:Label>
        </ItemTemplate>
        <ItemStyle ForeColor="Black" />
    </asp:TemplateField>
    <asp:TemplateField HeaderText="qty" SortExpression="qty">
        <EditItemTemplate>
            <asp:TextBox ID="txtqty" runat="server" Text='<%# Bind("qty") %>'></asp:TextBox>
        </EditItemTemplate>
        <ItemTemplate>
            <asp:Label ID="oldqty" runat="server" Text='<%# Bind("qty") %>'/>
         </ItemTemplate>
         <ItemStyle ForeColor="Black" />
    </asp:TemplateField>
    <asp:TemplateField HeaderText="price" SortExpression="price">
      <EditItemTemplate>
        <asp:TextBox ID="txtprice" ReadOnly="true" runat="server" Text='<%# Bind("price") %>'></asp:TextBox>
        </EditItemTemplate>
        <ItemTemplate>
            <asp:Label ID="Lblp" runat="server" Text='<%# Bind("price") %>'></asp:Label>
        </ItemTemplate>
        <ItemStyle ForeColor="Black" />
    </asp:TemplateField>
    <asp:CommandField ShowEditButton="True" />
    <asp:CommandField ButtonType="Button" ShowDeleteButton="True" ShowEditButton="True" />
    <asp:CommandField ButtonType="Button" ShowDeleteButton="True" />
</Columns>
<PagerStyle ForeColor="Black" HorizontalAlign="Center" BackColor="#999999" />
<SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="Gainsboro" />
</asp:GridView>
<br />
<asp:Button ID="final" runat="server" Text="Final Purchase" OnClick="final_Click" />
</center>
<asp:Label ID="Label1" runat="server"></asp:Label>
<br />
<br />
<br />
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
    SelectCommand="SELECT * FROM [tmp]"




Photobucket
in the .cs file...
 
 
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
System.Web.UI.WebControls.
TextBox TextBox1;
TextBox1 = (System.Web.UI.WebControls.
TextBox)GridView1.Rows[e.RowIndex].FindControl("txtqty");

 
System.Web.UI.WebControls.
Label Label3;
Label3 = (System.Web.UI.WebControls.
Label)GridView1.Rows[e.RowIndex].FindControl("lblcode");


 
objConnection.Open();
 
SqlCommand comm = new SqlCommand("UPDATE tmp SET qty=@qty,price=@price WHERE code=@code", objConnection);
comm.Parameters.AddWithValue(
"@qty", TextBox1.Text.ToString());
// comm.Parameters.AddWithValue("@price",Convert.ToDouble(newprice.ToString()));
 

comm.Parameters.AddWithValue(
"@code", Label3.Text.ToString());
comm.ExecuteNonQuery();
 
//Finally Close the Connection
objConnection.Close();
GridView1.EditIndex = -1;
GridView1.DataBind();

when user edit quantity it updated... bt how to update the price as well..?




Answers (17)