Reputation: 670
How do I access a control in a grid view so that I can change it's forecolor? In this code below, FindControl() returns null.
protected void mileageRowBound(object sender, GridViewRowEventArgs e)
{
(e.Row.Cells[1].FindControl("ddlStateCode") as DropDownList).ForeColor = System.Drawing.Color.LightBlue;
}
I have also tried e.Row.FindControl("ddlStateCode") and a few other variations. I'm stumped.
Someone asked for the markup:
<asp:GridView runat="server" OnPreRender="grvStateWiseMileage_OnPreRender" OnRowCommand="grvStateWiseMileage_OnRowCommand"
CssClass="GridViewStyle" BorderWidth="1" Width="100%" ID="grvStateWiseMileage"
AutoGenerateColumns="false" RowStyle-Height="25px" ShowHeader="false" OnRowDataBound="mileageRowBound">
<Columns>
<asp:TemplateField>
<ItemStyle CssClass="GridViewRowStyle" Width="5%" />
<ItemTemplate>
<asp:Label ID="lblLine" runat="server" onKeyUp="javascript:ValidateDecimal(this)"
Text='<%# Eval("Line#") %>'></asp:Label>
<asp:ImageButton runat="server" ID="imgbtnMileageDelete" ImageUrl="Images/delete.png" />
<asp:HiddenField runat="server" ID="hdnFuelMileageCode" Value='<% #Eval("FuelMileageCode") %>' />
<asp:HiddenField runat="server" ID="hdnMileageCode" Value='<% # Eval("MileageCode") %>' />
<asp:HiddenField runat="server" ID="hdnMileagePosted" Value='<% # Eval("MileagePosted") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemStyle CssClass="GridViewRowStyle" Width="10%" />
<ItemTemplate>
<asp:DropDownList runat="server" OnLoad="ddlStateCode_OnLoad" ID="ddlStateCode" Style="border: none;
border-width: 0px; width: 100px">
</asp:DropDownList>
<asp:HiddenField runat="server" ID="hdnStateCode" Value='<% # Eval("State")%>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemStyle CssClass="GridViewRowStyle" Width="10%" />
<ItemTemplate>
<BDP:BasicDatePicker ID="bdpMileageDate" runat="server">
</BDP:BasicDatePicker>
<asp:HiddenField runat="server" ID="hdnMileageDate" Value='<% # Eval("Date")%>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemStyle CssClass="GridViewRowStyle" Width="10%" />
<ItemTemplate>
<asp:TextBox ID="txtMiles" Style="border: none; border-width: 0px; text-align: right"
Width="90%" runat="server" MaxLength="12" onKeyUp="javascript:ValidateDecimal(this)"
Text='<%# Eval("Miles") %>' onblur="postBackHiddenField('hdnStateWiseMileage')"
onkeydown="return postBackHiddenFieldForEnterMiles(event)"></asp:TextBox>
<cc1:FilteredTextBoxExtender runat="server" FilterMode="ValidChars" FilterType="Custom, Numbers"
ValidChars="." TargetControlID="txtMiles">
</cc1:FilteredTextBoxExtender>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemStyle CssClass="GridViewRowStyle" Width="10%" />
<ItemTemplate>
<asp:DropDownList runat="server" ID="ddlLoadStatus" Style="border: none; border-width: 0px;
width: 100px">
<asp:ListItem Selected="True" Text="Loaded" Value="1"></asp:ListItem>
<asp:ListItem Text="Empty" Value="2"></asp:ListItem>
<asp:ListItem Text="Toll" Value="3"></asp:ListItem>
</asp:DropDownList>
<asp:HiddenField runat="server" ID="hdnMileageType" Value='<% # Eval("LoadStatus")%>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BorderStyle="None" BackColor="White" />
</asp:GridView>
Upvotes: 0
Views: 664
Reputation: 108957
Try to wrap your line of code in
if(e.Row.RowType == DataControlRowType.DataRow)
{
(e.Row.FindControl("ddlStateCode") as DropDownList).ForeColor = System.Drawing.Color.LightBlue;
}
So it will skip header (and footer and a few others).
Upvotes: 1
Reputation: 505
I guess the better answer to this question is use CSS to change the color of the control text.
Try add the following lines to the ASPX page.
<style type="text/css">
#ddlStateCode
{
color: #FF0000; /* Change to the hexacode you want */
}
</style>
Hope it helps.
Upvotes: 0