我想将CSS(想要应用颜色)应用到网格视图的最后一行 [英] I want to apply CSS (want to apply color) to last row of grid view

查看:101
本文介绍了我想将CSS(想要应用颜色)应用到网格视图的最后一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望以彩色格式显示网格视图的最后一列。

即我想在gridview的最后一列上应用Css



我创建了使用Gridview绑定数据的存储过程

工作正常



我尝试过:



我创建了Css课程为



I Want To Display Last Column Of Grid View In Color Format.
I.e I want to apply Css on Last Column Of gridview

I Have Created Stored Procedure To Bind Data With Gridview
Its Working Fine

What I have tried:

I Have Created Css Class As

<style>
        .highlighted 
  {
     color:Red ! important;
     background-color: blue ! important;
  }
    </style>
</head>







然后在Gridview控件的PreRender事件中我有了wriiten代码为








then On PreRender Event Of Gridview Control I have wriiten Code as


protected void GridView1_PreRender1(object sender, EventArgs e)
        {

            try
            {
                GridViewRow getRow = GridView1.Rows[GridView1.Rows.Count - 1];
                getRow.Attributes.Add("class", "highlighted");
            }
            catch(Exception ex)
            {
                Response.Write(ex);
            }
}





在Gridview1_prerender中显示异常





It Showing Exception in Gridview1_prerender as

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: in

推荐答案

从我看到这个,问题出在硬编码 [...计数 - 1] 事情,为什么首先这样做,并让它暴露在几个条件下?例如,如果 Count 属性为零(列表为空?)会发生什么。而不是这样,我个人会尝试使用它,

From where I see this, the problem is with the hardcoded [...Count - 1] thing, why do that in the first place, and leave it exposed to several conditions? For example, what will happen if the Count property is zero (the list is empty?). Instead of this, I would personally try using this,
GridViewRow row = null;
if(GridView1.Rows.Count == 0) {
   // Handle the empty grid
} else if (GridView1.Rows.Count == 1) {
   row = GridView1.Rows[0]; // Capture this row, or ignore the single row.
} else if (GridView1.Rows.Count > 1) {
   row = GridView1.Rows[GridView1.Rows.Count - 1];
}

if(row != null) {
   // Check if there is a row
   row.Attributes.Add("class", "highlighted");
}



这应该可以正确地确保您的问题成为目标,并且有一个解决方案,代码可以采取防止代码崩溃的方法。


This should properly ensure that your problems are targeted and there is a solution, that the code can take to prevent code crashes.


这篇关于我想将CSS(想要应用颜色)应用到网格视图的最后一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆