突出显示最近插入的行GridView asp.net

时间:2015-10-18 02:26:38

标签: c# asp.net gridview row

我的ASP.NET项目中有一个分页GridView,我在其中将人力资源插入到数据库中。每次将所有人力资源插入数据库时​​,我的GridView都会加载。 现在,每次添加新行(人力资源)或修改现有行时,我都希望它在网格中突出显示,以向用户说明操作已执行。我还没有找到一个好的方法,而gridview被分页的事实使它变得更加复杂。我将不胜感激一些帮助:)

我通过使用dataTable绑定de grid来添加行:

protected void llenarGrid()        //se encarga de llenar el grid cada carga de pantalla
    {
        DataTable recursosHumanos = crearTablaRH();
        DataTable dt = controladoraRecursosHumanos.consultarRecursoHumano(1, 0); // en consultas tipo 1, no se necesita la cédula

        Object[] datos = new Object[4];


        if (dt.Rows.Count > 0)
        {
            foreach (DataRow dr in dt.Rows)
            {
                datos[0] = dr[0];
                datos[1] = dr[1];
                datos[2] = dr[2];
                int id = Convert.ToInt32(dr[3]);
                String nomp = controladoraRecursosHumanos.solicitarNombreProyecto(id);
                datos[3] = nomp;
                recursosHumanos.Rows.Add(datos);
            }
        }
        else
        {
            datos[0] = "-";
            datos[1] = "-";
            datos[2] = "-";
            datos[3] = "-";
            recursosHumanos.Rows.Add(datos);
        }
        RH.DataSource = recursosHumanos;
        RH.DataBind();

    }



   protected DataTable crearTablaRH()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("Cedula", typeof(int));
        dt.Columns.Add("Nombre Completo", typeof(String));
        dt.Columns.Add("Rol", typeof(String));
        dt.Columns.Add("Nombre Proyecto");
        //dt.
        return dt;
    }

2 个答案:

答案 0 :(得分:1)

我使用rowdatabound事件来查找已编辑的行,然后将bootstrap css类分配给该行,如下所示:

e.Row.CssClass = "danger";

答案 1 :(得分:1)

存储主键/唯一值,用于在插入行时唯一标识“查看状态”中的行:

假设第一列具有唯一值。在llenarGrid()方法的末尾添加以下行。

 ViewState["LastRowUniqueValue"] = datos[0];

处理Page_PreRender事件,突出显示插入的行:

protected void Page_PreRender(object sender, EventArgs e)
    {
        string lastInsertedRowValue = string.Empty;

        // only highlight the row if last inserted values are NOT a Hyphen -

        if (ViewState["LastRowUniqueValue"] != "-")
        {
            // Assuming the Unique value is String, else cast accordingly
            string lastInsertedRowValue = (string)ViewState["LastRowUniqueValue"];

            int rowCnt = 0;
            foreach (GridViewRow row in GridView1.Rows)
            {
               string CellText = row.Cells[0].Text;
                if (CellText.Equals(lastInsertedRowValue))
                {
                    row.Attributes.Add(“bgcolor”, “Yellow”);
                    break;
                }
                rowCnt++;
            }
        }
    }