1

我想在网格视图上突出显示最后插入的记录。在我的网格视图中,我有一个主键列。新插入的行按照此主键的升序排列。现在我想突出显示最后插入的行。从我这样写的搜索中,但它显示网格视图的最后一行突出显示。,而不是新插入的行。请帮忙。我的代码在这里,

   int lastRowIndex = 0;

     if(!IspostBack)
       {
  string select_string="SELECT student_name,student_id,student_nric,student_group FROM student_details WHERE student_group='"+groups[0].ToString().Trim()+"' ";
        for(int i=1;i<groups.Count;i++)
        {
            select_string+= " or student_group='"+groups[i].ToString().Trim()+"'";    
        }

        if(Session["STUDENT_ID"]!=null)
        {

        for(int i=0;i<student_id_list.Count;i++)
        {
            select_string+= " or student_id='"+student_id_list[i].ToString().Trim()+"'";    

        }
        }
     SqlDataAdapter adapter = new SqlDataAdapter(select_string, con);
        adapter.Fill(ds);
        GridView2.DataSource = ds;
        GridView2.DataBind();
        con.Close();


        foreach (GridViewRow grv in GridView2.Rows)
        {
            if (grv.RowType == DataControlRowType.DataRow)
            {
                if (grv.RowIndex > lastRowIndex)
                {
                    lastRowIndex = grv.RowIndex;
                }
            }
        }
        lastRowIndex = GridView2.Rows.Count - 1;

        GridView2.Rows[lastRowIndex].BackColor = System.Drawing.Color.LightGoldenrodYellow;

    }
}

}

4

2 回答 2

1

我认为你的问题在这里:

lastRowIndex = GridView2.Rows.Count - 1;

您总是将 lastRowIndex 设置为相同的...

您可以使用 RowsAdded 事件:

private void grv_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
    // the new row = e.RowIndex
}
于 2013-08-06T06:29:09.710 回答
0

有几个解决方案你可以做第一个是创建一个在不同视图状态中访问的变量,如下所示:

private bool AddedSuccesfuly
    {
        get
        {
            return (bool)ViewState["flag"];
        }
        set
        {
            ViewState["flag"] = value;
        }
    }

接下来在页面加载中,您将其值添加为 false,因为您尚未插入任何行,然后在 insertRow 的按钮单击事件上,您将其值设置为 true,并且在 datarowbound 方法或绑定方法中,它在 if 语句中并不重要像这样:

if (AddedSuccesfuly == true)
        {
            gridviewname.Rows[gridviewname.Rows.Count1].BackColorSystem.Drawing.Color.Beige;
        }

我还建议您将主键设置为身份或自动增量等效项,并且不要在网格视图中显示它,因为学生不需要查看其 ID。

于 2018-11-21T13:46:14.233 回答