1.GridView控件绑定并添加主键
public void GVInfoBand()
{
gvInfoShow.DataSource = op.SelectInfo();
gvInfoShow.DataKeyNames = new string[] { "id" };//为GridView主键绑定
gvInfoShow.DataBind();
}
2.GridView控件行绑定数据事件
protected void gvInfoShow_RowDataBound(object sender, GridViewRowEventArgs e)
{
//当前行为数据绑定行
if (e.Row.RowType == DataControlRowType.DataRow)
{
//选中行高亮显示 行高亮
e.Row.Attributes.Add("onMouseOver", "color=this.style.backgroundColor;this.style.backgroundColor='#f0f0f0';");
e.Row.Attributes.Add("onMouseOut","this.style.backgroundColor=color;");
//审核状态显示 状态替换
if (e.Row.Cells[5].Text == "False")
{
e.Row.Cells[5].Text = StringFormat.HighLight("未审核", true);
}
else
{
e.Row.Cells[5].Text = StringFormat.HighLight("已审核", false);
}
//信息内容多余字 使用...显示 多余字符...替代
e.Row.Cells[2].Text = StringFormat.Out(e.Row.Cells[2].Text, 18);
//信息详情列绑定 获取选中行的主键 详情查看
e.Row.Cells[6].Text = "<a href='LookInfo.aspx?info_id="+this.gvInfoShow.DataKeys[e.Row.RowIndex].Value.ToString()+"'>查看详情</a>";
}
}