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 Error: Column Codeitem does not belong to table <p>Hi <a class="username" rel="akhter">akhter</a>,</p> <p>If you want to insert data in gridview using datatable so you need to use viewstate.</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;&gt; &lt;Columns&gt; &lt;asp:BoundField DataField=&#34;Codeitem&#34; HeaderText=&#34;Item&#34; /&gt; &lt;asp:BoundField DataField=&#34;QTY&#34; HeaderText=&#34;QTY&#34; /&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt; &lt;br /&gt; CodeItems : &lt;asp:TextBox runat=&#34;server&#34; ID=&#34;Codeitem&#34; /&gt; &lt;br /&gt; Qty : &lt;asp:TextBox runat=&#34;server&#34; ID=&#34;txtqty&#34; /&gt; &lt;br /&gt; &lt;asp:Button Text=&#34;Insert&#34; runat=&#34;server&#34; OnClick=&#34;GVadd_Click&#34; /&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; </pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.Data </pre> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">DataTable dt = new DataTable(); DataRow dr; protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { dt.Columns.Add(&#34;Codeitem&#34;); dt.Columns.Add(&#34;QTY&#34;); ViewState[&#34;dt&#34;] = dt; } } protected void GVadd_Click(object sender, EventArgs e) { dt = ViewState[&#34;dt&#34;] as DataTable; dr = dt.NewRow(); dr[&#34;Codeitem&#34;] = Codeitem.Text; dr[&#34;QTY&#34;] = txtqty.Text; dt.Rows.Add(dr); GridView1.DataSource = dt; GridView1.DataBind(); clear(); } private void clear() { Codeitem.Text = &#34;&#34;; txtqty.Text = &#34;&#34;; }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Private dt As DataTable = New DataTable() Private dr As DataRow Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)Handle Me.Load If Not Me.IsPostBack Then dt.Columns.Add(&#34;Codeitem&#34;) dt.Columns.Add(&#34;QTY&#34;) ViewState(&#34;dt&#34;) = dt End If End Sub Protected Sub GVadd_Click(ByVal sender As Object, ByVal e As EventArgs) dt = TryCast(ViewState(&#34;dt&#34;), DataTable) dr = dt.NewRow() dr(&#34;Codeitem&#34;) = Codeitem.Text dr(&#34;QTY&#34;) = txtqty.Text dt.Rows.Add(dr) GridView1.DataSource = dt GridView1.DataBind() clear() End Sub Private Sub clear() Codeitem.Text = &#34;&#34; txtqty.Text = &#34;&#34; End Sub</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><span style="text-decoration: underline;"><strong><img src="https://i.imgur.com/nXYtbOt.gif" alt="" width="246" height="330" /></strong></span></p>https://www.aspforums.net:443/Threads/743855/ASPNet-Error-Column-Codeitem-does-not-belong-to-table/https://www.aspforums.net:443/Threads/743855/ASPNet-Error-Column-Codeitem-does-not-belong-to-table/Tue, 06 Aug 2019 02:25:51 GMT