Okay I researched this as much as possible before I posted so I hope this is not a repost but here goes...
I made a datatable, lets say it has a coulmn called "cartype" , then I added a column called "color"
well, I bound a gridview what I want to do is use a label, and have its FORECOLOR be the VALUE in the column "color".
I tried this:
<asp:BoundField DataField="cartype" HeaderText="Cars" ItemStyle-Width="130" ItemStyle-ForeColor='<% Eval("Color") %>' />
but I got an error about
"Cannot create an object of type System.drawing.color from its string representation '<%Eval("color")%>' for the 'ForeColor' Property.
I also tried adding a template field and got the same result.
I was trying not to use the rowdatabound event and use .Cells[3] , because then when I add columns, it's going to change the cell number, and switch up everything! I was hoping I could make it cleaner by binding the color with the data.
Okay I ended up doing this. sad :(
At the VERY END of my grid I added an ASP:Hidden template field
<asp:TemplateField>
<ItemTemplate>
<asp:HiddenField ID="hColor" runat="server" Value='<%# Eval("Color") %>'/>
</ItemTemplate>
</asp:TemplateField>
Then in the RowDataBoundEvent I did this
Dim hid As HiddenField
hid = e.Row.FindControl("hColor")
If (hid.Value <> Nothing) Then
e.Row.Cells(1).ForeColor = System.Drawing.ColorTranslator.FromHtml("#" + hid.Value)
End If
PS. I heart C#. Dont like VB.NET. lol :)
Have you tried:
ForeColor='<%# System.Drawing.Color.FromName(Eval("Color")) %>'
?
The reason for the error is that the runtime expects the value of ForeColor to be a color, NOT a string. The runtime cannot create "Red" from the string "Red".
You can create a function at code behind using following example to work around your issue .. function will accept string color value and will return System color. Also before populating gridview you can store the color column in the arraylist and use that arraylist to return system color. Not the best way but cant think of anything else.
ForeColor='<%# Convert.ToString(Eval("Color")) == "Blue" ? System.Drawing.Blue>
Related
i have one textbox and it is not readonly..
but by default it has to get filled by 0..
then depending on user requirement, textbox value may be altered...
Does anyone know how to do this?
A question like this was already solved. I personally would do it in the markup: Text="0". Linked the other question below.
Default value of textbox
"Placeholder" property will do the job:
Try the following: This works well with all latest browsers
<asp:TextBox runat="server" ID="TextBox1" placeholder="0">
</asp:TextBox>
I have a string value that is returned from the database. the value will always either be "Red" or "Green". I am trying to change the forecolor of a label in my repeater based on this string value. If the value is "Red" the forecolor of the label should be red and visa versa.
I try by using this code in my asp page:
ForeColor='<%# Eval("Color")%>'
however I get the flowing error:
Specified cast is not valid.
I have had a similar problem before by trying to set the width of an image dynamically and ended up casting the value like this:
<asp:Image
ID="Image1"
runat="server"
Width='<%# System.Web.UI.WebControls.Unit.Parse(Eval("ImageSize").ToString()) %>'
ImageUrl="~/Images/ProgressBar.jpg"/>
The problem is how do I use the same technique with a system color?
ForeColor needs a Color object not a string.
So you could use ColorTranslator.FromHtml:
ForeColor='<%# Drawing.ColorTranslator.FromHtml(Eval("Color").ToString())%>'
Try
style='color:<%# Eval("Color")%>'
instead
I have a GridView and one of the columns has a TemplateField using a LinkButton (ButtonField)
I can get the value of a specific cell in my Grid Just fine. Using:
GridViewRow row = GridView1.SelectedRow;
lblSalesmanCustomers.Text = row.Cells[2].Text;
I then display the text from that cell in a Label.
However I cannot get this to work with a ButtonField Template. It only works with a non Template column.
How can I get the value of a specific cell in a column that is using a ButtonField / TemplateField?
EDIT: This is My Button Field Code inside my GridView:
<asp:ButtonField DataTextField="Customer" HeaderText="Customer" ButtonType="Link" CommandName="Select" />
Also, this is happening in this event:
protected void gvManagerCustomers_SelectedIndexChanged(object sender, EventArgs e)
See if var Button = row.Cells["Customer"].Controls[0]; would retrieve the button you need.
You might need to cast it to the correct type.
I know there are issues with hyperlinkfield and buttonfield but there is a work around. Say you are binding buttonfield text based on a column called ButtonNames, and in that column you have all your names, such as "button bob", "button jerry" etc. In your GridView, add an invisible column as your very first column and bind its value as ButtonNames. You make it invisible by setting one of the visibility properties. Forgot what it was from top of my head. Then, when you want to get the text for the buttonfield simply get the data from that invisible column instead Same applies to hyperlinkfiends.
EDIT: here's some code.
<asp:BoundColumn ItemStyle-HorizontalAlign="Left" DataField="ButtonNames" SortExpression="ButtonNames" HeaderText="TriageId" Visible="false" ReadOnly="true"></asp:BoundColumn>
Then you retrieve it via string s = e.Item.Cells[0].Text where e is a DataGridCommandEventArgs or something to that nature.
There will be a controls collection in the cell - you may be able to access it there.
Though a simpler way would be to use something like:
Label l = row.FindControl("myControlId");
EDIT: true the exact approach above does not work - but you can use the controls, the following does work, note that what we are doing here is pretty much rife with bad practices (but then we are using a GridView for convenience sake after all).
protected void gvManagerCustomers_SelectedIndexChanged(object sender, EventArgs e)
{
var x = ((sender as GridView).SelectedRow.Cells[0].Controls[0] as LinkButton).Text;
}
In order to figure this out set up a debug environment and breakpoint in the handler method then drill down through the class hierarchy. The debugger is our friend ;)
EDIT just to mention the obvious - the column is hard coded here - you will probably have to change it.
First of all, I've got a DataTable which stores data in a number of fields. One of these fields stores a value, which can either be '1', '0' or '-1'.
What I'm trying to accomplish is to fill a GridView with all the data contained in this DataTable, except for the previously mentioned field. Instead of this field, I would like to display a column which shows a different icon depending on the value (either 0, 1 or -1).
In other words, if a row contains a value of '0' in this field within the DataTable, I would like a red circle icon to appear in the equivalent cell within the GridView.
Could anyone kindly explain how this may be accomplished? Below is some code which may help to explain the situation further.
The GridView is filled with the data contained in the DataTable.
source = new DataSource();
TableGridView.DataSource = source.FillTable();
TableGridView.DataBind();
What should be done after this?
EDIT: Solution added as a comment below
Well there are different approaches:
1) you could add a field to your DataTable for the image and fill it with the url of the image that corresponds to the value. Add an image to the GridView and set it's ImageUrl to that field.
2) Add a template field to your GridView and add the 3 images. In the Visible property of the each image you set something like <% (int)Eval("MyNumericValue") == x %> (x being 0, 1 or -1) . That way only 1 image is shown, the others hidden (not rendered even).
from the hip:
<TemplateField>
<ItemTemplate>
<asp:image runat="server" imageurl="~/images/0.png" Visible='<%# (int)Eval("MyNumericValue") == 0 %>' />
<asp:image runat="server" imageurl="~/images/1.png" Visible='<%# (int)Eval("MyNumericValue") == 1 %>' />
<asp:image runat="server" imageurl="~/images/-1.png" Visible='<%# (int)Eval("MyNumericValue") == -1 %>' />
</ItemTemplate>
</TemplateField>
Solved: I created the TemplateField in ASP.NET code, and the other fields programmatically in the .cs class. Next, I added a chunk of code which switches round the order of the columns. Hope someone finds this helpful.
How can I give an hex color code to gridview's row background color? I know that it is to be given on a rowdatabound event. But I am not sure whether a hex color code can be given or a default - System.Drawing.Color.(ColorName) would only work?
You need to assign a System.Drawing.Color type. But you can easily get the instance from hex or named values like shown below:
Color clr = ColorTranslator.FromHtml("#FFFF33");
named colors:
Color clr = ColorTranslator.FromHtml("Red");
Also, you can directly specify the background/foreground color in the markup for gridview:
<asp:GridView ID="gridView1" Runat="server"
...
<RowStyle ForeColor="red" BackColor="#FFF7E7"></RowStyle>
</asp:GridView>
You can use both named and hex values.
Look here for conversions between int, string and hex values