ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Update record in Nested ASP.Net GridView on TextBox Change using C# and VB.Net<p>Hey <a class="username" rel="Warren">Warren</a>,</p> <p>Please refer below sample.</p> <p><span style="text-decoration: underline;"><strong>HTML</strong></span></p> <pre class="brush: html">&lt;script type=&#34;text/javascript&#34; src=&#34;http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js&#34;&gt;&lt;/script&gt; &lt;script type=&#34;text/javascript&#34;&gt; $(&#34;[src*=plus]&#34;).live(&#34;click&#34;, function () { $(this).closest(&#34;tr&#34;).after(&#34;&lt;tr&gt;&lt;td&gt;&lt;/td&gt;&lt;td colspan = &#39;999&#39;&gt;&#34; + $(this).next().html() + &#34;&lt;/td&gt;&lt;/tr&gt;&#34;) $(this).attr(&#34;src&#34;, &#34;images/minus.png&#34;); }); $(&#34;[src*=minus]&#34;).live(&#34;click&#34;, function () { $(this).attr(&#34;src&#34;, &#34;images/plus.png&#34;); $(this).closest(&#34;tr&#34;).next().remove(); }); &lt;/script&gt; &lt;asp:ScriptManager runat=&#34;server&#34; /&gt; &lt;asp:UpdatePanel runat=&#34;server&#34;&gt; &lt;ContentTemplate&gt; &lt;asp:GridView ID=&#34;gvCustomers&#34; runat=&#34;server&#34; AutoGenerateColumns=&#34;false&#34; CssClass=&#34;Grid&#34; DataKeyNames=&#34;CustomerID&#34; OnRowDataBound=&#34;OnRowDataBound&#34;&gt; &lt;Columns&gt; &lt;asp:TemplateField&gt; &lt;ItemTemplate&gt; &lt;img alt=&#34;&#34; style=&#34;cursor: pointer&#34; src=&#34;images/plus.png&#34; /&gt; &lt;asp:Panel ID=&#34;pnlOrders&#34; runat=&#34;server&#34; Style=&#34;display: none&#34;&gt; &lt;asp:GridView ID=&#34;gvOrders&#34; runat=&#34;server&#34; AutoGenerateColumns=&#34;false&#34; CssClass=&#34;ChildGrid&#34;&gt; &lt;Columns&gt; &lt;asp:TemplateField HeaderText=&#34;OrderId&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:TextBox ID=&#34;txtOrderId&#34; runat=&#34;server&#34; Text=&#39;&lt;%#Eval(&#34;OrderId&#34;) %&gt;&#39; /&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;OrderDate&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:TextBox ID=&#34;txtDate&#34; runat=&#34;server&#34; Text=&#39;&lt;%#Eval(&#34;OrderDate&#34;) %&gt;&#39; AutoPostBack=&#34;true&#34; OnTextChanged=&#34;Change&#34; /&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt; &lt;/asp:Panel&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;ContactName&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:TextBox ID=&#34;txtName&#34; runat=&#34;server&#34; Text=&#39;&lt;%#Eval(&#34;ContactName&#34;) %&gt;&#39; /&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;City&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:TextBox ID=&#34;txtCity&#34; runat=&#34;server&#34; Text=&#39;&lt;%#Eval(&#34;City&#34;) %&gt;&#39; /&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;Update&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Button Text=&#34;Update&#34; runat=&#34;server&#34; ID=&#34;btnUpdate&#34; /&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt; &lt;/ContentTemplate&gt; &lt;/asp:UpdatePanel&gt;</pre> <p><span style="text-decoration: underline;"><strong>Namespaces</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.Data; using System.Data.SqlClient; using System.Configuration;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.Data.SqlClient Imports System.Data</pre> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { gvCustomers.DataSource = GetData(&#34;select top 10 * from Customers&#34;); gvCustomers.DataBind(); } } private static DataTable GetData(string query) { string strConnString = ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; using (SqlConnection con = new SqlConnection(strConnString)) { using (SqlCommand cmd = new SqlCommand()) { cmd.CommandText = query; using (SqlDataAdapter sda = new SqlDataAdapter()) { cmd.Connection = con; sda.SelectCommand = cmd; using (DataSet ds = new DataSet()) { DataTable dt = new DataTable(); sda.Fill(dt); return dt; } } } } } protected void OnRowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { string customerId = gvCustomers.DataKeys[e.Row.RowIndex].Value.ToString(); GridView gvOrders = e.Row.FindControl(&#34;gvOrders&#34;) as GridView; gvOrders.DataSource = GetData(string.Format(&#34;select top 3 * from Orders where CustomerId=&#39;{0}&#39;&#34;, customerId)); gvOrders.DataBind(); } } protected void Change(object sender, EventArgs e) { GridViewRow row = (sender as TextBox).NamingContainer as GridViewRow; string orderId = (row.FindControl(&#34;txtOrderId&#34;) as TextBox).Text; string orderdate = (row.FindControl(&#34;txtDate&#34;) as TextBox).Text; string strConnString = ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; using (SqlConnection con = new SqlConnection(strConnString)) { using (SqlCommand cmd = new SqlCommand(&#34;UPDATE Orders SET OrderDate = @OrderDate WHERE OrderID = @OrderId&#34;, con)) { cmd.Parameters.AddWithValue(&#34;@OrderId&#34;, orderId.Split(&#39;,&#39;)[orderId.Split(&#39;,&#39;).Length - 1]); cmd.Parameters.AddWithValue(&#34;@OrderDate&#34;, orderdate.Split(&#39;,&#39;)[orderdate.Split(&#39;,&#39;).Length - 1]); con.Open(); cmd.ExecuteNonQuery(); con.Close(); } } (row.FindControl(&#34;txtDate&#34;) as TextBox).Text = orderdate.Split(&#39;,&#39;)[orderdate.Split(&#39;,&#39;).Length - 1]; (row.FindControl(&#34;txtOrderId&#34;) as TextBox).Text = orderId.Split(&#39;,&#39;)[orderId.Split(&#39;,&#39;).Length - 1]; }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load If Not IsPostBack Then gvCustomers.DataSource = GetData(&#34;select top 10 * from Customers&#34;) gvCustomers.DataBind() End If End Sub Private Shared Function GetData(ByVal query As String) As DataTable Dim strConnString As String = ConfigurationManager.ConnectionStrings(&#34;constr&#34;).ConnectionString Using con As SqlConnection = New SqlConnection(strConnString) Using cmd As SqlCommand = New SqlCommand() cmd.CommandText = query Using sda As SqlDataAdapter = New SqlDataAdapter() cmd.Connection = con sda.SelectCommand = cmd Using ds As DataSet = New DataSet() Dim dt As DataTable = New DataTable() sda.Fill(dt) Return dt End Using End Using End Using End Using End Function Protected Sub OnRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) If e.Row.RowType = DataControlRowType.DataRow Then Dim customerId As String = gvCustomers.DataKeys(e.Row.RowIndex).Value.ToString() Dim gvOrders As GridView = TryCast(e.Row.FindControl(&#34;gvOrders&#34;), GridView) gvOrders.DataSource = GetData(String.Format(&#34;select top 3 * from Orders where CustomerId=&#39;{0}&#39;&#34;, customerId)) gvOrders.DataBind() End If End Sub Protected Sub Change(ByVal sender As Object, ByVal e As EventArgs) Dim row As GridViewRow = TryCast((TryCast(sender, TextBox)).NamingContainer, GridViewRow) Dim orderId As String = (TryCast(row.FindControl(&#34;txtOrderId&#34;), TextBox)).Text Dim orderdate As String = (TryCast(row.FindControl(&#34;txtDate&#34;), TextBox)).Text Dim strConnString As String = ConfigurationManager.ConnectionStrings(&#34;constr&#34;).ConnectionString Using con As SqlConnection = New SqlConnection(strConnString) Using cmd As SqlCommand = New SqlCommand(&#34;UPDATE Orders SET OrderDate = @OrderDate WHERE OrderID = @OrderId&#34;, con) cmd.Parameters.AddWithValue(&#34;@OrderId&#34;, orderId.Split(&#34;,&#34;c)(orderId.Split(&#34;,&#34;c).Length - 1)) cmd.Parameters.AddWithValue(&#34;@OrderDate&#34;, orderdate.Split(&#34;,&#34;c)(orderdate.Split(&#34;,&#34;c).Length - 1)) con.Open() cmd.ExecuteNonQuery() con.Close() End Using End Using TryCast(row.FindControl(&#34;txtDate&#34;), TextBox).Text = orderdate.Split(&#34;,&#34;c)(orderdate.Split(&#34;,&#34;c).Length - 1) TryCast(row.FindControl(&#34;txtOrderId&#34;), TextBox).Text = orderId.Split(&#34;,&#34;c)(orderId.Split(&#34;,&#34;c).Length - 1) End Sub</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><img src="https://imgur.com/TJORnOR.gif" alt="" width="446" height="358" /></p>https://www.aspforums.net:443/Threads/604540/Update-record-in-Nested-ASPNet-GridView-on-TextBox-Change-using-C-and-VBNet/https://www.aspforums.net:443/Threads/604540/Update-record-in-Nested-ASPNet-GridView-on-TextBox-Change-using-C-and-VBNet/Mon, 07 Jan 2019 00:37:49 GMT