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?
 
Dustin SaundersConnect With a Mentor Director 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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.