Click here to Skip to main content
15,902,114 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I was Implemented the gridView with datasource is dataTable. GridView contains textBox in templatefield.. My Question is I want to Update Gridview with values of textBox when i clicks the update button..

My GridView
ASP.NET
<asp:GridView ID="GridView1" runat="server" Width="100%" CssClass="scrtablegrid" GridLines="None" 
                onrowdatabound="GridView1_RowDataBound" ShowFooter="True" 
                AutoGenerateColumns="False" onrowupdating="GridView1_RowUpdating" >
            <AlternatingRowStyle CssClass="scrgridbg2" />
            
            <HeaderStyle CssClass="scrgridheader" Height="30px" HorizontalAlign="Left" Font-Underline="true" ForeColor="White"/>
            <RowStyle CssClass="scrtablegrid" Height="40px" HorizontalAlign="Left" />
            <Columns>
                <asp:BoundField DataField="Rno" HeaderText="#" />
                <asp:TemplateField HeaderText="Item">
                     <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Text='<%# Bind("Item") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Qty">
                    
                    <ItemTemplate>
                    [<a href="#" class="removeqty">Remove</a>]<br />
		            <asp:TextBox ID="txtQty" runat="server" Text='<%# Bind("Qty") %>'	class="qtyinput" />
                        
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField HeaderText="Available" />
                <asp:BoundField DataField="Price" HeaderText="Price" DataFormatString="{0:c}" />
                <asp:BoundField HeaderText="Total" DataField="Total" />
            </Columns>
            <FooterStyle CssClass="mitemstotalbg" Height="30px" HorizontalAlign="Left" />
        </asp:GridView>


Page behind Code
HTML
public void btnGo_Click(object sender, EventArgs e)
    {
        if (Session["CurrentData"] != null)
        {
            DataTable dt = (DataTable)Session["CurrentData"];
            int count = dt.Rows.Count;
            BindGrid(count);
        }
        else
        {
            BindGrid(1);
        }
    
    }


HTML
private void BindGrid(int rowcount)
    {
        
        string cs = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
        using (SqlConnection con = new SqlConnection(cs))
        {
            string itemCode = txtItemCode.Text;
            string query = "select  ISNULL(ItemCode+' ','')+'<br />'+ISNULL(Name+' ','') as Item,Isnull(left(Price,4),0) as Price  from Y_ProductItem where ItemCode='" + @itemCode + "'";
            SqlCommand cmd = new SqlCommand(query, con);
            cmd.Parameters.AddWithValue("@itemCode", itemCode);
            con.Open();
            SqlDataReader dataReader = cmd.ExecuteReader();
            while (dataReader.Read())
            {
                Session["Item"] = dataReader["Item"];
                Session["Price"] = dataReader["Price"];
            }

            DataTable dt = new DataTable();
            DataRow dr;
            dt.Columns.Add(new System.Data.DataColumn("Rno", typeof(String)));
            dt.Columns.Add(new System.Data.DataColumn("Item", typeof(String)));
            dt.Columns.Add(new System.Data.DataColumn("Qty", typeof(String)));
            dt.Columns.Add(new System.Data.DataColumn("Available", typeof(String)));
            dt.Columns.Add(new System.Data.DataColumn("Price", typeof(String)));
            dt.Columns.Add(new System.Data.DataColumn("Total", typeof(String)));
            GridView1.DataSource = dt;
            GridView1.DataBind();
            if (Session["CurrentData"] != null)
            {
                for (int i = 0; i < rowcount + 1; i++)
                {
                    dt = (DataTable)Session["CurrentData"];
                    
                    if (dt.Rows.Count > 0)
                    {

                        
                      
                        dr = dt.NewRow();
                      

                      

                        dr[0] = dt.Rows[0][0].ToString();
                        dr[1] = dt.Rows[0][1].ToString();
                        dr[2] = dt.Rows[0][2].ToString();
                        dr[3] = dt.Rows[0][3].ToString();
                        dr[4] = dt.Rows[0][4].ToString();
                        dr[5] = dt.Rows[0][5].ToString();


                       
                    }
                }
                dr = dt.NewRow();
                dr[0] = 1;
                dr[1] = Convert.ToString(Session["Item"]);
                 dr[2] = txtQuantity.Text;
                // dr[3] = TextBox4.Text;
                 dr[4] = Convert.ToString(Session["Price"]);
                 float q = Single.Parse(txtQuantity.Text);
                 float d = Single.Parse(Convert.ToString(Session["Price"]));

                 dr[5] = d * q;
                 for (int r = 0; r < rowcount + 1; r++)
                 {

                     dr[0] = r + 1;
                 }
                dt.Rows.Add(dr);

            }
            else
            {
                dr = dt.NewRow();
                dr[0] = 1;
                dr[1] = Convert.ToString(Session["Item"]);
                dr[2] = txtQuantity.Text;
                // dr[3] = TextBox4.Text;
                dr[4] = Convert.ToString(Session["Price"]);
                float q =Single.Parse(txtQuantity.Text);
                float d = Single.Parse(Convert.ToString(Session["Price"]));
               
                dr[5] = d * q;
                dt.Rows.Add(dr);

            }

            // If Session has a data then use the value as the DataSource
            if (Session["CurrentData"] != null)
            {
                GridView1.DataSource = (DataTable)Session["CurrentData"];
                GridView1.DataBind();
            }
            else
            {
                // Bind GridView with the initial data assocaited in the DataTable
                GridView1.DataSource = dt;
                GridView1.DataBind();

            }
            // Store the DataTable in Session to retain the values
            Session["CurrentData"] = dt;

        }
    }


HTML
protected void btnUpdate_Click(object sender, EventArgs e)
    {

       // I want to implement here when update...
        
    }
Posted
Updated 7-Feb-14 19:35pm
v3
Comments
Karthik_Mahalingam 8-Feb-14 1:43am    
what do u want to do ?
Bajid Khan 8-Feb-14 2:37am    
In GridView txtQty named TextBox is there .. when user entered the number .. that should be update... Qty * Price =Total.. How do i write the code in btnUpdate_Click Event... ?

You need to find the controls and fetch their values. Then you can update it.

Examples -
1. GridView Row Edit, Delete and Update[^]
2. Asp.net insert, Edit, update, delete data in gridview[^]
 
Share this answer
 
An EditItemTemplate will be an easy option for Updating the Gridview .

Refer
how-to-inserteditupdate-and-delete-data[^]
 
Share this answer
 

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