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: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
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);
}
}
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;
}
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
// I want to implement here when update...
}