组合框的所有选项 [英] All option to combo box

查看:91
本文介绍了组合框的所有选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我已经使用Visual Studio 2008在Windows C#中开发了一个系统.我需要在组合框中添加全部"选项,以选择组合框中的所有数据以查看报告.现在我只能从组合中选择单个选项,但是我需要有一个选项来选择组合顶部的所有选项.

我已经尝试了很多方法,但是没有用.请一个人简短地帮助我.
他是我的密码...

这是课程代码...

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...

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...

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


请有人能清楚地帮助我,因为我对开发不是很熟悉..

谢谢...

如果有人支持我的情况,请.如果愿意,我可以发送项目检查u [/Edit]


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

Thanks...

Please if any one can support in my case. if wish i can send my project to check for u[/Edit]

推荐答案

将查询更改为->
Change your query to ->
string sqlstr = "SELECT DISTINCT Work_Location FROM Employee UNION ''ALL'' FROM Employee";



然后,当您在组合框中选择ALL时,适当地处理组合数据.



Then when you select ALL in the combobox, process combo data appropriately.


您可以在代码中将"All"插入到数据表中,就像这样

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";


您需要像这样将行添加到数据表中

You need to add a Row to your datatable like this

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




顺便说一句,当您打开它时,您还应该在FillCombo方法中关闭数据库连接,但它永远不会关闭.最好的选择是使用这样的Using语句




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

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


这篇关于组合框的所有选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆