advanced data binding of a textbox

ses41

Member
Joined
Apr 17, 2002
Messages
13
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.
 
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?
 
Its not so simple...

To bind a property to a DataSets column, the types must be equal. There is no equivalent database type for .NETs "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 databases 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".

C#:
// Add a new column named LastNameColor2 to the table.
// Its 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
 
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
 
For the record, I tried using an expression column to automatically convert the string (FF00FF00) to a Color but it didnt work. I also tried using the Parse/Format events but you cant define them until AFTER youve added a DataBinding to a control - which wont work because of the original error.

Id 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 havent had the need yet, but it might just be a matter of time.

-Nerseus
 
Back
Top