how to add dropdown list in gridview and how to populate data in that dropdown list


Thanks in advance

Recommended Answers

All 4 Replies

Adding a Dropdownlist in gridview and add a Datasource to the dropdownlist

first drag and drop one gridview:
in the below example just i am going to display two columns in a gridview.one column for id and another column displaying a dropdownlist with data through sqldatasource..

in gridview create a <ItemTemplate> as follows

<asp:GridView ID="GridView1" runat="server" Style="z-index: 100; left: 292px; position: absolute; 
top: 135px" AutoGenerateColumns="False"> 
<Columns> 
<asp:TemplateField HeaderText="ID"> 
<ItemTemplate> 
<asp:Label id="idlbl" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"id")%>' > 
</asp:Label> 
</ItemTemplate> 
</asp:TemplateField> 

//adding a DropdownList in Gridview 

<asp: TemplateField HeaderText="select Salary"> 
<ItemTemplate> 
<asp: DropDownList ID="ddl" runat ="server" DataTextField ="salary" DataValueField ="id" DataSourceID ="SqlDataSource1" > 
</asp: DropDownList> 
</ItemTemplate> 
</asp:TemplateField> </Columns> 
</asp:GridView>

in the above <asp:Dropdownlist > i created one sqldatasouce.
How to create a SqlDataSource?
First Drag and drop on sqldatasource(SqlDataSource1) from a ToolBox on a webpage.then select SqlDataSource1 to bind a Data by clicking on the Right edge of a SqlDataSource.
in above <asp:DropDownlist> i set DataTextField="salary" and DataValueField="id".Here salary and id are coulmns in a emp table.

afer sucessfull Binding data with SqlDataSource looks as Below
[B<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:db1ConnectionString %>"
ProviderName="<%$ ConnectionStrings:db1ConnectionString.ProviderName %>" SelectCommand="SELECT * FROM [emp]">
</asp:SqlDataSource> >


In Page Load you have to bind a Data to Gridview as follows:

protected void Page_Load(object sender, EventArgs e) 
{ 
SqlConnection conn = new SqlConnection("connection string"); 
conn.Open(); 
SqlDataAdapter ad = new SqlDataAdapter("select * from emp", conn); 
DataSet ds = new DataSet(); 
ad.Fill(ds, "emp"); 
GridView1.DataSource = ds.Tables[0]; 
GridView1.DataBind(); 

}

thanks a lot pritesh

if you got your answer mark thread as solved

Hi

Its not easy to to add dropdown list in gridview .you can refer following example.

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridViewRow gvr=GridView1.SelectedRow;
        CheckBox chk = (CheckBox)gvr.FindControl("CheckBox1");
        DropDownList dp = (DropDownList)sender;
        if (dp.SelectedValue == "P")
            chk.Enabled = true;
        else
            chk.Enabled = false;

    }
    protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    {
        GridViewRow gvr = GridView1.SelectedRow;
        Label lblINTIME = (Label)gvr.FindControl("lblInTime");
        lblINTIME.Text = "asasf";  // assign system date time here

    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.SelectedIndex = e.NewEditIndex;
    }

Otherwise you can check this link.It will help you to add dropdown list in gridview

http://www.dotnetspider.com/forum/38274-how-add-DropDownList-checkbox-gridview.aspx

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.