Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 689263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:18:51+00:00 2026-05-14T02:18:51+00:00

I am trying to write a method, that takes a ComboBox, a DataTable and

  • 0

I am trying to write a method, that takes a ComboBox, a DataTable and a TextBox as arguments. The purpose of it is to filter the members displayed in the ComboBox according to the TextBox.Text. The DataTable contains the entire list of possible entries that will then be filtered. For filtering, I create a DataView of the DataTable, add a RowFilter and then bind this View to the ComboBox as DataSource.

To prevent the user from typing into the ComboBox, I choose the DropDownStyle DropDownList. That’s working fine so far, except that the user should also be able to choose nothing / empty line. In fact, this should be the default member to be displayed (to prevent choosing a wrong member by accident, if the user clicks through the dialog too fast).

I tried to solve this problem by adding a new Row to the view. While this works for some cases, the main issue here is that any DataTable can be passed to the method. If the DataTable contains columns that cannot be null and don’t contain a default value, I suppose I will raise an error by adding an empty row.

A possibility would be to create a view that contains only the column that is defined as DisplayMember, and the one that is defined as ValueMember. Alas, this can’t be done with a view in C#. I would like to avoid creating a true copy of the DataTable at all cost, since who knows how big it will get with time.

Do you have any suggestions how to get around this problem?

Instead of a view, could I create an object containing two members and assign the DisplayMember and the ValueMember to these members? Would the members be passed as reference (what I hope) or would true copied be created (in which case it would not be a solution)?

Thank you very much for your help!

Best regards

public static void ComboFilter(ComboBox cb, DataTable dtSource, TextBox filterTextBox)
{
 cb.DropDownStyle = ComboBoxStyle.DropDownList;
 string displayMember = cb.DisplayMember;
 DataView filterView = new DataView(dtSource);
 filterView.AddNew();
 filterView.RowFilter = displayMember + " LIKE '%" + filterTextBox.Text + "%'";
 cb.DataSource = filterView;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T02:18:52+00:00Added an answer on May 14, 2026 at 2:18 am

    Ok, by chance, I stumbled over a working solution: Any changes to a DataView – such as adding a new row – are supposed to be completed using .EndEdit. If you don’t do that, you’ll be confronted with side effects, such as the added row not being sorted properly. BUT: by not adding .EndEdit, you’ll also get an advantage: C# won’t check, if any of the columns in the underlying DataTable (dtSource) does not allow null!

    So, as a solution, I added .EndEdit to a try-Block. If the DataTable allows null for every column, it will work and the empty line will appear on top of the combobox. If it doesn’t allow null, EndEdit won’t be executed, the empty line will still be added, at the bottom of the ComboBox, however.

    Please note that if you have set autoincrement in dtSource, the empty line will also return a SelectedValue (most likely -1). This needs to be considered when using this method!

    Cheers!

    public static void ComboFilter(ComboBox cb, DataTable dtSource, TextBox filterTextBox)
    {
        cb.DropDownStyle = ComboBoxStyle.DropDownList;
        string displayMember = cb.DisplayMember;
        DataView filterView = new DataView(dtSource);
        DataRowView newRow = filterView.AddNew();
        newRow[displayMember] = "";
        try { newRow.EndEdit(); }   // fails, if a column in dtSource does not allow null
        catch (Exception) { }       // works, but the empty line will appear at the end
        filterView.RowFilter = displayMember + " LIKE '%" + filterTextBox.Text + "%'";
        filterView.Sort = displayMember;
        cb.DataSource = filterView;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 531k
  • Answers 531k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Seems like what you want to do is put an… May 16, 2026 at 11:56 pm
  • Editorial Team
    Editorial Team added an answer These pages show all the attributes you can apply to… May 16, 2026 at 11:56 pm
  • Editorial Team
    Editorial Team added an answer You need to download and install the db2 client only.… May 16, 2026 at 11:56 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I'm trying to write a method that acts as a setter and takes some
I'm trying to write a method for an app that takes a chemical formula
I have a method that takes a System.Web.UI.Page as an input and returns some
I'm trying to write a method to take a multiline tab-delimited file and return
I am new to programming and Java and trying to write a program which
I'm trying to write my first iPhone app and i'm having some trouble with
I'm trying to load JSON back into an object. The loads method seems to
I ran into the problem that two drives connected at nearly the same time
My knowledge of Lambda expressions is a bit shaky, while I can write code
I know that this is probably something easy to implement, but I am having

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.