【问题标题】:Highlight most recently inserted row GridView asp.net突出显示最近插入的行 GridView asp.net
【发布时间】:2015-10-18 02:26:37
【问题描述】:

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

我通过将网格与数据表绑定来添加行:

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;
    }

【问题讨论】:

  • 您应该发布相关代码和标记。如果你不这样做,很难说出你是如何插入你的行的,这意味着无论谁回答都必须花更少的时间来处理它(所以你更有可能得到答案)和答案将更适合您的情况。
  • 您好,感谢您的评论,我会尽快编辑问题。

标签: c# asp.net gridview row


【解决方案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++;
            }
        }
    }

【讨论】:

    【解决方案2】:

    我使用rowdatabound 事件查找已编辑的行,然后为该行分配一个引导 css 类,如下所示:

    e.Row.CssClass = "danger";
    

    【讨论】:

      猜你喜欢
      • 2016-01-06
      • 1970-01-01
      • 1970-01-01
      • 2011-06-16
      • 1970-01-01
      • 2020-11-26
      • 2011-01-28
      • 2010-10-03
      • 2015-10-10
      相关资源
      最近更新 更多