C# Windows Form Search Gridview from databound combobox using foreign key

I have a windows form with a Gridview linked to a dataset.  I want to filter the gridview by a databound combobox which shows the display member but searches by the value member.  The datasource of the combobox is bound to a table that links to the gridview table with a foreign key.  So the value I need to filter by ultimately is an Int, but that would not make sense to the user, so the display member is the descriptive text value.  When writing the code behind, I get an error cannot convert from string to text.  This makes me believe that I did not configure it correctly to pass the int to the filter, and it is trying to use the text that is displayed in the combobox.  Any suggestions?

  private void button1_Click(object sender, EventArgs e)
        {
            try

                {
                    this.tbl_RateTableAdapter.FillBy(this.ds_FeedRatebyCommodity.tbl_Rate, comboBox1.Text);
                }

                catch (System.Exception ex6)

                {
                    System.Windows.Forms.MessageBox.Show(ex6.Message);
                }
            }

Open in new window

LVL 5
rtayIT DirectorAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dustin SaundersDirector of OperationsCommented:
See this example:
public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private DataTable dataTable = new DataTable();

        private void Form1_Load(object sender, EventArgs e)
        {
            string[] columns = { "ID", "Value1", "Value2", "Value3", "TypeID" };
            foreach (string column in columns)
            {
                DataColumn dataColumn = new DataColumn { ColumnName = column };
                dataTable.Columns.Add(column);
            }

            Random r = new Random();
            int j;

            for(int i = 0; i <100; i++)
            {
                j = r.Next(1, 6);
                DataRow row = dataTable.NewRow();
                row["ID"] = i.ToString();
                row["TypeID"] = j.ToString();
                row["Value1"] = "Value1_" + i.ToString();
                row["Value2"] = "Value2_" + i.ToString();
                row["Value3"] = "Value3_" + i.ToString();
                dataTable.Rows.Add(row);
            }

            dataGridView1.DataSource = dataTable;

            comboBox1.DataSource = (new DataView(dataTable)).ToTable(true, "TypeID");
            comboBox1.DisplayMember = "TypeID";
            comboBox1.ValueMember = "TypeID";
        }
        
        private void button1_Click(object sender, EventArgs e)
        {
            dataGridView1.DataSource = (new DataView(dataTable, "TypeID = '" + comboBox1.SelectedValue + "'", "ID asc", DataViewRowState.CurrentRows));
        }
    }

Open in new window


You can change the DataGridView source to a new DataView with the RowFilter set to the ComboBox's SelectedValue.
dataGridView1.DataSource = (new DataView(dataTable, "TypeID = '" + comboBox1.SelectedValue + "'", "ID asc", DataViewRowState.CurrentRows));

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.