How do I specify CSS classes for specific rows in a GridView?

后端 未结 2 1295
清酒与你
清酒与你 2021-01-11 11:25

I am creating a SharePoint web part in C# and part of it outputs a GridView control to the page. While I can get fairly extensive control over the way it is displayed by set

相关标签:
2条回答
  • 2021-01-11 12:11

    I do something similar with RowDataBound:

    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        // Check the XXXX column - if empty, the YYYY needs highlighting!
        if (e.Row.Cells[6].Text == " ")
        {
           e.Row.CssClass = "highlightRow"; // ...so highlight it
        }
    }
    

    One way to check that what you are doing is correct is to monitor your html output via the browser... something like Firebug really helps.

    Here's some sample CSS, where we assign the CssClass 'dataGrid' to the Grid:

    /* Used to highlight rows */
    table.dataGrid tr.highlightRow td
    {
        background-color: #FF6666;
        border-bottom: 1px solid #C0C0FF;
    }
    

    Update: Wiring all this up: I use auto-wire-up on the aspx page. Your page declaration looks something like this:

    <%@ Page Language="C#" MasterPageFile="~/XXXXXX.master" AutoEventWireup="true" CodeBehind="YYYY.aspx.cs" Inherits="ZZZ.ZZZ.AAAAAA" Title="View Blah" %>
    

    This setting on the page allows you to use the UI to connect up the events. Click the grid, select the properties, click the lightning-strike icon, and under the RowDataBound event, select your method. All this does behind the scenes is add an attribute to the DataGridView, thus:

            <asp:GridView ID="uiActionGridView" runat="server" AllowSorting="True" AutoGenerateColumns="False"
            OnRowDataBound="uiActionGridView_RowDataBound" OnDataBound="uiActionGridView_DataBound">
    
    • this shows two events being wired-up, the DataBound and RowDataBound events.

    This is what I do using VS2005 and it all seems to 'just work'. The only thing that I can think you are experiencing is that you are manually binding the event after the databind has occurred.

    0 讨论(0)
  • 2021-01-11 12:15

    Optional! you can also add bootstrap class in code behind like...

    protected void gvEmpContactsHistory_SelectedIndexChanged(object sender, EventArgs e)
        {
            string val = Convert.ToDateTime(gvEmpContactsHistory.SelectedDataKey.Value).ToString("dd-MM-yyyy hh:mm:ss", new System.Globalization.CultureInfo("en-US"));
            GetEmployeeDetail(val);
    
            foreach (GridViewRow row in gvEmpContactsHistory.Rows)
            {
                if (row.RowIndex == gvEmpContactsHistory.SelectedIndex)
                {
                    row.ToolTip = string.Empty;
                    row.Attributes.Add("class", "btn-success");
                }
                else
                {
                    row.ToolTip = "Click to select this row.";
                    row.Attributes.Add("class", "btn-outline-success");
                }
            }
        }
    
    0 讨论(0)
提交回复
热议问题