Windows Develop Bookmark and Share   
 index > Windows Forms Designer > Combobox
 

Combobox

Hi all,

I have a combo box that is bound to a database table.
How do i restrict the items displayed to the user; for example, i do not want to display names that containthe character "%" in c#

thanks
omogen  Thursday, September 24, 2009 1:08 PM
Hi omogen,

ComboBox’s items cannot be added or removed once it is data bound. Do the filter in the data source is a proper way. Please look at the following code.
dtComboBox.Columns.Add("Name");
dtComboBox.Columns.Add("Show");
dtComboBox.Rows.Add("John", true);
dtComboBox.Rows.Add("Mike", true);
dtComboBox.Rows.Add("%Kira", true);

foreach (DataRow dr in dtComboBox.Rows)
{
if (dr["Name"].ToString().Contains('%'))
{
dr["Show"] = false;
}
}

dtComboBox.DefaultView.RowFilter = "Show=true";

comboBox1.DataSource = dtComboBox;
comboBox1.DisplayMember = "Name";
dtComboBox is a datatable which will be used to do the databinding. In the example, I have added a column called “Show� At first, all the rows�Show value is set to true. Then put a loop to check if the name contains �� Set Show to false for the row whose name contains �� After that, you can use RowFilter to show the rows whose “Show�value is true.

If you have any problems with my solution, please feel free to tell me.

Sincerely,
Kira Qian
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework!
Kira Qian  Monday, September 28, 2009 6:17 AM

Hi Omogen,

1. Put the result set of your query into a DataTable

dim myDataTable = <your query>

2. Change the DefaultView of your DataTable. That is to exclude those items you dont need

myDataTable.DefaultView.RowFilter = <your filter criteria>

3. Then assign your DataTable as the Datasource of your comboBox

myComboBox.DataSource = myDataTable

Hope this will work...


ricky_b_nino  Monday, September 28, 2009 10:05 AM
Why not just update the SQL Select query that pulls the list to filter out those?
MCP
webJose  Thursday, September 24, 2009 5:37 PM

I would like to update the c# code not the query

omogen  Friday, September 25, 2009 8:37 AM
Hi omogen,

ComboBox’s items cannot be added or removed once it is data bound. Do the filter in the data source is a proper way. Please look at the following code.
dtComboBox.Columns.Add("Name");
dtComboBox.Columns.Add("Show");
dtComboBox.Rows.Add("John", true);
dtComboBox.Rows.Add("Mike", true);
dtComboBox.Rows.Add("%Kira", true);

foreach (DataRow dr in dtComboBox.Rows)
{
if (dr["Name"].ToString().Contains('%'))
{
dr["Show"] = false;
}
}

dtComboBox.DefaultView.RowFilter = "Show=true";

comboBox1.DataSource = dtComboBox;
comboBox1.DisplayMember = "Name";
dtComboBox is a datatable which will be used to do the databinding. In the example, I have added a column called “Show� At first, all the rows�Show value is set to true. Then put a loop to check if the name contains �� Set Show to false for the row whose name contains �� After that, you can use RowFilter to show the rows whose “Show�value is true.

If you have any problems with my solution, please feel free to tell me.

Sincerely,
Kira Qian
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework!
Kira Qian  Monday, September 28, 2009 6:17 AM

Hi Omogen,

1. Put the result set of your query into a DataTable

dim myDataTable = <your query>

2. Change the DefaultView of your DataTable. That is to exclude those items you dont need

myDataTable.DefaultView.RowFilter = <your filter criteria>

3. Then assign your DataTable as the Datasource of your comboBox

myComboBox.DataSource = myDataTable

Hope this will work...


ricky_b_nino  Monday, September 28, 2009 10:05 AM

You can use google to search for other answers

Custom Search

More Threads

• How can I hide a property? (DockPadding)
• IToolboxService and the DesignSurface class
• PointF Type Converter
• Cannot CreateDesigner for MainMenu
• Can you hide an item in the component tray at design time
• Manipulating data from two excel sheets into one using Visual Studio 2005
• ControlDesigner at runtime
• How can I implement the property DataBindings in a Smart Tag of a control
• Windows Form Design Time Error
• Hosting a designsurface within a designer