sparkle999 Posted January 31, 2004 Posted January 31, 2004 I've built a simple windows form to maintain a single database table (add, insert, update and delete). I am using a listbox to display the list of customer names. Previously I bound the listbox to the dataset directly, however I have now found that when I add a new record, it gets added to the bottom of the listbox, and I want to sort the customer names into alphabetical order. So, I've now added a data view which sits on top of the dataset. The listbox is now bound to this instead. The problem is, I have a bit of code which updates the other fields on the form when you select an item in the listbox. Previously I had some code like this: private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e) { sqlDataAdapter1.SelectCommand.Parameters["@BusRegID"].Value = dataSet21.t_BusinessRegister[this.listBox1.SelectedIndex].BUS_REG_ID; dataSet11.Clear(); sqlDataAdapter1.Fill(dataSet11); SetButtons(true, true, false, false, true); } which was fine and worked, however now I want to access the equivalent "currently selected item" in the data view (rather than the data set), and the following code gives me a compile error: private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e) { sqlDataAdapter1.SelectCommand.Parameters["@BusRegID"].Value = dataView1[this.listBox1.SelectedIndex].BUS_REG_ID; dataSet11.Clear(); sqlDataAdapter1.Fill(dataSet11); SetButtons(true, true, false, false, true); } The error is (1032): 'System.Data.DataRowView' does not contain a definition for 'BUS_REG_ID' I know it is something to do with the different data structures, can anyone help? Thanks a lot Quote
*Experts* Bucky Posted January 31, 2004 *Experts* Posted January 31, 2004 The DataView control is not strongly-typed, that is, it will not contain methods and properties that refer exclusively to your DataSet. You need to, instead, use the indexer of the DataRow to get the data from the "BUS_REG_ID" column: sqlDataAdapter1.SelectCommand.Parameters["@BusRegID"].Value = dataView1[this.listBox1.SelectedIndex].Row["BUS_REG_ID"]; Quote "Being grown up isn't half as fun as growing up These are the best days of our lives" -The Ataris, In This Diary
sparkle999 Posted January 31, 2004 Author Posted January 31, 2004 Thanks Bucky, that worked! Many thanks Quote
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.