how to get dropdownlist selected value in inside gridview in asp.net [Resolved]

Posted by Gow.Net under ASP.NET on 10/19/2012 | Points: 10 | Views : 36257 | Status : [Member] | Replies : 4
how to get dropdownlist selected value in inside gridview in asp.net?
in my code
<asp:GridView ID="Grid_viewprofile" CellPadding="4" ForeColor="Black"  
runat="server" AllowPaging="true" PageSize="1"
AutoGenerateColumns="false" onrowcommand="Grid_viewprofile_RowCommand"
>
<Columns>
<asp:TemplateField HeaderText="Profile_Status">
<ItemTemplate>
<asp:DropDownList ID="ddl_groub" runat="server" DataSource='<%# ddlist %>' DataTextField="Key" DataValueField="Value" >
</asp:DropDownList>
<asp:Button ID="btn_move" runat="server" Text="Move to group" CommandName="Move" />
<asp:Button ID="btn_close" runat="server" Text="Close" CommandName="Close"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</GridView>

protected void Grid_viewprofile_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Close")
{
Panel1.Visible = false;
}
if (e.CommandName == "Move")
{
how to get dropdownlist selected item value from here
}
}



gowthaman8870226416


Responses

Posted by: Jayakumars on: 10/21/2012 [Member] [MVP] Bronze | Points: 50

Up
0
Down

Resolved
hi
Gow.Net

try this Code

Server Side

SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["DbConnect"].ToString());
SqlDataAdapter sqladp = new SqlDataAdapter();
DataTable dt = new DataTable();
DataTable dt1 = new DataTable();
DataTable dt2 = new DataTable();


protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataSet ds=new DataSet ();
ds = ddlist("Select * from UserDetails");
Grid_viewprofile.DataSource = ds;
Grid_viewprofile.DataBind();
}

}



protected void Grid_viewprofile_RowCommand(object sender, GridViewCommandEventArgs e)

{

if (e.CommandName == "Close")

{

// Panel1.Visible = false;

}

if (e.CommandName == "Move")

{
GridViewRow gvRow = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
DropDownList lstuserName = (DropDownList)gvRow.FindControl("ddl_groub");
Response.Write("Selected Value : " + lstuserName.SelectedValue + " " + "Selected Item : " + lstuserName.SelectedItem.Text);

}

}

public DataSet ddlist(string Qry)
{
DataSet ds = new DataSet();
SqlDataAdapter sqladp = new SqlDataAdapter(Qry, sqlcon);
sqladp.Fill(ds);
return ds;
}



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

DropDownList ddl = (DropDownList)e.Row.FindControl("ddl_groub");
ddl.DataSource = ddlist("Select * from UserDetails");
ddl.DataTextField = "UserName";
ddl.DataValueField = "ID";
ddl.DataBind();
ddl.Items.Insert(0, new ListItem("--Select--", "0"));
}
}


}




Client Side

<asp:GridView ID="Grid_viewprofile" CellPadding="4" ForeColor="Black"

runat="server" AllowPaging="true" PageSize="1"

AutoGenerateColumns="false" onrowcommand="Grid_viewprofile_RowCommand"
onrowdatabound="Grid_viewprofile_RowDataBound">



<Columns>

<asp:BoundField DataField="ID" HeaderText="ID" />
<asp:BoundField DataField="UserName" HeaderText="UserName" />



<asp:TemplateField HeaderText="Profile_Status">

<ItemTemplate>

<asp:DropDownList ID="ddl_groub" runat="server">
</asp:DropDownList>

<asp:Button ID="btn_move" runat="server" Text="Move to group" CommandName="Move" />

<asp:Button ID="btn_close" runat="server" Text="Close" CommandName="Close"/>

</ItemTemplate>

</asp:TemplateField>

</Columns>

</asp:GridView>


Mark as Answer if its helpful to you

Kumaraspcode2009@gmail.com

Gow.Net, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sourabh07 on: 10/20/2012 [Member] Starter | Points: 25

Up
0
Down
hi..
Please try this code inside the row_command event of the GridView...



if (e.CommandName == "Move")

{

DropDownList ddl_groub=e.Row.FindControl("ddl_groub") as DropDownList;

string str_var = ddl_groub.SelectedValue.ToString().Trim();

}


Sourabh07

Gow.Net, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jayakumars on: 10/21/2012 [Member] [MVP] Bronze | Points: 25

Up
0
Down
hi
gow.net
this is wotking in my pc check this image.

 Download source file

Mark as Answer if its helpful to you

Kumaraspcode2009@gmail.com

Gow.Net, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Gow.Net on: 10/22/2012 [Member] Starter | Points: 25

Up
0
Down
hi Sourabh07 in your code i got following error

 DropDownList ddl_groub = e.Row .FindControl("ddl_groub") as DropDownList;


Error

'System.Web.UI.WebControls.GridViewCommandEventArgs' does not contain a definition for 'Row' and no extension method 'Row' accepting a first argument of type 'System.Web.UI.WebControls.GridViewCommandEventArgs' could be found (are you missing a using directive or an assembly reference?)



gowthaman8870226416

Gow.Net, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response