Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Good day all,

I have this dropdownlist :

<asp:DropDownList ID="DropDownList1" 
    runat="server" 
    AutoPostBack="true" 
    DataSourceID="SqlDataSource1" 
    DataTextField="Categorie" 
    DataValueField="Cat_ID" 
    >
</asp:DropDownList>

and the sqldatasource select * all from [tbl_Cat]

Its used to filter the database on category. It works perfectly but it only shows the 3 categories that are in the tbl_Cat(duh) But I also want a select all item in de ddl. The dropdownlist and the datagrid are not made with code-behind. Is it possible to entry a select - all record trough codebehind?

Kind regards, and thanks for your time

share|improve this question

3 Answers

You need to write the below code which help you to select all option for category.

 <asp:DropDownList ID="DropDownList1" AutoPostBack="true" runat="server">
 </asp:DropDownList>

In Code behind file

SqlConnection con = new SqlConnection(str);
string com = "select * all from tbl_Cat";
SqlDataAdapter adpt = new SqlDataAdapter(com, con);
DataTable dt = new DataTable();
adpt.Fill(dt);

DropDownList1.DataSource = dt;
DropDownList1.DataBind();
DropDownList1.DataTextField = "Categorie";
DropDownList1.DataValueField = "Cat_ID";
DropDownList1.DataBind();
DropDownList1.Items.Insert(0, new ListItem("Select ALL", "0"));

Now you can check if drop down selected value is 0, if it is 0 then you can load all the records in the grid.

Let me know if any thing i miss.

share|improve this answer
it says : Compiler Error Message: BC30188: Declaration expected. At:SqlConnection con = new SqlConnection(str); – DiederikEEn Apr 24 at 12:26

following is the way to bind DropDownList from codebehind. visit this link for details

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

CODE BEHIND

SqlConnection con = new SqlConnection(str);
string com = "select * all from tbl_Cat";
SqlDataAdapter adpt = new SqlDataAdapter(com, con);
DataTable dt = new DataTable();
adpt.Fill(dt);

DropDownList1.DataSource = dt;
DropDownList1.DataBind();
DropDownList1.DataTextField = "Categorie";
DropDownList1.DataValueField = "Cat_ID";
DropDownList1.DataBind();
share|improve this answer

May be you have this query,

DropDownList1.Items.Add(new ListItem("Select All", "0"));
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.