Jump to content
Xtreme .Net Talk

Recommended Posts

Posted
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.
Posted

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?

  • *Experts*
Posted

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

"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
Posted

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

  • *Experts*
Posted

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

"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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...