I am wanting to change my WinGrid cell button appearance depending on a value I have flagged (a boolean flag coming from the stored procedure).
I have been successful in changing the appearance, a little, at least enough to know that it can be changed some. I just want to make the button appearance change more drastic.
In the Win Grid Initialize Row procedure I have the following code...
If e.Row.Cells("Notes_Flag").Value = True Then e.Row.Cells("Notes").ButtonAppearance.ForeColor = Color.Blacke.Row.Cells("Notes").ButtonAppearance.ThemedElementAlpha = Alpha.Transparent e.Row.Cells("Notes").ButtonAppearance.Image = "~/Images/Notes.jpg"Else e.Row.Cells("Notes").ButtonAppearance.ForeColor = Color.Beige End If
e.Row.Cells("Notes").ButtonAppearance.ForeColor = Color.Blacke.Row.Cells("Notes").ButtonAppearance.ThemedElementAlpha = Alpha.Transparent e.Row.Cells("Notes").ButtonAppearance.Image = "~/Images/Notes.jpg"
e.Row.Cells("Notes").ButtonAppearance.ForeColor = Color.Black
e.Row.Cells("Notes").ButtonAppearance.Image = "~/Images/Notes.jpg"
e.Row.Cells("Notes").ButtonAppearance.ForeColor = Color.Beige
End If
So now my button will change to the display text of black, and the button looks like a plain jane Windows button, if the flag = True. If the flag = False, the button will change the text to the color of Beige (not visible) and the button looks like a XP button. So I can tell quickly which records has the Notes_Flag checked and which ones do not.
Now I am wanting to spruce this up.
I would like to do the following...
The button's value is based on an ID, and this ID is needed to be passed to a stored procedure. But I would like to either change the text of the button to read "Notes" (instead of displaying the ID value) OR insert an image (which ever is easier). My code above to insert the image is ignored and I am not certain why.
Is this possible, or must the Button display the value of the cell it is connected to (unbound column)?
Thank you,
T.J.
Hi T.J.,
In order to answer your question, I need to know how you are creating the button in the cell. Are you just setting the Style on the column? If so, what Style are you using? Or are you using an editor to get the button?
On a side note, this code is going to be very inefficient. By doing this, you will create a new Appearance object for every cell. So you are creating a large number of Appearance objects with the same properties. It would be better to use the InitializeLayout event of the grid to add an Appearance to the Appearances collection of the DisplayLayout and then assign that Appearance to the ButtonAppearance of the cell - thus re-using the same appearance.
TBernard said:e.Row.Cells("Notes").ButtonAppearance.ForeColor = Color.Blacke.Row.Cells("Notes").ButtonAppearance.ThemedElementAlpha = Alpha.Transparent e.Row.Cells("Notes").ButtonAppearance.Image = "~/Images/Notes.jpg"
Mike,
Thank you for your help, I greatly appreciate it.
I created the button by going to the WinGrid Designer to add a new Unbound Column. For this column, I set the Key equal to the field in my stored procedure when contained the ID I need to pass to a pop-up form this button will open (Field name "Notes"). I set the Style of the column = "Button". I set the Button Display Style = "Always" and I set the Cell Button Appearance Key property = "Notes" as well.
So this column is simply a button style column (set on the Win Grid Designer) and it contains the data for the [Notes] field in my stored procedure.
Is there a better way to set-up my button column, to allow for the text to read "Notes" instead of the data value in the [Notes] field?
It would be great if the button would display the text "Notes" where my [Notes_Flag] field was True, and the button to be empty when my [Notes_Flag] field is false. The [Notes_Flag] field is a Boolean field in my stored procedure which is also set to an Unbound (and hidden) column on my Win Grid.
Thank you very much for your help.
Thank you very much. The one line of code you offered opened up my mind and allowed me to find the solution I was looking for.
I set the button column to a field in the stored procedure which simply said "NOTES" when notes existed for the record and "" when there were no notes. I then used your line of code to correctly open up the pop-up form with the correct Notes data.
Hi T.J.
I'm still a bit confused. The column is either bound or it's not. There's no such thing as an unbund colmun that is linked to another column. Unless by linked you mean you have some code that is copying values. If you have an unbound column in your grid and the column's key exists in the data source, then my guess is that the unbound column gets destroyed and replaced with a new bound column. I could be wrong.
In any case, when the user clicks the button, the CellButtonClick event on the grid fires. This event probably passes you the cell that was clicked. If not, you can just use grid.ActiveCell. Once you have the cell, you can read any value in the row using the Row property on the cell and accessing the Cells collection.
this.ultraGrid1.ActiveCell.Row.Cells["Primary Key"].Value
Sorry about the confusion, I re-read what I typed and I confused myself, LOL. I added the field to the grid as an "Unbound column", but the entire Grid is connect to a DataSet table, and there is a field in the dataset table which links to the button field, by the "Key" property. So the button column is actually connected to the primary key of the dataset table.
Sounds like I have more work ahead of me. This primary key is already stored in another column that is hidden on the grid, once the project is run. So it sounds like I can change the Button Column so that it is not bound to the Primary Key, and then I can change the text according to the flag field. How would I reference the primary key, on the button click event, if the button column no longer is bound to the primary key?
Thank you for all your help.
I'm a little confused by your description here. You say that this is an unbound column. If that's the case, then where is it getting populated with the value of the Notes column in your data? If the column is unbound, then you must be populating this field in your code somewhere. Typically, you would do this in the InitializeRow event of the grid. If that's the case, then you already know the answer to your question. Just populate the field with whatever text you want instead of the actual value of the field.
If you column style is set to Button, then the button will display the value of the cell as text. So if this is an unbound column, you can set it to whatever you want. If it's a bound column, then you would have to do a bit more work, like maybe hiding the bound column and adding an unbound column or using a CreationFilter to change the display.