Click here to Skip to main content
15,888,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to get grid dropdown value in variabe. i am populated gridview here.

my code
XML
<asp:GridView ID="gvUserInfo" runat="server" AutoGenerateColumns="false" OnRowDataBound="gvAppraisalForm_RowDataBound"  CellSpacing="10">
                    <HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
                    <Columns>
                        <asp:BoundField DataField="sName"/>
                        <asp:TemplateField HeaderText="">
                            <ItemTemplate>
                                <asp:DropDownList ID="ddlCharacteristics" runat="server" Width="200px" />
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>



protected void BindGridview()
{
SqlCommand cmd = new SqlCommand("select lAppraisalCharacteristicsID,sName from tblAppraisalCharistics", oSQL._SQLConnection);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
gvUserInfo.DataSource = ds;
gvUserInfo.DataBind();
}



protected void gvAppraisalForm_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{

var ddl = (DropDownList)e.Row.FindControl("ddlCharacteristics");
//int CountryId = Convert.ToInt32(e.Row.Cells[0].Text);
SqlCommand cmd = new SqlCommand("select * from tblAppraisalGrade", oSQL._SQLConnection);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
ddl.DataSource = ds;
ddl.DataTextField = "sDescription";
ddl.DataValueField = "nScore";
ddl.DataBind();
ddl.Items.Insert(0, new ListItem("N-NON Evaluation", "0"));
}
}
Posted

1 solution

You need to do a list of works that are listed bellow:
1.Put your gridview databind (e.g. BindGridview()) in inside a (!IsPostBack) 
2.Put your contents gridview,button,label e.t.c that are updateable inside a updatepanel
3.Use trigger for that event in which your are about to get values of dropdownlist
4.use foreach loop to loop through each row to get dropdownlist selected value

For you help I am providing a sample for you . :)


IN .ASPX


ASP
%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="WebApplication6._Default" %>

<asp:content id="HeaderContent" runat="server" contentplaceholderid="HeadContent" xmlns:asp="#unknown">
</asp:content>
<asp:content id="BodyContent" runat="server" contentplaceholderid="MainContent" xmlns:asp="#unknown">
<asp:updatepanel>
<contenttemplate>
<asp:gridview id="gvUserInfo" runat="server" autogeneratecolumns="false" onrowdatabound="gvAppraisalForm_RowDataBound" onrowcommand="gvAppraisalForm_RowCommand" cellspacing="10">
                    <HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
                    <columns>
                        <asp:boundfield datafield="sName" />
                        <asp:templatefield headertext="">
                            <itemtemplate>
                                <asp:dropdownlist id="ddlCharacteristics" runat="server" width="200px" />
                            </itemtemplate>
                        </asp:templatefield>
                    </columns>
             </asp:gridview>
             <asp:label id="lblValues" runat="server"></asp:label>
              <asp:button id="btnGetValueOfDdl" text="Get Value" runat="server" onclick="btnGetValueOfDdl_Click" />
             </contenttemplate>
             <triggers>
             <asp:asyncpostbacktrigger controlid="btnGetValueOfDdl" eventname="Click" />
             </triggers>
             </asp:updatepanel>
       
</asp:content>


In .cs (Code Behind)

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication6
{
    public partial class _Default : System.Web.UI.Page
    {
        // this is not for you I used it as I have no data in Database like yours
        List<ddlclass> lstddlClass = new List<ddlclass>();
        public class ddlClass
        {
            public string sDescription { get; set; }
            public string sName { get; set; }
            public int nScore { get; set; }
        }

        //end


        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                //use your BindGridview() method instead of this block of code
                ddlClass addlclass = new ddlClass();
                addlclass.sName = "Name1";
                addlclass.sDescription = "description1";
                addlclass.nScore = 10;
                lstddlClass.Add(addlclass);

                addlclass = new ddlClass();
                addlclass.sName = "Name2";
                addlclass.sDescription = "description2";
                addlclass.nScore = 20;
                lstddlClass.Add(addlclass);

                addlclass = new ddlClass();
                addlclass.sName = "Name3";
                addlclass.sDescription = "description3";
                addlclass.nScore = 30;
                lstddlClass.Add(addlclass);

                gvUserInfo.DataSource = lstddlClass;
                gvUserInfo.DataBind();

                //end
            }

        }

        protected void gvAppraisalForm_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {

                var ddl = (DropDownList)e.Row.FindControl("ddlCharacteristics");
                //I commentout these lines of code you need to open it 

                //int CountryId = Convert.ToInt32(e.Row.Cells[0].Text);
                //SqlCommand cmd = new SqlCommand("select * from tblAppraisalGrade", oSQL._SQLConnection);
                //SqlDataAdapter da = new SqlDataAdapter(cmd);
                //DataSet ds = new DataSet();
                //da.Fill(ds);

                //end

                ddl.DataSource = lstddlClass;
                ddl.DataTextField = "sDescription";
                ddl.DataValueField = "nScore";
                ddl.DataBind();
                ddl.Items.Insert(0, new ListItem("N-NON Evaluation", "0"));
            }
        } 



        protected void gvAppraisalForm_RowCommand(object sender, GridViewCommandEventArgs e)
        {

           
        
        }

        protected void btnGetValueOfDdl_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow row in gvUserInfo.Rows)
            {
                DropDownList ddl = (DropDownList)row.FindControl("ddlCharacteristics");

                lblValues.Text =lblValues.Text+ ddl.SelectedValue;
            }
        }
    }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900