Results 1 to 2 of 2

Thread: datagridview filter

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2007
    Posts
    694

    datagridview filter

    //this is in the form.cs...
    //step 1
    filterDataGridView1.DataSource = dataSet.Tables[0];

    //this is in a separate class...
    //step2
    private static DataTable dataSource = null;

    if (dataSource == null)
    dataSource = (DataTable)this.DataSource;


    The above works where in the form.cs I use the datasource to be the a datatable i.e. in step 1 above

    Question:
    If I change the datasource in step 1 to be BindingSource i.e.

    DataView defaultView = dataSet.Tables[0].DefaultView;
    BindingSource bs = new BindingSource(); //used for creating the autofilter...
    bs.DataSource = defaultView;
    filterDataGridView1.DataSource = null;
    filterDataGridView1.DataSource = bs;

    Then my code above in step 2 will fail.
    So, how can the code in step 2 be converted to bindingsource i.e
    something like the following?


    filterDataGridView1.DataSource = (BindingSource)dataSet.Tables[0];

    if (dataSource == null)
    dataSource = (BindingSource)this.DataSource;

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,348

    Re: datagridview filter

    First up, there is no point binding the DefaultView. Just bind the DataTable. When you bind a DataTable the data comes from the DefaultView anyway. I'm willing to bet that your DataSet is useless too. Does it contain anything other than the one DataTable? If not, why not just use a DataTable?

    As for your question, it doesn't really make sense because you haven't provided us with a clear picture of the relationships. That said, if you want to get access to the BindingSource then you can get it from the grid's DataSource and if you want DataTable then you can get it from the BindingSource's DataSource. It's no more complex than that. That's where you put them in in the first place so that's where you get them out again.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width