DataGridView Question from Newbie

ellpee

Active member
Joined
Apr 16, 2023
Messages
27
Programming Experience
10+
Hi again all. Still taking baby steps to learn how C#/VS work. Form design project, datagridview, linked to a very simple 2-table MSAccess database. All good to that point, understand what code fills my grid with either table A or Table B. Now trying to spread my wings a little further by having the grid empty on open, use radio buttons to pick A or B, then a "launch button" to (1) set the datasource and (2) display the new data. The fill event apparently happens during the form load, but not clear how to "change my mind" after the load event and populate the grid with whatever I select at that point. I can probably write the necessary code, just not sure where to put it as it seems to me it would involve some kind of a ?reload? or ?refresh? event and I don't know of C#/VS has such a thing. Help welcome, once again. Not urgent, just learning how to code and chew gum.
 
Have you created a typed DataSet and set up the bindings in the designer or are you using an untyped DataSet in code only? The general advice is the same but the specifics will differ. If you're not sure, show us this Fill call you mention and that should be enough to work it out.
 
Have you created a typed DataSet and set up the bindings in the designer or are you using an untyped DataSet in code only? The general advice is the same but the specifics will differ. If you're not sure, show us this Fill call you mention and that should be enough to work it out.

This time everything (so far) was generated by the designer, I just add a widget or change a property and see how that looks in designer-generated code. FWIW, here's the code for the load that binds the datagrid to either table A or table B. (I changed the datasource property manually just to see how the two possibilities would differ.) What I want to try now just as a learning experience is to change the datasource dynamically AFTER the form has opened, and my humble approach to that is two radio buttons and a button to execute a ?re-load? that will change what I see in the datagrid. In VBA that would be a requery, not sure how to handle it in C#/VS. If the form is already loaded, can the following just be called again with a different datasource? Greatly appreciate your interest and ideas.
C#:
        private void Form1_Load(object sender, EventArgs e)
        {
            this.requestorsTableAdapter.Fill(this.cSTestDB2DataSet.Requestors);
            //this.productsTableAdapter.Fill(this.cSTestDB2DataSet.Products);
        }
 
Last edited by a moderator:
You are using a typed DataSet and all the bindings are configured in the designer. You should notice that you have a BindingSource that is bound to a particular DataTable in that DataSet and then the DataGridView is bound to that. If you don't want any data displayed when the form loads, unbind the grid and clear its columns.

The next question is whether you want to populate both DataTables at load and then just switch between them without reloading data or load new data each time the user switches.
 
You are using a typed DataSet and all the bindings are configured in the designer. You should notice that you have a BindingSource that is bound to a particular DataTable in that DataSet and then the DataGridView is bound to that. If you don't want any data displayed when the form loads, unbind the grid and clear its columns.

The next question is whether you want to populate both DataTables at load and then just switch between them without reloading data or load new data each time the user switches.

Yes, I see the binding sources in code, and almost understand what that's about. What I'm trying to do this iteration is, instead of having the form load with the grid bound to either Table A or Table B, have it load empty and have the user (Me!) pick which table to load into the grid. AND, have the user be able to change his mind and choose the other table, all while the form is still loaded. No particular end goal involved, just trying out various what-ifs to see if I can make it happen (and to get C#-smarter by studying the designer-written code as I go.

So, I tried using two radio buttons and a go button and having the button click event just change the data source for the grid, but so far that doesn't work, still experimenting. Thinking maybe the code to change the data source and reload the grid needs to be somewhere else than in the button click event?
 
You do need to change the data source but there's more to it. I'm assuming that the two tables have different schemas, so the grid will need to display different columns based on table selected. I would suggest that you have two BindingSources that are permanently bound to the two DataTables. When you want to change the table displayed, set the DataSource to null, which will remove all the rows, then remove all the columns yourself. You can then bind the other BindingSource and that will automatically generate the appropriate columns.
 
Or do the old Win31 strategy of having two controls and just making one visible and the other invisible.
 
I prefer that "two controls" approach too, as it's more seamless with strongly typed datasets and designer bound controls

Add a dataset type of file to your project. Open it, right click, Add a table adapter, choose the access data base within the wizard, choose the table A (or if doing it by SQL, a query of SELECT * FROM TableA ) . Repeat add another tableadapter for table B. Switch to forms designer, add a tab control, open data sources panel (view menu>>other windows), drag tableA out of data sources and onto tab page 1, switch to (or add a) tab page 2, drag tableB onto it

You now have table A and B and an implicit "radio button" behavior to swap between them.

Note, this post was written from memory of what the tableadapter wizard did when using Access- it's been many years since. I think you can also add an access db file to the project and then drag it into a dataset designer to get a table picker.. if you get stuck with either of these approaches post a screenshot and it'll jog my memory of where to go

Note also, you should do this in a Net Framework 4.8 project, not a net core (net 5, 6, 7 etc) - the data binding designers flat out don't work in netcore projects so MS turned them off

Note three, I appreciate its baby steps, learning project etc, but I don't personally think you're doing yourself any favours using an access database here. SQLServer express or localdb would teach you more relevant things; few real life scenarios use Access
 
Last edited:
Back
Top Bottom