Subscribe to Subscribers
Talk to Webmaster Tony John

Resources » Code Snippets » ASP.NET GridView

How to use DropDownList in GridView


Posted Date:     Category: ASP.NET GridView    
Author: Member Level: Diamond    Points: 8


In this article I have explained about how to use drop down list in grid view. In this example application I used employee table for bind details in Grid view. One field in the table mention employee retired or not. I show in the grid view Yes/No if user click edit option that two values are displayed in Drop down list for editing that value.



 


How to bind drop down value in grid view during page load?



Client side
use Eval method in grid view drop down selected value

Server side

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
GridData();
}
}
void GridData()
{
sqlcmd = new SqlCommand("select *,CASE empstatus when 'Y' then 'Yes' else 'No' END as STATUS from emp", sqlcon);
sqlcon.Open();
da = new SqlDataAdapter(sqlcmd);
dt.Clear();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}

How to update Grid view drop down value?

protected void GridView1_RowUpdating(object sender,GridViewUpdateEventArgs e)
{

GridViewRow row = GridView1.Rows[e.RowIndex];
string eno;

eno = GridView1.DataKeys[e.RowIndex].Value.ToString();

TextBox empname = (TextBox)row.FindControl("txtempname");
DropDownList status = (DropDownList)row.FindControl("DropDownList1");
string selval;

if (status.SelectedValue == "Yes")
{
selval = "Y";
}
else
{
selval = "N";
}

sqlcon.Open();
sqlcmd = new SqlCommand("update emp set empname='" + empname.Text + "',empstatus='" + selval + "' where eno='" + eno + "'",sqlcon);
sqlcmd.CommandType = CommandType.Text;
sqlcmd.ExecuteNonQuery();
sqlcon.Close();
GridView1.EditIndex = -1;
GridData();
}

Source Code Detail:
Here with I have attached source code to show drop down list in Gridview. Download it and try to update values using that drop down list.
Front End : ASP.NET
Code Behind : C#

Conclusion:
I hope this Article is help to you understand Drop down list in Grid View.

Attachments
  • GridView_DropDownList (42858-221021-GridViewDDL.rar)





  • Did you like this resource? Share it with your friends and show your love!


    Responses to "How to use DropDownList in GridView"
    Feedbacks      

    Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Next Resource: Different ways of using Hyperlink control in data controls
    Previous Resource: How to change grid view row back color based on condition using ASP.NET
    Return to Resources
    Post New Resource
    Category: ASP.NET GridView


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    How to bind grid view dropdownlist value?  .  
    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2012 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.