Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 当找到与相邻行比较的相同文本时,如何突出显示行-列背景?_C#_Asp.net_Gridview - Fatal编程技术网

C# 当找到与相邻行比较的相同文本时,如何突出显示行-列背景?

C# 当找到与相邻行比较的相同文本时,如何突出显示行-列背景?,c#,asp.net,gridview,C#,Asp.net,Gridview,如果与前一行或下一行第3列文本相比发现相同文本,我必须突出显示该行第3列的背景。 例如,如果第1行第3列和第2行第3列文本相同,则其背景的cssclass都将为“ipsame1”。我能够做到这一点,由张贴的源代码如下。但是,在对gridview排序后,它会失败 protected void gv_useronline_RowDataBound(object sender, GridViewRowEventArgs e) { GridViewRow row = e.Row; if

如果与前一行或下一行第3列文本相比发现相同文本,我必须突出显示该行第3列的背景。 例如,如果第1行第3列和第2行第3列文本相同,则其背景的cssclass都将为“ipsame1”。我能够做到这一点,由张贴的源代码如下。但是,在对gridview排序后,它会失败

protected void gv_useronline_RowDataBound(object sender, GridViewRowEventArgs e)
{
    GridViewRow row = e.Row;

    if (row.RowType == DataControlRowType.DataRow)
    {
        if (compareToPrevious(row.RowIndex, row.Cells[3].Text))
        {
                row.Cells[3].CssClass = "sameIP1";
        }
    }
}

private bool compareToPrevious(int currentRowIndex, string currentRowCellsText)
{
    DataTable dt = Session["useronlineTable"] as DataTable;
    if (currentRowIndex == 0)
    {
        if (currentRowCellsText == dt.Rows[currentRowIndex + 1][3].ToString())
            return true;
    }
    else if (currentRowIndex != dt.Rows.Count - 1)
    {
        if ((currentRowCellsText == dt.Rows[currentRowIndex - 1][3].ToString())|| 
            (currentRowCellsText == dt.Rows[currentRowIndex + 1][3].ToString()))
            return true;
    }
    else
    {
        if (currentRowCellsText == dt.Rows[currentRowIndex - 1][3].ToString())
            return true;
    }

    return false;
}

protected void gv_useronline_Sorting(object sender, GridViewSortEventArgs e)
{

    //Retrieve the table from the session object.
    DataTable dt = Session["useronlineTable"] as DataTable;

    if (dt != null)
    {

        //Sort the data.
        dt.DefaultView.Sort = e.SortExpression + " " + GetSortDirection(e.SortExpression);
        Session["useronlineTable"] = dt;
        gv_useronline.DataSource = dt;


        gv_useronline.DataBind();
    }

}

我认为问题在于
gv\u useronline\u排序方法。您可以尝试修改
gv\u useronline\u排序方法,如下所示:

protected void gv_useronline_Sorting(object sender, GridViewSortEventArgs e)
{
    //Retrieve the table from the session object.
    DataTable dt = Session["useronlineTable"] as DataTable;

    if (dt != null)
    {
        //Sort the data
        DataView dv = dt.DefaultView;
        dv.Sort = e.SortExpression + " ASC"; // ASC or DESC 
        DataTable dtSorted = dv.ToTable();

        // Put the sorted table in session
        Session["useronlineTable"] = dtSorted;

        // Bind the GridView to the new 
        gv_useronline.DataSource = dt;
        gv_useronline.DataBind();
    }
}

我认为它的工作原理是,既然您已将排序表传递到会话中,那么在
compareToPrevious
方法中,它将与排序表进行比较。

谢谢Jack!它正在工作!在排序方法中,为什么必须首先将其转换为dataview?我想我的逻辑和你的逻辑是一样的。但实际上是不同的