data grid data binding

turrican

Freshman
Joined
Jan 24, 2003
Messages
27
Location
Phoenix, Az
Hello again. My question this week is about data binding. For example, I am attempting to write a simple program taht will have a form with a list box and a data grid as controls. Just for fun I will make it a list of my DVD's that can be searched by genre.
So if the user selects comedy in the list box, a list of all of the comedy dvds will apear in the datagrid. My problem though is that I am setting the datagrid's data source and data member properties at design time. What I did was generate a dataset for each genre of movie (Kids, action, drama, etc) but I can only bind one data set to the flex grid at a time, so when the user chooses the first option the movies appear in the data grid perfectly but when you chose the next option nothing shows up. Can I set these properties so that the databinding takes place after the user makes his choice? I guess this would be considered at run time right?
 
Im sorry could you give me an example of the proper syntax for that? Im sorry,if this is a lame question, Im new and Im learning this stuff on my own with only a few books and this forum. thank you for your time and your help.
BTW your picture kind of scares me.
 
Well, I'm more or less the blind leading the blind here as I've never actually written anything in ADO.NET yet, but if I'm not mistaken the proper syntax would be..

(Using C#)
dataGrid1.DataSource = yourDataSet.Tables["YourTable"].DefaultValue;

(Using VB)
dataGrid1.DataSource = yourDataSet.Tables("YourTable").DefaultValue
 
I still cant get it to work. It seems like I should bind the dataset to the datagrid after the user makes his selection in the listbox. I need to bind the data that corresponds with the selection in the listbox. does this make sense?
 
Use the following search in your .NET help (minus the double quotes):
"walkthrough creating master detail windows"

It should find a how-to titled "Walkthrough: Creating a Master-Detail Windows Form" which shows how to bind a ListBox and as you click entries, it automatically fills a filtered DataGrid.

-nerseus
 
No problem :)

If you want to learn how to bind a grid to a DataView (a filtered, sorted view of a DataTable) let me know. Ah heck, here it is...

Assume you have two tables, Table1 and Table2, which both contain a key column named State. You want to show a list of cities from Table2 that match the state currently chosen from Table1 (which is bound to a listbox).
C#:
string state = listBox1.Text;

DataView dv = new DataView(ds.Tables["Table2"], "state = '" + state.Replace("'", "''") + "'", "Cityname", DataViewRowState.CurrentRows);

dataGrid1.DataSource = dv;

-Nerseus
 
Back
Top