• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 721
  • Last Modified:

Linq Gridview - changing the style of the gridview depending on its database values

Hi experts,

I started using Linq in my projects to fill my gridview.
Now I have the following problem:
I have a table field "active" which tells me if an record in that table is active or not.
If it's not active, I would like to display that record in the gridview in a different style (ie. different fore-color for that row)

Can anyone help me in accomplishing this
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
            AllowSorting="True" AutoGenerateColumns="False" CellPadding="4" DataKeyNames="ID"
            DataSourceID="SourceDB" ForeColor="#333333" GridLines="None" 
            OnRowCommand="gridView1_Command" PageSize="20" OnRowUpdated="gridView1_RowUpdated">
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <Columns>
                <asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True" 
                    SortExpression="ID" InsertVisible="False" />
                <asp:BoundField DataField="CounterNationals" HeaderText="#Nationals" 
                    SortExpression="CounterNationals" />
                <asp:BoundField DataField="SalespersonCode" HeaderText="SPCode" SortExpression="SalespersonCode" />
                <asp:BoundField DataField="Name" HeaderText="Name" 
                    SortExpression="Name" />
                <asp:BoundField DataField="FirstName" HeaderText="FirstName" 
                    SortExpression="FirstName" />
                <asp:BoundField DataField="Institution" HeaderText="Institution" 
                    SortExpression="Institution" />
                <asp:BoundField DataField="City" HeaderText="City" 
                    SortExpression="City" />
                <asp:BoundField DataField="Country" HeaderText="Country" 
                    SortExpression="Country" />           
                    <asp:TemplateField HeaderText="CongressTicket" SortExpression="CongressTicket">
                        <ItemTemplate>
                            <%#Eval("CongressTicket")%>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:DropDownList ID="ddlCongressTicket" runat="server" AutoPostBack="True"  DataSourceID="TicketDataSource"
                                 SelectedValue='<%# Bind("CongressTicket") %>'>
                            </asp:DropDownList>
                            <asp:LinqDataSource ID="TicketDataSource" runat="server" ContextTypeName="TicketValues" TableName="AvailableTicketValues" />
                        </EditItemTemplate>
                    </asp:TemplateField>
                <asp:CheckBoxField DataField="EveningEvent" HeaderText="EveningEvent" 
                    SortExpression="EveningEvent" />
                <asp:CheckBoxField DataField="AccompanyPerson" HeaderText="Add. person" 
                    SortExpression="AccompanyPerson" />
                <asp:BoundField DataField="TotalAmount" HeaderText="Open Amount" SortExpression="TotalAmount" />
                <asp:TemplateField HeaderText="Distributor" SortExpression="Distributor.DistriName">
                    <ItemTemplate>
                        <%#Eval("Distributor.DistriName") %>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:DropDownList ID="ddlDistris" runat="server" AutoPostBack="True" DataSourceID="DistributorDataSource"
                            DataTextField="DistriName" DataValueField="ID" SelectedValue='<%# Bind("DistriID") %>'>
                        </asp:DropDownList>
                        <asp:LinqDataSource ID="DistributorDataSource" runat="server" ContextTypeName="EventManager.Event"
                            Select="new (ID, DistriName)"  TableName="Distributor">
                        </asp:LinqDataSource>
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:CommandField ShowEditButton="True"  ButtonType="Image" 
                    CancelImageUrl="~/Controls/Images/cancel.gif" 
                    EditImageUrl="~/Controls/Images/pencil.gif"
                    UpdateImageUrl="~/Controls/Images/accept.gif"/>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:ImageButton ID="delete" Runat="server" ImageUrl="~/Controls/Images/cross.gif"
                            OnClientClick="return confirm('Are you sure you want to delete this record?');"
                      CommandName="Delete" CommandArgument="<%# Container.DataItemIndex %>">
                        </asp:ImageButton>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:ButtonField ButtonType="Image" ImageUrl="~/Controls/Images/details2.gif" CommandName="CallDetails"/>
            </Columns>
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <EditRowStyle BackColor="#999999" />
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        </asp:GridView>

Open in new window

0
arthrex
Asked:
arthrex
1 Solution
 
novynovCommented:
This should be fairly straightforward. Here are the steps I recommend and code snippet below:

- Intercept the GridView OnRowDataBound event handler. This event handler receives an instance of a GridViewRowEventArgs.
- The GridViewRowEventArgs has a Row property, and the Row property has a DataItem property that contains an instance of your bound data for rows that are data rows. Notice the RowType check in the code snippet.
- Inspect the data item, if it meets the criteria, change the style on the row or the individual elements.

My code snippet assumes a schema with a person table, and a linqdatasource query against this table. The gridview is databound to this datasource. If the firstname in the row contains "Dave" then the row is made red. The commented code is for cases where I just want to color the cell value. To do this, I first converted the <asp:BoundField> to an <asp:TemplateField> and then did a FindControl() against the row, looking for the label.

For this to work, the following needs to be added to the GridView in the aspx:

OnRowDataBound="Person_DataBound"


Let me know if this helps.


protected void Person_DataBound(object sender, GridViewRowEventArgs e)
{
   GridViewRow row = e.Row;
 
   if (row.RowType == DataControlRowType.DataRow)
   {
      person p = row.DataItem as person;
      if (p.firstname.Contains("Dave"))
      {
         row.ForeColor = Color.Red;
					
	//Label labelFirstName = row.FindControl("labelFirstName") as Label;
	//labelFirstName.ForeColor = Color.Red;
 
 
      }
 
   }
}
 
		

Open in new window

0
 
arthrexAuthor Commented:
Perfect answer!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now