turrican Posted February 2, 2003 Posted February 2, 2003 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? Quote
wyrd Posted February 2, 2003 Posted February 2, 2003 Your can bind your DataGrid at run-time like this... dataGrid1.DataSource = theSource; Quote Gamer extraordinaire. Programmer wannabe.
turrican Posted February 2, 2003 Author Posted February 2, 2003 so would that be datagrid1.datasource = datasetname or the path to the file? Quote
wyrd Posted February 2, 2003 Posted February 2, 2003 You'd more then likely want to bind the DataGrid to one of the tables inside your DataSet. Quote Gamer extraordinaire. Programmer wannabe.
turrican Posted February 2, 2003 Author Posted February 2, 2003 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. Quote
wyrd Posted February 2, 2003 Posted February 2, 2003 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 Quote Gamer extraordinaire. Programmer wannabe.
turrican Posted February 2, 2003 Author Posted February 2, 2003 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? Quote
*Experts* Nerseus Posted February 3, 2003 *Experts* Posted February 3, 2003 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 Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
turrican Posted February 3, 2003 Author Posted February 3, 2003 Thanks Nerseus!! I see your in Az, me too. Mesa. Thanks again. Quote
*Experts* Nerseus Posted February 3, 2003 *Experts* Posted February 3, 2003 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). string state = listBox1.Text; DataView dv = new DataView(ds.Tables["Table2"], "state = '" + state.Replace("'", "''") + "'", "Cityname", DataViewRowState.CurrentRows); dataGrid1.DataSource = dv; -Nerseus Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.