how to add a dropdownlist to gridview and how to add a datasource to dropdown.

Posted by Thiruhitech under Regular Expressions on 5/13/2009 | Views : 195984 | Status : [Member] | Replies : 7
i have gridview with 5 colomns c1,c2,c3,c4,c5 here i want to add a dropdownlist only c1 and c2 what is the procedure for do this work

here i am using the code like this but it produce error pls anybody correct that error

error: object reference not set to an instance of an object

code is:

conn.Open();
DataSet ds = new DataSet();
SqlCommand cmp = new SqlCommand("SELECT * from Projectcodetable ", conn);
SqlDataAdapter dr = new SqlDataAdapter(cmp);
dr.Fill(ds);
DropDownList list = (DropDownList)e.Row.FindControl("DropDownList1");

error line ==> list.DataSource = ds; // object reference not set to an instance of an object//

list.DataValueField = "ProjectCode";
list.DataTextField = "ProjectCode";
list.DataBind();
conn.Close();
thanks

note: vs2008,asp.net with c#




Responses

Posted by: Lakn2 on: 5/25/2011 [Member] Starter | Points: 25

Up
0
Down
Do onething
<templatefield>
<itemtemplate>
<asp:dropdownlist id="ddl1" runat="server" onload="ddl1_Load">

in code behind
proteced void ddl1_Load(object sender,EvetArgs e)
{

Dropdownlist ddl=(Dropdownlist)sender;
//remaining everything is same.
use ddl from dropdown

Thanks&Regards
LakshmiNarayana Nalluri.

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

Posted by: Zeeshan on: 4/15/2012 [Member] Starter | Points: 25

Up
0
Down
can you please give me complete code aspx/.cs/.js if any

Worm Regards,
Zeeshan Ansari
Sr. dotnet Trainer

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

Posted by: Veerac85 on: 4/17/2012 [Member] Starter | Points: 25

Up
0
Down
you will find complete instructions on doing this at this below link

http://technico.qnownow.com/2012/04/05/use-dropdownlist-in-asp-net-gridview-control/

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

Posted by: Syedshakeer on: 5/14/2009 [Member] Starter

Up
0
Down
Hi
chek the following linkhttp://www.aspnettutorials.com/tutorials/controls/dropdownlist-gridview-csharp.aspx

Syed Shakeer Hussain

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

Posted by: Syedshakeer on: 5/21/2009 [Member] Starter

Up
0
Down
Hi once again

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();

}


Give Feed back about my coding:
Thanks For Posting in Dotnetfunda

Syed Shakeer Hussain

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

Posted by: Blessybaby on: 6/1/2009 [Member] Starter

Up
0
Down
Hi,

You can also use DataBound event of the grid view for binding data to drop down. In this event you should find the drop down list by using e.item.findcontrol("<control>") and set data source and other properties.

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

Posted by: Anup1252000 on: 8/2/2009 [Member] Starter

Up
0
Down
go through this
http://www.highoncoding.com/Articles/169_DropDownList_Inside_GridView__Method_1_.aspx

https://wordpress.com/pages/anuphosur.wordpress.com

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

Login to post response