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 Button Click using C# and VB.Net<p>Hey <a class="username" rel="Warren"> Warren</a>,</p> <p>Please refer below sample.</p> <p><strong><span style="text-decoration: underline;">Database</span></strong></p> <p>For this example I have used of Northwind database that you can download using the link given below.</p> <p><a href="https://www.aspsnippets.com/Articles/Install-Microsoft-Northwind-and-Pubs-Sample-databases-in-SQL-Server-Management-Studio.aspx">Download Northwind Database</a></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: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;OrderId&#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; /&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;OrderId&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Button Text=&#34;Update&#34; runat=&#34;server&#34; OnClick=&#34;gvOrders_Update&#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; OnClick=&#34;gvCustomers_Update&#34; /&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;/Columns&gt; &lt;/asp:GridView&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 gvCustomers_Update(object sender, EventArgs e) { Button btn = (Button)sender; GridViewRow gvr = (GridViewRow)btn.NamingContainer; TextBox txtName = gvr.FindControl(&#34;txtName&#34;) as TextBox; TextBox txtCity = gvr.FindControl(&#34;txtCity&#34;) as TextBox; //update code. ScriptManager.RegisterStartupScript(this, this.GetType(), &#34;CropImage&#34;, &#34;alert(&#39;Name is : &#34; + txtName.Text + &#34;, City is &#34; + txtCity.Text + &#34;&#39;);&#34;, true); } protected void gvOrders_Update(object sender, EventArgs e) { Button btn = (Button)sender; GridViewRow row = (GridViewRow)btn.NamingContainer; string txtId = (row.FindControl(&#34;txtOrderId&#34;) as TextBox).Text; string txtDate = (row.FindControl(&#34;txtDate&#34;) as TextBox).Text; int index = txtId.IndexOf(&#34;,&#34;); string OrderId = txtId.Substring(index + 1); index = txtDate.IndexOf(&#34;,&#34;); string orderDate = txtDate.Substring(index + 1); // Update code. ScriptManager.RegisterStartupScript(this, this.GetType(), &#34;CropImage&#34;, &#34;alert(&#39;Order Id is : &#34; + OrderId + &#34;, Date is : &#34; + orderDate + &#34;&#39;);&#34;, true); }</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("select top 10 * from Customers") gvCustomers.DataBind() End If End Sub Private Shared Function GetData(ByVal query As String) As DataTable Dim strConnString As String = ConfigurationManager.ConnectionStrings("constr").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("gvOrders"), GridView) gvOrders.DataSource = GetData(String.Format("select top 3 * from Orders where CustomerId='{0}'", customerId)) gvOrders.DataBind() End If End Sub Protected Sub gvCustomers_Update(ByVal sender As Object, ByVal e As EventArgs) Dim btn As Button = CType(sender, Button) Dim gvr As GridViewRow = CType(btn.NamingContainer, GridViewRow) Dim txtName As TextBox = TryCast(gvr.FindControl("txtName"), TextBox) Dim txtCity As TextBox = TryCast(gvr.FindControl("txtCity"), TextBox) &nbsp; &nbsp; ' Update code. ScriptManager.RegisterStartupScript(Me, Me.GetType(), "CropImage", "alert('Name is : " &amp; txtName.Text &amp; ", City is " + txtCity.Text &amp; "');", True) End Sub Protected Sub gvOrders_Update(ByVal sender As Object, ByVal e As EventArgs) Dim btn As Button = CType(sender, Button) Dim row As GridViewRow = CType(btn.NamingContainer, GridViewRow) Dim txtId As String = (TryCast(row.FindControl("txtOrderId"), TextBox)).Text Dim txtDate As String = (TryCast(row.FindControl("txtDate"), TextBox)).Text Dim index As Integer = txtId.IndexOf(",") Dim OrderId As String = txtId.Substring(index + 1) index = txtDate.IndexOf(",") Dim orderDate As String = txtDate.Substring(index + 1) ' Update code. ScriptManager.RegisterStartupScript(Me, Me.GetType(), "CropImage", "alert('Order Id is : " &amp; OrderId &amp; ", Date is : " &amp; orderDate &amp; "');", True) End Sub</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><img src="https://imgur.com/SE75ix1.gif" alt="" width="449" height="369" /></p>https://www.aspforums.net:443/Threads/116688/Update-record-in-Nested-ASPNet-GridView-on-Button-Click-using-C-and-VBNet/https://www.aspforums.net:443/Threads/116688/Update-record-in-Nested-ASPNet-GridView-on-Button-Click-using-C-and-VBNet/Tue, 18 Dec 2018 04:51:32 GMT