How to get dropdown selected values in gridview

Last Reply 4 days ago By pandeyism

Posted 5 days ago

 Hi Team,

I am trying to get dropdown selected values in gridview. its not giving correct values. when I select 1st dropdown and second dropdown will get loaded but when i check web console dropdown values not coming properly. Please suggest me where is the issue.

<asp:GridView ID="gvConcentration" runat="server" ShowFooter="true" AutoGenerateColumns="false" CssClass="table table-bordered table-striped" OnRowDataBound="gvConcentration_RowDataBound">
                                        <Columns>
                                       
                                            <asp:TemplateField HeaderText="Row No">
                                                <ItemTemplate>
                                                    <%# Container.DataItemIndex + 1 %>
                                                </ItemTemplate>
                                                <FooterStyle HorizontalAlign="Left" />
                                                <FooterTemplate>
                                                    <asp:Button ID="btnAddConcentration" runat="server" Text="+" OnClick="btnAddConcentration_Click" CssClass="btn btn-default btn-sm" />
                                                </FooterTemplate>
                                            </asp:TemplateField>
                                            <asp:TemplateField HeaderText="Degree">
                                                <ItemTemplate>
                                                    <asp:DropDownList ID="ddlDegree" runat="server" AppendDataBoundItems="true" CssClass="selectPicker form-control" OnSelectedIndexChanged="ddlDegree_SelectedIndexChanged" AutoPostBack="true">                                                  
                                                    </asp:DropDownList>
                                                </ItemTemplate>
                                            </asp:TemplateField>
                                            <asp:TemplateField HeaderText="Type">
                                                <ItemTemplate>
                                                    <asp:DropDownList ID="ddlType" runat="server"  AppendDataBoundItems="true" CssClass="selectPicker form-control"  >
                                                       <%-- <asp:ListItem Value="0" Text="--Select--"></asp:ListItem>--%>
                                                    </asp:DropDownList>
                                                </ItemTemplate>
                                            </asp:TemplateField>
                                            <asp:TemplateField HeaderText="Concentration">
                                                <ItemTemplate>
                                                    <asp:DropDownList ID="ddlConc" runat="server" AppendDataBoundItems="true" CssClass="selectPicker form-control" >
                                                    <%-- <asp:ListItem Value="0" Text="--Select--"></asp:ListItem>--%>
                                                    </asp:DropDownList>
                                                </ItemTemplate>
                                            </asp:TemplateField>
                                            <asp:TemplateField HeaderText="Completed">
                                                <ItemTemplate>
                                                    <asp:DropDownList ID="ddlCompleted" runat="server" AppendDataBoundItems="true" CssClass="selectPicker form-control">
                                                        <asp:ListItem Value="0" Text="--Select--"></asp:ListItem>
                                                        <asp:ListItem Value="1">Yes</asp:ListItem>
                                                        <asp:ListItem Value="2">No</asp:ListItem>
                                                    </asp:DropDownList>
                                                </ItemTemplate>
                                            </asp:TemplateField>
                                        </Columns>
                                    </asp:GridView>

 

   protected void gvConcentration_RowDataBound(object sender, GridViewRowEventArgs e)
        { 
            
               SqlConnection con = new SqlConnection(connStr);          
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    DataRowView drv = e.Row.DataItem as DataRowView;
                    DropDownList ddldegree = (e.Row.FindControl("ddlDegree") as DropDownList);
                   DropDownList ddlType = (e.Row.FindControl("ddlType") as DropDownList);
                    DropDownList ddlCon = (e.Row.FindControl("ddlConc") as DropDownList);
                    LoadDropDown("SHR_GetHighestDegreeList", "DegreeID", "DegreeName", (DropDownList)ddldegree);
                    LoadDropDown("SHR_GetConcentrationList", "ConcentrationID", "ConcentrationName", (DropDownList)ddlCon);
                ddlType.Items.Add("--Select--");
                
            }
                     
           
        }

 

   protected void ddlDegree_SelectedIndexChanged(object sender, EventArgs e)
        {          
            SqlConnection con = new SqlConnection(connStr);
            DropDownList ddlDegreeList = sender as DropDownList;

            //foreach (GridViewRow gRow in gvConcentration.Rows)
            //{
            //    DropDownList ddlType = (DropDownList)gRow.FindControl("ddlType");
            //    ddlType.DataTextField = "TypeName";
            //    ddlType.DataValueField = "TypeID";
            //    ddlType.DataSource = RetriveType(ddlDegreeList.SelectedValue);
            //    ddlType.DataBind();

            //}

            GridViewRow currentRow = (GridViewRow)ddlDegreeList.NamingContainer;
            GridViewRow row = (GridViewRow)ddlDegreeList.Parent.Parent;
            int idx = row.RowIndex;
            DropDownList ddlType = (DropDownList)gvConcentration.Rows[idx].Cells[2].FindControl("ddlType");
            DropDownList ddlCon = (DropDownList)currentRow.FindControl("ddlConc");
            ddlType.Items.Clear();
            ddlType.Items.Add("--Select--");
            string Typevalue = ddlDegreeList.SelectedValue;
            if (Typevalue != null && Typevalue != "0")
            {
                ddlType.DataTextField = "TypeName";
                ddlType.DataValueField = "TypeID";
                ddlType.DataSource = RetriveType(Typevalue);
                ddlType.DataBind();


            }


        }

 

      private void SetInitialConcentration()
        {

            DataTable dt = new DataTable();
            DataRow dr = null;
            dt.Columns.Add(new DataColumn("RowNo", typeof(string)));
            dt.Columns.Add(new DataColumn("DegreeID", typeof(string)));
            dt.Columns.Add(new DataColumn("TypeID", typeof(string)));
            dt.Columns.Add(new DataColumn("ConcentrationID", typeof(string)));
            dt.Columns.Add(new DataColumn("IsCompleted", typeof(string)));
            dr = dt.NewRow();
            dr["RowNo"] = 1;
            dt.Rows.Add(dr);
            ViewState["ConcentrationTable"] = dt;
            gvConcentration.DataSource = dt;
            gvConcentration.DataBind();

        }

        private void AddNewRowToConcentration()
        {

            if (ViewState["ConcentrationTable"] != null)
            {

                DataTable dtConcentrationTable = (DataTable)ViewState["ConcentrationTable"];
                DataRow drCurrentRow = null;
                if (dtConcentrationTable.Rows.Count > 0)
                {

                    drCurrentRow = dtConcentrationTable.NewRow();
                    drCurrentRow["RowNo"] = dtConcentrationTable.Rows.Count + 1;
                    dtConcentrationTable.Rows.Add(drCurrentRow);
                    ViewState["ConcentrationTable"] = dtConcentrationTable;
                    for (int i = 0; i < dtConcentrationTable.Rows.Count - 1; i++)
                    {
                        DropDownList ddl1 = (DropDownList)gvConcentration.Rows[i].Cells[1].FindControl("ddlDegree");
                        DropDownList ddl2 = (DropDownList)gvConcentration.Rows[i].Cells[2].FindControl("ddlType");
                        DropDownList ddl3 = (DropDownList)gvConcentration.Rows[i].Cells[3].FindControl("ddlConc");
                        DropDownList ddl4 = (DropDownList)gvConcentration.Rows[i].Cells[4].FindControl("ddlCompleted");
                        dtConcentrationTable.Rows[i]["DegreeID"] = ddl1.SelectedItem.Text;
                        dtConcentrationTable.Rows[i]["TypeID"] = ddl2.SelectedItem.Text;
                        dtConcentrationTable.Rows[i]["ConcentrationID"] = ddl3.SelectedItem.Text;
                        dtConcentrationTable.Rows[i]["IsCompleted"] = ddl4.SelectedItem.Text;

                    }

                    gvConcentration.DataSource = dtConcentrationTable;
                    gvConcentration.DataBind();

                }

            }

            else
            {

                Response.Write("ViewState is null");

            }
            SetPreviousConcentration();

        }

        private void SetPreviousConcentration()
        {
           string strType = "";
            int rowIndex = 0;
            if (ViewState["ConcentrationTable"] != null)
            {
                DataTable dt = (DataTable)ViewState["ConcentrationTable"];

                if (dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        DropDownList ddl1 = (DropDownList)gvConcentration.Rows[rowIndex].Cells[1].FindControl("ddlDegree");
                        DropDownList ddl2 = (DropDownList)gvConcentration.Rows[rowIndex].Cells[2].FindControl("ddlType");
                        DropDownList ddl3 = (DropDownList)gvConcentration.Rows[rowIndex].Cells[3].FindControl("ddlConc");
                        DropDownList ddl4 = (DropDownList)gvConcentration.Rows[rowIndex].Cells[4].FindControl("ddlCompleted");
                        if (i < dt.Rows.Count - 1)
                        {
                          
                            ddl1.ClearSelection();
                            ddl1.Items.FindByText(dt.Rows[i]["DegreeID"].ToString()).Selected = true;
                            ddl2.ClearSelection();                                       
                            strType = dt.Rows[i]["TypeID"].ToString();                         
                            ddl2.SelectedItem.Text = strType;
                            ddl2.SelectedValue = strType;
                            strType = string.Empty;
                            ddl3.ClearSelection();
                            ddl3.Items.FindByText(dt.Rows[i]["ConcentrationID"].ToString()).Selected = true;
                            ddl4.ClearSelection();
                            ddl4.Items.FindByText(dt.Rows[i]["IsCompleted"].ToString()).Selected = true;
                        }

                        rowIndex++;

                    }


                }

            }

        }

        protected void btnAddConcentration_Click(object sender, EventArgs e)
        {
            AddNewRowToConcentration();
        }

 

You are viewing reply posted by: pandeyism 4 days ago.
Posted 4 days ago Modified on 4 days ago

Hi rajeshg12,

Refer below sample.

HTML

<asp:GridView ID="Gridview1" runat="server" ShowFooter="true" 
    AutoGenerateColumns="false" 
    onselectedindexchanged="Gridview1_SelectedIndexChanged">
    <Columns>
        <asp:BoundField DataField="RowNumber" HeaderText="Row Number" />
        <asp:TemplateField HeaderText="Header 1">
            <ItemTemplate>
                <asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="true">
                    <asp:ListItem Value="-1">Select</asp:ListItem>
                </asp:DropDownList>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Header 2">
            <ItemTemplate>
                <asp:DropDownList ID="DropDownList2" runat="server" AppendDataBoundItems="true">
                    <asp:ListItem Value="-1">Select</asp:ListItem>
                </asp:DropDownList>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Header 3">
            <ItemTemplate>
                <asp:DropDownList ID="DropDownList3" runat="server" AppendDataBoundItems="true">
                    <asp:ListItem Value="-1">Select</asp:ListItem>
                </asp:DropDownList>
            </ItemTemplate>
            <FooterStyle HorizontalAlign="Right" />
            <FooterTemplate>
                <asp:Button ID="ButtonAdd" runat="server" Text="Add New Row" OnClick="ButtonAdd_Click" />
            </FooterTemplate>
        </asp:TemplateField>
        <asp:CommandField SelectText="Select" ShowSelectButton="true" ButtonType="Link" />
    </Columns>
</asp:GridView>

Namespaces

using System.Collections;
using System.Data;

Code

private ArrayList GetDummyData()
{
    ArrayList arr = new ArrayList();
    arr.Add(new ListItem("Item1", "1"));
    arr.Add(new ListItem("Item2", "2"));
    arr.Add(new ListItem("Item3", "3"));
    arr.Add(new ListItem("Item4", "4"));
    arr.Add(new ListItem("Item5", "5"));
    return arr;
}

private void FillDropDownList(DropDownList ddl)
{
    ArrayList arr = GetDummyData();
    foreach (ListItem item in arr)
    {
        ddl.Items.Add(item);
    }
}

private void SetInitialRow()
{
    DataTable dt = new DataTable();
    DataRow dr = null;

    dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
    dt.Columns.Add(new DataColumn("Column1", typeof(string)));
    dt.Columns.Add(new DataColumn("Column2", typeof(string)));
    dt.Columns.Add(new DataColumn("Column3", typeof(string)));

    dr = dt.NewRow();
    dr["RowNumber"] = 1;
    dt.Rows.Add(dr);

    ViewState["CurrentTable"] = dt;
    Gridview1.DataSource = dt;
    Gridview1.DataBind();

    DropDownList ddl1 = (DropDownList)Gridview1.Rows[0].Cells[1].FindControl("DropDownList1");
    DropDownList ddl2 = (DropDownList)Gridview1.Rows[0].Cells[2].FindControl("DropDownList2");
    DropDownList ddl3 = (DropDownList)Gridview1.Rows[0].Cells[3].FindControl("DropDownList3");
    FillDropDownList(ddl1);
    FillDropDownList(ddl2);
    FillDropDownList(ddl3);

}

private void AddNewRowToGrid()
{

    if (ViewState["CurrentTable"] != null)
    {
        DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
        DataRow drCurrentRow = null;

        if (dtCurrentTable.Rows.Count > 0)
        {
            drCurrentRow = dtCurrentTable.NewRow();
            drCurrentRow["RowNumber"] = dtCurrentTable.Rows.Count + 1;
            dtCurrentTable.Rows.Add(drCurrentRow);
            ViewState["CurrentTable"] = dtCurrentTable;

            for (int i = 0; i < dtCurrentTable.Rows.Count - 1; i++)
            {
                DropDownList ddl1 = (DropDownList)Gridview1.Rows[i].Cells[1].FindControl("DropDownList1");
                DropDownList ddl2 = (DropDownList)Gridview1.Rows[i].Cells[2].FindControl("DropDownList2");
                DropDownList ddl3 = (DropDownList)Gridview1.Rows[i].Cells[3].FindControl("DropDownList3");
                dtCurrentTable.Rows[i]["Column1"] = ddl1.SelectedItem.Text;
                dtCurrentTable.Rows[i]["Column2"] = ddl2.SelectedItem.Text;
                dtCurrentTable.Rows[i]["Column3"] = ddl3.SelectedItem.Text;

            }
            Gridview1.DataSource = dtCurrentTable;
            Gridview1.DataBind();
        }
    }
    else
    {
        Response.Write("ViewState is null");
    }
    SetPreviousData();
}

private void SetPreviousData()
{
    int rowIndex = 0;
    if (ViewState["CurrentTable"] != null)
    {
        DataTable dt = (DataTable)ViewState["CurrentTable"];
        if (dt.Rows.Count > 0)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DropDownList ddl1 = (DropDownList)Gridview1.Rows[rowIndex].Cells[1].FindControl("DropDownList1");
                DropDownList ddl2 = (DropDownList)Gridview1.Rows[rowIndex].Cells[2].FindControl("DropDownList2");
                DropDownList ddl3 = (DropDownList)Gridview1.Rows[rowIndex].Cells[3].FindControl("DropDownList3");

                FillDropDownList(ddl1);
                FillDropDownList(ddl2);
                FillDropDownList(ddl3);

                if (i < dt.Rows.Count - 1)
                {
                    ddl1.ClearSelection();
                    ddl1.Items.FindByText(dt.Rows[i]["Column1"].ToString()).Selected = true;

                    ddl2.ClearSelection();
                    ddl2.Items.FindByText(dt.Rows[i]["Column2"].ToString()).Selected = true;

                    ddl3.ClearSelection();
                    ddl3.Items.FindByText(dt.Rows[i]["Column3"].ToString()).Selected = true;
                }

                rowIndex++;
            }
        }
    }
}

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        SetInitialRow();
    }
}

protected void ButtonAdd_Click(object sender, EventArgs e)
{
    AddNewRowToGrid();
}

protected void Gridview1_SelectedIndexChanged(object sender, EventArgs e)
{
    int index = Gridview1.SelectedRow.RowIndex;
    string text1 = (Gridview1.Rows[index].FindControl("DropDownList1") as DropDownList).SelectedItem.Text;
    string text2 = (Gridview1.Rows[index].FindControl("DropDownList2") as DropDownList).SelectedItem.Text;
    string text3 = (Gridview1.Rows[index].FindControl("DropDownList3") as DropDownList).SelectedItem.Text;
    ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "alert('" + text1 + "\\n" + text2 + "\\n" + text3 + "');", true);
}

Screenshot