Click here to Skip to main content
15,884,177 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,

I have developed a System in windows C# using visual studio 2008. I need to add 'all' option to my combo box to select all the data's in the combo box to view the report. now i can select only single option from the combo, but i need to have an option to select all in that's top of the combo.

I have tried in so many ways but it not works. Please can some one help me in brief.
He is my code...

this is class code...
C#
public DataTable fillcombo()
        {
            string sqlstr = "SELECT DISTINCT Work_Location FROM Employee";
            con = new OleDbConnection(clsConnection.strconnection);
            con.Open();
            
            da = new OleDbDataAdapter();
            da.SelectCommand = new OleDbCommand(sqlstr, con);
            da.SelectCommand.ExecuteNonQuery();
            DataTable ds = new DataTable();
            da.Fill(ds);
            return ds;
        } 

here is form load code...
C#
private void rptMain_Load(object sender, EventArgs e)
        {
            try
            {
                Classes.clsReport rpt = new clsReport();
                DataTable dt = new DataTable();
                dt = rpt.fillcombo();
                EmpNo.DataSource = dt;
                EmpNo.ValueMember = "Work_Location";
                
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message);
            }
        }

Please can anyone help me in clearly, because i'm not much familiar in development..

Thanks...

[Edit: OP's comment moved to here]Please if any one can support in my case. if wish i can send my project to check for u[/Edit]
Posted
Updated 21-Jun-11 3:50am
v4

Change your query to ->
string sqlstr = "SELECT DISTINCT Work_Location FROM Employee UNION 'ALL' FROM Employee";


Then when you select ALL in the combobox, process combo data appropriately.
 
Share this answer
 
Comments
fjdiewornncalwe 21-Jun-11 9:47am    
+5. An excellent solution considering that the OP did not mention that he was using Access upfront and that this is a great solution for MS-SQL
ruzan hizar 22-Jun-11 5:04am    
This Query is not working and SELECT have to be there next to UNION. but even it wont output all Values once i select the ALL in combo.
Abhinav S 22-Jun-11 8:01am    
You can simply get all values from the combo boxes source once the client selects ALL in it.
Hi you can insert "All" to your datatable in code like this

dt = rpt.fillcombo();
DataRow _firstrow=dt.NewRow();
_firstrow.ItemArray=new object[] {"All"};
dt.Rows.InsertAt(_firstrow, 0);
EmpNo.DataSource = dt;
EmpNo.ValueMember = "Work_Location";
 
Share this answer
 
Comments
ruzan hizar 20-Jun-11 8:06am    
It shows "All" in combo box but once i select all it wont generate All in the report
ruzan hizar 22-Jun-11 5:02am    
In this way All option coming in the top of the Combo but it wont out put any values
You need to add a Row to your datatable like this

C#
try
           {
               DataTable dt = fillcombo();
               dt.Rows.Add(new object[] {"All"});
               comboBox1.DataSource = dt.DefaultView;
               comboBox1.DataSource = dt;
               comboBox1.ValueMember = "Work_Location";
           }
           catch (Exception ee)
           {
               MessageBox.Show(ee.Message);
           }



By the way you should also Close your Database connection in your FillCombo method, as you Open it but it never gets closed. Your best bet would be to use a Using statement like this

C#
using (OleDbConnection con = new OleDbConnection("yourConnectionString"))
           {
               string sqlstr = "SELECT Work_Location FROM Employees";
               con.Open();
               OleDbDataAdapter da = new OleDbDataAdapter();
               da.SelectCommand = new OleDbCommand(sqlstr, con);
               da.SelectCommand.ExecuteNonQuery();
               DataTable ds = new DataTable();
               da.Fill(ds);
               return ds;
           }


Hope this helps
 
Share this answer
 
Comments
ruzan hizar 20-Jun-11 7:47am    
Pls brother shall i have ur mail id then i can send my project. then u can see.
shall i have?
ruzan hizar 22-Jun-11 5:06am    
In this way All option coming in the top of the Combo but once select the All option in the combo It wont output all values. i mean it wont take all the values in the combo.
Thanks.
I'll give you a simple way(AppendDataBoundItems is nice trick)
<asp:dropdownlist id="DropDownList1" appenddatabounditems="true" runat="server" datatextfield="Work_Location" datavaluefield="Work_Location" xmlns:asp="#unknown">
    <asp:listitem text="ALL" value="" />   
</asp:dropdownlist>

Code behind
DropDownList1.DataSource = fillcombo();
DropDownList1.DataBind();

Further reading
ListControl.AppendDataBoundItems Property in ASP.NET[^]

BTW it will be better if you mention values to DataValueField of the DropDownList.
Work_LocationID Work_Location
1                London
2                Moscow
3                Paris

So assign the Work_LocationID to DataValueField of the DropDownList.
<asp:dropdownlist id="DropDownList1" appenddatabounditems="true" runat="server" datatextfield="Work_Location" datavaluefield="Work_LocationID" xmlns:asp="#unknown">
    <asp:listitem text="ALL" value="" />   
</asp:dropdownlist>
 
Share this answer
 
Comments
ruzan hizar 22-Jun-11 5:07am    
Will this method support for C#? if so please can u explain Bit clearly.

Thanks.
thatraja 22-Jun-11 5:18am    
Of course, I gave you in C#. AppendDataBoundItems is simple option. For more read the link.
thatraja 16-Jul-11 23:07pm    
Hi, Did you click Vote 1 accidentally? In CP Vote 5 for best answers. Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900