Hi akhter,
Refer below sample.
HTML
Qty :
<asp:TextBox runat="server" ID="txtqty" />
Description :
<asp:DropDownList runat="server" ID="ddlItems">
</asp:DropDownList>
<asp:Button Text="Insert" runat="server" OnClick="GVadd_Click" />
<asp:GridView runat="server" ID="GridView1" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Descriptionitem" HeaderText="Descriptionitem" />
<asp:BoundField DataField="QTY" HeaderText="QTY" />
</Columns>
</asp:GridView>
Namespaces
C#
using System.Data;
VB.Net
Imports System.Data
Code
C#
DataTable dt = new DataTable();
DataRow dr;
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
dt.Columns.Add("Descriptionitem");
dt.Columns.Add("Qty");
DataTable dtddl = new DataTable();
dtddl.Columns.Add("Descriptionitem");
dtddl.Columns.Add("Codeitem");
dtddl.Rows.Add("A", 1);
dtddl.Rows.Add("B", 2);
dtddl.Rows.Add("C", 3);
ddlItems.DataSource = dtddl;
ddlItems.DataTextField = "Descriptionitem";
ddlItems.DataValueField = "Codeitem";
ddlItems.DataBind();
ddlItems.Items.Insert(0, new ListItem("Select", "0"));
ViewState["dt"] = dt;
}
}
protected void GVadd_Click(object sender, EventArgs e)
{
dt = ViewState["dt"] as DataTable;
dr = dt.NewRow();
dr["Descriptionitem"] = ddlItems.SelectedItem.Text;
dr["QTY"] = txtqty.Text;
dt.Rows.Add(dr);
GridView1.DataSource = dt;
GridView1.DataBind();
}
VB.Net
Private dt As DataTable = New DataTable()
Private dr As DataRow
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
dt.Columns.Add("Descriptionitem")
dt.Columns.Add("Qty")
Dim dtddl As DataTable = New DataTable()
dtddl.Columns.Add("Descriptionitem")
dtddl.Columns.Add("Codeitem")
dtddl.Rows.Add("A", 1)
dtddl.Rows.Add("B", 2)
dtddl.Rows.Add("C", 3)
ddlItems.DataSource = dtddl
ddlItems.DataTextField = "Descriptionitem"
ddlItems.DataValueField = "Codeitem"
ddlItems.DataBind()
ddlItems.Items.Insert(0, New ListItem("Select", "0"))
ViewState("dt") = dt
End If
End Sub
Protected Sub GVadd_Click(ByVal sender As Object, ByVal e As EventArgs)
dt = TryCast(ViewState("dt"), DataTable)
dr = dt.NewRow()
dr("Descriptionitem") = ddlItems.SelectedItem.Text
dr("QTY") = txtqty.Text
dt.Rows.Add(dr)
GridView1.DataSource = dt
GridView1.DataBind()
End Sub
Screenshot