Save DropDownList selected Value in database and display selected Text in ASP.Net GridView using C# and VB.Net

Last Reply 15 days ago By pandeyism

Posted 15 days ago

i am inserting data in gridview from dropdownlist

here DataValueField and DataTextField are used.

i want to insert DataValueField id into database but here when i debug it show DataTextField.

i also want to display DataTextField in gridview not DataValueField  

con.Open();
SqlDataAdapter adpr1 = new SqlDataAdapter("select * from ItemMasterFile ", con);
DataSet dspr1 = new DataSet();
adpr1.Fill(dspr1);
DropDownList1.DataSource = dspr1.Tables[0];
DropDownList1.DataTextField = "Descriptionitem";
DropDownList1.DataValueField = "Codeitem";
DropDownList1.DataBind();

for inserting in gridview. 

protected void GVadd_Click(object sender, EventArgs e)
{
    dt = ViewState["dt"] as DataTable;
    dr = dt.NewRow();
    dr["Codeitem"] = Codeitem.Text;
    dr["QTY"] = txtqty.Text;
    dt.Rows.Add(dr);
    GridView1.DataSource = dt;
    GridView1.DataBind();
    clear();
}

 

Posted 15 days ago Modified on 15 days ago

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