ses41 Posted March 3, 2003 Posted March 3, 2003 I am using an access database that is bound to a textbox through the text property. I would like to store the forecolor of the text in this access database, since different records will need to have different forecolors. I have tried creating a field to store the color name and then bind it to the forecolor of the textbox, however, it appears that I do not understand this binding. Can anyone give me some insite on how to use advanced bindings like forecolor. Quote
Leaders quwiltw Posted March 3, 2003 Leaders Posted March 3, 2003 Take a look at this tutorial on databinding it should give you a good start. If by chance you're already passed that point, pls give more insight into what exactly isn't working. http://samples.gotdotnet.com/quickstart/winforms/doc/WinFormsData.aspx Quote --tim
ses41 Posted March 3, 2003 Author Posted March 3, 2003 Thanks for the reply, however, I am still confused. I will try to explain further. I have a two field access database. One field lets say is called LastName, the second field is called LastNameColor. Both fields are strings. I also have a textbox in Visual Basic called txtLastName. I have bound LastName to the text property and run the program and everything works fine. I then bind the LastNameColor field to the forecolor property, run the program and I get the following error message. An unhandled exception of type 'System.Exception' occurred in system.windows.forms.dll Additional information: DataBinding could not find a row in the list that is suitable for all bindings Obviously, I am missing something simple. Any idea? Quote
*Experts* Nerseus Posted March 3, 2003 *Experts* Posted March 3, 2003 It's not so simple... To bind a property to a DataSet's column, the types must be equal. There is no equivalent database type for .NET's "Color". I tried a couple of things to work around this, but the closest thing I found was the following. It adds a new column to a datatable and manually converts the data in the dataset from the database's native type to a Color object. My database table has a column LastNameColor defined as varchar(8) (8 characters). It has values like "FFFF0000", "FF00FF00", and "FF0000FF". // Add a new column named LastNameColor2 to the table. // It's datatype is Color ds.Tables[0].Columns.Add("LastNameColor2", typeof(Color)); // Loop through all rows and convert the column LastNameColor // from the "FF00FF00" value to an int // Then convert that int to a Color to store in the LastNameColor2 field foreach(DataRow row in ds.Tables[0].Rows) row["LastNameColor2"] = Color.FromArgb(Convert.ToInt32(row["LastNameColor"].ToString(), 16)); // Finally, bind the ForeColor property to the new LastNameColor2 field txtLast.DataBindings.Add("ForeColor", ds.Tables[0], "LastNameColor2"); -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
ses41 Posted March 3, 2003 Author Posted March 3, 2003 Nerseus, I appreciate the information. As I have been working on this all day, or should I say researching this all day, I had also come to the conclusion it was not going to be easy. Your effort is greatly aprreciated and is a great help to me. Thanks Quote
*Experts* Nerseus Posted March 3, 2003 *Experts* Posted March 3, 2003 For the record, I tried using an expression column to automatically convert the string (FF00FF00) to a Color but it didn't work. I also tried using the Parse/Format events but you can't define them until AFTER you've added a DataBinding to a control - which won't work because of the original error. I'd be curious to know if you can find a more automated approach to the conversion between your database field and the Color object for binding. I haven't had the need yet, but it might just be a matter of time. -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.