8 Replies - 3591 Views - Last Post: 18 July 2012 - 05:15 PM Rate Topic: -----

#1 Chris.Bertsch  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 23-January 12

Filtering a DataGrid with a ComboBox

Posted 18 July 2012 - 10:13 AM

This probably has a really simple solution, but I've been playing around with this for a few hours now...I have a combobox on my form, that when you change the value, I need it to filter the datagrind. That's fine, I have that, but when I use the code below, it unhides all my columns I set to be hidden (manufacture_id column), and the textboxes on my form that use the same bindingsource as my datagrid do not update with each selection change in the datagrid, they only display the values from the first row in the datagrid...only time they will update is when I use the next and back buttons on the navigator, but then it doesn't change the current selection on the datagrid...

So, I have two problems I need to solve.
1) get my textboxe.text values to update when you select a new cell or row in the datagrid. (and yes, they are using the same binding source as the datagrid.)
2) Have the manufacture_ID column stay hidden in the datagrid...even though I have it set that way through properties..

here is a little of my code for the filter..

    public partial class frmChapters : Form
    {
        DataView dv = new DataView();
        int index = 0;
        SqlConnection cs = new SqlConnection(ConfigurationManager.ConnectionStrings["QuikQuote.Properties.Settings.QuikQuoteConnectionString"].ConnectionString);
        SqlDataAdapter da = new SqlDataAdapter();

        public frmChapters()
        {
            InitializeComponent();
        }

        private void LoadGridView()
        {
            int ManufactureID = Convert.ToInt32(cmbobxManufacturers.SelectedValue);

            //Fills the Chapters data grid based off combobox selection
            this.chaptersTableAdapter.Fill(this.QuikQuoteDataSet.Chapters);
            //The next two lines of code are causing the problems I believe...
            QuikQuoteDataSet.Tables["Chapters"].DefaultView.RowFilter = "Manufacture_ID = " + ManufactureID;
            flxgrdChapters.DataSource = QuikQuoteDataSet.Tables["Chapters"];
            this.Text = cmbobxManufacturers.Text + " Chapters";
        }

        private void frmChapters_Load(object sender, EventArgs e)
        {
            this.manufacturersTableAdapter.Fill(this.QuikQuoteDataSet.Manufacturers);
            LoadGridView();
        }

        private void cmbobxManufacturers_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadGridView();
        }


I have very little programming experience (mostly VB.NET), so if any of that code is poorly written, that's why.

Thanks in advance to anyone that can help me out!

Is This A Good Question/Topic? 0
  • +

Replies To: Filtering a DataGrid with a ComboBox

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3617
  • View blog
  • Posts: 11,269
  • Joined: 05-May 12

Re: Filtering a DataGrid with a ComboBox

Posted 18 July 2012 - 11:54 AM

Do you have you DataGrid setup to automatically create columns? If so, each time you change the DataSource, it will go create columns.
Was This Post Helpful? 0
  • +
  • -

#3 Chris.Bertsch  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 23-January 12

Re: Filtering a DataGrid with a ComboBox

Posted 18 July 2012 - 01:29 PM

I'm sorry, I'm not sure what you mean by having my datagrid set up to automatically add columns. I drug the datagrid onto the form from the toolbox, selected it's datasource, and in the edit columns dialog, I uncheck the "visible" properties. When I comment out the code...

QuikQuoteDataSet.Tables["Chapters"].DefaultView.RowFilter = "Manufacture_ID = " + ManufactureID;
flxgrdChapters.DataSource = QuikQuoteDataSet.Tables["Chapters"];


...the textboxes will update depending on the currently selected cell in the datagrid like I want, however, my combobox will not filter the datagrid anymore.
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3617
  • View blog
  • Posts: 11,269
  • Joined: 05-May 12

Re: Filtering a DataGrid with a ComboBox

Posted 18 July 2012 - 02:00 PM

Sorry, I was thinking WPF DataGrid. I assume you are playing with the WinForms DataGrid based on your answer above.
Was This Post Helpful? 0
  • +
  • -

#5 Chris.Bertsch  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 23-January 12

Re: Filtering a DataGrid with a ComboBox

Posted 18 July 2012 - 02:24 PM

Yes, Sir, it's a WinForms Datagrid.
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3617
  • View blog
  • Posts: 11,269
  • Joined: 05-May 12

Re: Filtering a DataGrid with a ComboBox

Posted 18 July 2012 - 02:59 PM

Sorry, on WinForms, I always go to the DataGridView for my needs based on advice from senior developers who have told me that "the DataGrid has too many bugs". So I have very little experience with the DataGrid other than an occasional debugging session whenever those bugs come up within older products.

So keep that in mind when I say that the advice on this link looks promising: http://www.bigresour...MJZPUrEhbk.html
Was This Post Helpful? 0
  • +
  • -

#7 Chris.Bertsch  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 23-January 12

Re: Filtering a DataGrid with a ComboBox

Posted 18 July 2012 - 04:47 PM

Sorry, I AM using a DataGridView* I guess I just always called it a datagrid..
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3617
  • View blog
  • Posts: 11,269
  • Joined: 05-May 12

Re: Filtering a DataGrid with a ComboBox

Posted 18 July 2012 - 05:09 PM

In the case of the DataGridView, ensure that AutoGenerateColumns is set to false.
Was This Post Helpful? 1
  • +
  • -

#9 Chris.Bertsch  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 23-January 12

Re: Filtering a DataGrid with a ComboBox

Posted 18 July 2012 - 05:15 PM

Yes! Thank you, Sir! That fixes that problem! Now to just figure out why it will not update my textboxes when I select new rows in the datagridview.

Thanks for all your help!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1