更推荐的实践:使用 HtmlRowPrepared 事件
虽然 HtmlRowCreated 可以实现你的需求,但 DevExpress 官方更推荐在 HtmlRowPrepared 事件中进行样式设置。
为什么更推荐 HtmlRowPrepared?
-
时机更晚:HtmlRowPrepared 在行创建并且数据绑定完成后触发。这意味着你可以访问该行的数据,并根据数据内容来动态地设置样式。
-
功能更强:这是进行条件格式化(例如,当某个字段值小于0时,将行背景标红)的理想位置。
protected void GridView_zypx_HtmlRowPrepared(object sender, ASPxGridViewTableRowEventArgs e)
{
int dqsxh = -1;//本事件的测试,必须先去掉updatepannel,否则不能发现错误
//string pcmc = e.GetValue("pc").ToString(); 放在这里出错 pc为visual=false的字段,必须设置字段列
try
{
string pcmc = e.GetValue("pc").ToString();
//Label_xs_sfxz
dqsxh = int.Parse(e.GetValue("sxh").ToString());//当前批次的专业顺序号 ddp_tzzy:下拉列表显示移到-》志愿1.。。志愿10.。。
string sfxz = e.GetValue("是否新增").ToString(); //((Label)e.Row.Cells[5].FindControl("Label_xs_sfxz")).Text;
//标记新增高校专业,该行浅黄色
if (!string.IsNullOrWhiteSpace(sfxz) && sfxz.ToLower() == "true")
{
// <asp:Label ID="Label_xs_ksid" runat="server" Text='<%# Eval("ID") %>' Visible="false"></asp:Label> 用于更新是否新增,不用再次查询
e.Row.BackColor = Color.SeaGreen;//在HtmlCreated中失效
// GridView_zypx.GetRow(e.VisibleIndex)
}
ASPxComboBox ddp = GridView_zypx.FindRowCellTemplateControl(e.VisibleIndex, null, "ddp_tzzy") as ASPxComboBox; // (DropDownList)e.Row.Cells[5].FindControl("ddp_tzzy");//找到下拉列表
if (ViewState["dqyh0936"] != null)
{
string hq_pczygs = ViewState["dqyh0936"].ToString();
if (hq_pczygs.Contains(" "))//多个批次
{
var cx_pczygs = from aa in hq_pczygs.Split(' ')
where aa.Contains(pcmc)
select aa;
if (cx_pczygs.Count() > 0)
{
int pczygs = Convert.ToInt32(cx_pczygs.First().Split(':')[1]);//当前行批次对应的高校专业个数
for (int i = 1; i <= pczygs; i++)
{
if (i != dqsxh)
{
if (i < 10)
{
ddp.Items.Add("志愿0" + i.ToString());
}
else
{
ddp.Items.Add("志愿" + i.ToString());
}
}
}
}
} //只有一个批次
else
{
int pczygs = Convert.ToInt32(hq_pczygs.Split(':')[1]);//当前行批次对应的高校专业个数
for (int i = 1; i <= pczygs; i++)
{
if (i != dqsxh)
{
if (i < 10)
{
ddp.Items.Add("志愿0" + i.ToString());
}
else
{
ddp.Items.Add("志愿" + i.ToString());
}
}
}
}
//this.UpdatePanel1.Update();//刷新页面
}
}
catch
{
// this.UpdatePanel1.Update();
return;
}