Click here to Skip to main content
15,893,381 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,

I had got multi forms with a common combo box object in many forms, which contains session/financial year. What I'm doing right now is writing again and again same code for the combo object.

Now, what I need is a class where I just write the code and can access for combo box on any form by referring to that class.

And I donno how to do that...

efforts I did : GOOGLE for this and tried by myself by creating a class and wrote code in it but couldn't achieve the target.

code:

C#
class financial_session
    {
        connectionString cs = new connectionString();
            
                OleDbConnection database = new OleDbConnection(cs.connString());
                database.Open();

                string queryString = "Select distinct(bc_session) from cm_master order by bc_session desc";

                OleDbCommand cmd = new OleDbCommand(queryString, database);
                OleDbDataReader dr = cmd.ExecuteReader();

                cmbSession.Items.Clear();

                while (dr.Read())
                {
                    cmbSession.Items.Add(dr.GetValue(0).ToString());
                }
                database.Close();

    }
Posted
Updated 30-Jul-14 22:08pm
v2

Make a class and create a method to bind data into combo box control.

Here is the sample code to bind the combobox control.

C#
public void BindComboBoxDataContext(ComboBox comboBox, DataTable dtDataContext, string displayMemberPath, string selectedValueMemberPath)
        {
            try
            {
                comboBox.DataContext = dtDataContext;
                comboBox.DisplayMemberPath = displayMemberPath;
                comboBox.SelectedValuePath = selectedValueMemberPath;
                comboBox.SelectedIndex = -1;
            }
            catch (Exception ex)
            { LogExceptionDetails(ex); }
        }


Whenever required to set the data for the control, just make an object for this class and call this method.
 
Share this answer
 
Comments
sachees123 31-Jul-14 6:36am    
thanks for your prompt reply, im getting an error

Error 3 Expected class, delegate, enum, interface, or struct
Sharmanuj 31-Jul-14 6:44am    
This is the method provided which you have to embed into a class. Then use on your forms.
Sharmanuj 31-Jul-14 6:46am    
you have to include the required namespaces as well on the class which you'll prepare.
sachees123 31-Jul-14 6:46am    
I did the same , here is the code, plz tell me where i'm doing wrong

using System;
using System.Collections.Generic;
using System.Text;

namespace MINE.classes
{
class bc_session
{
}
public void BindComboBoxDataContext(ComboBox comboBox, DataTable dtDataContext, string displayMemberPath, string selectedValueMemberPath)
{
try
{
comboBox.DataContext = dtDataContext;
comboBox.DisplayMemberPath = displayMemberPath;
comboBox.SelectedValuePath = selectedValueMemberPath;
comboBox.SelectedIndex = -1;
}
catch (Exception ex)
{ }
}
}
Sharmanuj 31-Jul-14 6:47am    
Try This code

using System;
using System.Collections.Generic;
using System.Text;

namespace MINE.classes
{
class bc_session
{

public void BindComboBoxDataContext(ComboBox comboBox, DataTable dtDataContext, string displayMemberPath, string selectedValueMemberPath)
{
try
{
comboBox.DataContext = dtDataContext;
comboBox.DisplayMemberPath = displayMemberPath;
comboBox.SelectedValuePath = selectedValueMemberPath;
comboBox.SelectedIndex = -1;
}
catch (Exception ex)
{ }
}
}
}
Create a UserControl, and either derive it from ComboBox or embed a ComboBox in it.
Add your code to that class, and use the next control instead of the "raw" ComboBox in all your forms.

It's easier than it sounds - I do it all the time, even if I will only be using the control once (since it encapsulates the control functions, and simplifies the form code significantly).
MSDN[^] can help with the details.
 
Share this answer
 

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