在绑定使用DataSet的值设置的GridView行背景颜色 [英] Set Gridview Row Background Color using value in Binding DataSet

查看:99
本文介绍了在绑定使用DataSet的值设置的GridView行背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个GridView,其中包含一列 ID

I'm having a GridView which contains a Column ID

我有一个数据表,其中包含两列

I'm having a DataTable which contains two columns

ID
DONE

我绑定 ID 列在数据表到GridView。
直到没有它的罚款。

I'm Binding the ID Column in the DataTable to the GridView. Until no Its fine.

但现在我需要设置在GridView行立足的背景颜色在DataTable中的完成列值。(如完成值为真正的行背景颜色已经被改变。)

But now I need to set the Background color of the GridView Row Basing on the DONE Column Value in DataTable.( If DONE value is true the Row Background Color has to be changed.)

我怎样才能做到这一点不绑定完成行到GridView ??

How can I achieve this without binding the DONE Row to the GridView??

推荐答案

为你的GridView创建 GridView1_RowDataBound 事件。

Create GridView1_RowDataBound event for your GridView.

if (e.Row.RowType == DataControlRowType.DataRow)
{
    //Check your condition here
    //Get Id from here and based on Id check value in the 
    //underlying dataSource Row where you have "DONE" column value
    // e.g.
    // (gridview.DataSource as DataTable), now you can find your row and cell 
    // of "Done"
    If(Condition True)
    {
        e.Row.BackColor = Drawing.Color.Red;  // your color settings 
    }
}

例如code片断:

example code snippet:

protected void EmployeeAvailabilityGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
               if (e.Row.RowType == DataControlRowType.DataRow)
                {                  
                    if(Convert.ToBoolean(DataBinder.Eval(e.Row.DataItem, "DONE")))
                    {
                        e.Row.BackColor = System.Drawing.Color.LightPink;
                    }
                }
            }
            catch (Exception ex)
            {
                //ErrorLabel.Text = ex.Message;
            }
        }

请参考以下链接了解更多详细的实施:结果
<一href=\"http://stackoverflow.com/questions/5048762/change-gridview-row-color-based-on-condition-in-c-sharp\">Change基于C#中条件的GridView行颜色

Refer following link for more detailed implementation:
Change GridView row color based on condition in C#

注意:如果该行不存在的数据源,那么你必须有一些逻辑来获取来自其他地方。可能是你有 ID 作为另一个表的外键。

Note: If that row does not exist in DataSource then you must have some logic to get that from other place. May be you have ID as Foreign Key in another table.

这篇关于在绑定使用DataSet的值设置的GridView行背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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