突出显示最近插入的行 GridView asp.net
Highlight most recently inserted row GridView asp.net
我的 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;
}
我使用 rowdatabound
事件查找已编辑的行,然后将 bootstrap css class 分配给该行,如下所示:
e.Row.CssClass = "danger";
存储主键/插入行时在视图状态中唯一标识行的唯一值:
让我们假设第一列具有唯一值。在 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++;
}
}
}
我的 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;
}
我使用 rowdatabound
事件查找已编辑的行,然后将 bootstrap css class 分配给该行,如下所示:
e.Row.CssClass = "danger";
存储主键/插入行时在视图状态中唯一标识行的唯一值:
让我们假设第一列具有唯一值。在 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++;
}
}
}