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.ASP.Net GridView Error: Specified argument was out of the range of valid values. Parameter name: index<p>Hi <a class="username" rel="akhter"> akhter</a>,</p> <p>Refer below sample.</p> <p><span style="text-decoration: underline;"><strong>HTML</strong></span></p> <pre class="brush: html">&lt;asp:GridView ID=&#34;GridView1&#34; runat=&#34;server&#34; AutoGenerateColumns=&#34;false&#34; OnDataBound=&#34;OnDataBound&#34; OnRowCreated=&#34;OnRowCreated&#34;&gt; &lt;Columns&gt; &lt;asp:BoundField DataField=&#34;CID&#34; HeaderText=&#34;CID&#34; /&gt; &lt;asp:BoundField DataField=&#34;SectionName&#34; HeaderText=&#34;SectionName&#34; /&gt; &lt;asp:BoundField DataField=&#34;CatagoryName&#34; HeaderText=&#34;CatagoryName&#34; /&gt; &lt;asp:BoundField DataField=&#34;QTY&#34; HeaderText=&#34;QTY&#34; DataFormatString=&#34;{0:N2}&#34; ItemStyle-HorizontalAlign=&#34;Right&#34; /&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.Configuration; using System.Data.SqlClient; using System.Data; using System.Drawing;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.Drawing Imports System.Data Imports System.Data.SqlClient </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) { this.BindGrid(); } } private void BindGrid() { string query = &#34;SELECT c.CID,s.ItemName, i.SectionName ,c.CatagoryName, SUM(QTY) QTY FROM Section s INNER JOIN Item i ON s.ItemId = i.SecId INNER JOIN Catagory c ON c.CID = s.ItemId INNER JOIN Production p ON p.ItemId = c.CID GROUP BY s.ItemName, i.SectionName ,c.CatagoryName, c.CID&#34;; string conString = ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; using (SqlConnection con = new SqlConnection(conString)) { using (SqlCommand cmd = new SqlCommand(query)) { using (SqlDataAdapter sda = new SqlDataAdapter()) { cmd.Connection = con; sda.SelectCommand = cmd; using (DataTable dt = new DataTable()) { sda.Fill(dt); GridView1.DataSource = dt; GridView1.DataBind(); } } } } } int currentId = 0; decimal subTotal = 0; decimal total = 0; int subTotalRowIndex = 0; protected void OnRowCreated(object sender, GridViewRowEventArgs e) { subTotal = 0; if (e.Row.RowType == DataControlRowType.DataRow) { DataTable dt = (e.Row.DataItem as DataRowView).DataView.Table; int orderId = Convert.ToInt32(dt.Rows[e.Row.RowIndex][&#34;CID&#34;]); total += Convert.ToDecimal(dt.Rows[e.Row.RowIndex][&#34;QTY&#34;]); if (orderId != currentId) { if (e.Row.RowIndex &gt; 0) { for (int i = subTotalRowIndex; i &lt; e.Row.RowIndex; i++) { subTotal += Convert.ToDecimal(GridView1.Rows[i].Cells[3].Text); } this.AddTotalRow(&#34;Sub Total&#34;, subTotal.ToString(&#34;N2&#34;)); subTotalRowIndex = e.Row.RowIndex; } currentId = orderId; } } } private void AddTotalRow(string labelText, string value) { GridViewRow row = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Normal); row.BackColor = ColorTranslator.FromHtml(&#34;#F9F9F9&#34;); row.Cells.AddRange(new TableCell[4] { new TableCell (),new TableCell (), //Empty Cell new TableCell { Text = labelText, HorizontalAlign = HorizontalAlign.Right}, new TableCell { Text = value, HorizontalAlign = HorizontalAlign.Right } }); GridView1.Controls[0].Controls.Add(row); } protected void OnDataBound(object sender, EventArgs e) { for (int i = subTotalRowIndex; i &lt; GridView1.Rows.Count; i++) { subTotal += Convert.ToDecimal(GridView1.Rows[i].Cells[3].Text); } this.AddTotalRow(&#34;Sub Total&#34;, subTotal.ToString(&#34;N2&#34;)); this.AddTotalRow(&#34;Total&#34;, total.ToString(&#34;N2&#34;)); }</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 Me.BindGrid() End If End Sub Private Sub BindGrid() Dim query As String = &#34;SELECT c.CID,s.ItemName, i.SectionName ,c.CatagoryName, SUM(QTY) QTY FROM Section s INNER JOIN Item i ON s.ItemId = i.SecId INNER JOIN Catagory c ON c.CID = s.ItemId INNER JOIN Production p ON p.ItemId = c.CID GROUP BY s.ItemName, i.SectionName ,c.CatagoryName, c.CID&#34; Dim conString As String = ConfigurationManager.ConnectionStrings(&#34;constr&#34;).ConnectionString Using con As SqlConnection = New SqlConnection(conString) Using cmd As SqlCommand = New SqlCommand(query) Using sda As SqlDataAdapter = New SqlDataAdapter() cmd.Connection = con sda.SelectCommand = cmd Using dt As DataTable = New DataTable() sda.Fill(dt) GridView1.DataSource = dt GridView1.DataBind() End Using End Using End Using End Using End Sub Private currentId As Integer = 0 Private subTotal As Decimal = 0 Private total As Decimal = 0 Private subTotalRowIndex As Integer = 0 Protected Sub OnRowCreated(ByVal sender As Object, ByVal e As GridViewRowEventArgs) subTotal = 0 If e.Row.RowType = DataControlRowType.DataRow Then Dim dt As DataTable = (TryCast(e.Row.DataItem, DataRowView)).DataView.Table Dim orderId As Integer = Convert.ToInt32(dt.Rows(e.Row.RowIndex)(&#34;CID&#34;)) total += Convert.ToDecimal(dt.Rows(e.Row.RowIndex)(&#34;QTY&#34;)) If orderId &lt;&gt; currentId Then If e.Row.RowIndex &gt; 0 Then For i As Integer = subTotalRowIndex To e.Row.RowIndex - 1 subTotal += Convert.ToDecimal(GridView1.Rows(i).Cells(3).Text) Next Me.AddTotalRow(&#34;Sub Total&#34;, subTotal.ToString(&#34;N2&#34;)) subTotalRowIndex = e.Row.RowIndex End If currentId = orderId End If End If End Sub Private Sub AddTotalRow(ByVal labelText As String, ByVal value As String) Dim row As GridViewRow = New GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Normal) row.BackColor = ColorTranslator.FromHtml(&#34;#F9F9F9&#34;) row.Cells.AddRange(New TableCell(3) {New TableCell(), New TableCell(), New TableCell With { .Text = labelText, .HorizontalAlign = HorizontalAlign.Right }, New TableCell With { .Text = value, .HorizontalAlign = HorizontalAlign.Right }}) GridView1.Controls(0).Controls.Add(row) End Sub Protected Sub OnDataBound(ByVal sender As Object, ByVal e As EventArgs) For i As Integer = subTotalRowIndex To GridView1.Rows.Count - 1 subTotal += Convert.ToDecimal(GridView1.Rows(i).Cells(3).Text) Next Me.AddTotalRow(&#34;Sub Total&#34;, subTotal.ToString(&#34;N2&#34;)) Me.AddTotalRow(&#34;Total&#34;, total.ToString(&#34;N2&#34;)) End Sub</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><img src="https://imgur.com/47fz3pv.gif" alt="" width="297" height="229" /></p>https://www.aspforums.net:443/Threads/546920/ASPNet-GridView-Error-Specified-argument-was-out-of-the-range-of-valid-values-Parameter-name-index/https://www.aspforums.net:443/Threads/546920/ASPNet-GridView-Error-Specified-argument-was-out-of-the-range-of-valid-values-Parameter-name-index/Mon, 27 May 2019 04:08:54 GMT