Hi akhter,
Check this example. Now please take its reference and correct your code.
HTML
<asp:DropDownList runat="server" ID="ddlitem">
</asp:DropDownList>
<asp:Button Text="Add" runat="server" OnClick="OnAdd" />
<asp:GridView ID="GVSO" runat="server" AutoGenerateColumns="false"
AutoGenerateDeleteButton="true" OnRowDeleting="GVSO_RowDeleting">
<Columns>
<asp:BoundField DataField="Item_Code" HeaderText="Code" />
<asp:BoundField DataField="Descriptionitem" HeaderText="Description" />
<asp:BoundField DataField="QTY" HeaderText="Quantity" />
</Columns>
</asp:GridView>
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] {
new DataColumn("Item_Code"),
new DataColumn("Description")
});
dt.Rows.Add(1, "Item 1");
dt.Rows.Add(2, "Item 2");
dt.Rows.Add(3, "Item 3");
dt.Rows.Add(4, "Item 4");
ViewState["dropdownlistdata"] = dt;
ddlitem.DataSource = dt;
ddlitem.DataTextField = "Description";
ddlitem.DataValueField = "Item_Code";
ddlitem.DataBind();
DataTable dt1 = new DataTable();
dt1.Columns.AddRange(new DataColumn[3] {
new DataColumn("Item_Code"),
new DataColumn("Descriptionitem"),
new DataColumn("QTY")
});
dt1.Rows.Add(1, "Item", "10");
ViewState["dt"] = dt1;
GVSO.DataSource = dt1;
GVSO.DataBind();
}
}
protected void GVSO_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int index = Convert.ToInt32(e.RowIndex);
DataTable dt = (DataTable)ViewState["dt"];
DataTable dtdrop = ViewState["dropdownlistdata"] as DataTable;
DataRow dr = dtdrop.NewRow();
dr["Item_Code"] = dt.Rows[index]["Item_Code"];
dr["Description"] = dt.Rows[index]["Descriptionitem"];
dtdrop.Rows.Add(dr);
ViewState["dropdownlistdata"] = dtdrop;
dt.Rows[index].Delete();
ViewState["dt"] = dt;
GVSO.DataSource = dt;
GVSO.DataBind();
ddlitem.DataSource = dtdrop;
ddlitem.DataTextField = "Description";
ddlitem.DataValueField = "Item_Code";
ddlitem.DataBind();
}
protected void OnAdd(object sender, EventArgs e)
{
// get value and text which item you have selected
int codeitem = Convert.ToInt32(ddlitem.SelectedItem.Value.ToString());
string Descriptionitem = ddlitem.SelectedItem.Text.ToString();
//delete the selected item from dropdownlist
DataTable dtdrop = ViewState["dropdownlistdata"] as DataTable;
for (int i = dtdrop.Rows.Count - 1; i >= 0; i--)
{
DataRow dr1 = dtdrop.Rows[i];
if (dr1["Item_Code"].ToString() == codeitem.ToString())
dr1.Delete();
}
dtdrop.AcceptChanges();
ViewState["dropdownlistdata"] = dtdrop;
ddlitem.DataSource = dtdrop;
ddlitem.DataTextField = "Description";
ddlitem.DataValueField = "Item_Code";
ddlitem.DataBind();
//add the selected item to gridview
DataTable dtgridview = ViewState["dt"] as DataTable;
DataRow dr2 = dtgridview.NewRow();
dr2["Item_Code"] = codeitem;
dr2["Descriptionitem"] = Descriptionitem;
dr2["QTY"] = "5";
dtgridview.Rows.Add(dr2);
ViewState["dt"] = dtgridview;
GVSO.DataSource = dtgridview;
GVSO.DataBind();
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn() {
New DataColumn("Item_Code"),
New DataColumn("Description")})
dt.Rows.Add(1, "Item 1")
dt.Rows.Add(2, "Item 2")
dt.Rows.Add(3, "Item 3")
dt.Rows.Add(4, "Item 4")
ViewState("dropdownlistdata") = dt
ddlitem.DataSource = dt
ddlitem.DataTextField = "Description"
ddlitem.DataValueField = "Item_Code"
ddlitem.DataBind()
Dim dt1 As DataTable = New DataTable()
dt1.Columns.AddRange(New DataColumn(2) {
New DataColumn("Item_Code"),
New DataColumn("Descriptionitem"),
New DataColumn("QTY")})
dt1.Rows.Add(1, "Item", "10")
ViewState("dt") = dt1
GVSO.DataSource = dt1
GVSO.DataBind()
End If
End Sub
Protected Sub GVSO_RowDeleting(ByVal sender As Object, ByVal e As GridViewDeleteEventArgs)
Dim index As Integer = Convert.ToInt32(e.RowIndex)
Dim dt As DataTable = CType(ViewState("dt"), DataTable)
Dim dtdrop As DataTable = TryCast(ViewState("dropdownlistdata"), DataTable)
Dim dr As DataRow = dtdrop.NewRow()
dr("Item_Code") = dt.Rows(index)("Item_Code")
dr("Description") = dt.Rows(index)("Descriptionitem")
dtdrop.Rows.Add(dr)
ViewState("dropdownlistdata") = dtdrop
dt.Rows(index).Delete()
ViewState("dt") = dt
GVSO.DataSource = dt
GVSO.DataBind()
ddlitem.DataSource = dtdrop
ddlitem.DataTextField = "Description"
ddlitem.DataValueField = "Item_Code"
ddlitem.DataBind()
End Sub
Protected Sub OnAdd(ByVal sender As Object, ByVal e As EventArgs)
Dim codeitem As Integer = Convert.ToInt32(ddlitem.SelectedItem.Value.ToString())
Dim Descriptionitem As String = ddlitem.SelectedItem.Text.ToString()
Dim dtdrop As DataTable = TryCast(ViewState("dropdownlistdata"), DataTable)
For i As Integer = dtdrop.Rows.Count - 1 To 0 Step -1
Dim dr1 As DataRow = dtdrop.Rows(i)
If dr1("Item_Code").ToString() = codeitem.ToString() Then dr1.Delete()
Next
dtdrop.AcceptChanges()
ViewState("dropdownlistdata") = dtdrop
ddlitem.DataSource = dtdrop
ddlitem.DataTextField = "Description"
ddlitem.DataValueField = "Item_Code"
ddlitem.DataBind()
Dim dtgridview As DataTable = TryCast(ViewState("dt"), DataTable)
Dim dr2 As DataRow = dtgridview.NewRow()
dr2("Item_Code") = codeitem
dr2("Descriptionitem") = Descriptionitem
dr2("QTY") = "5"
dtgridview.Rows.Add(dr2)
ViewState("dt") = dtgridview
GVSO.DataSource = dtgridview
GVSO.DataBind()
End Sub
Screenshot