如何为要从datalist列接收的字符串指定颜色 [英] How to assign a color to a string to be received from a datalist column

查看:110
本文介绍了如何为要从datalist列接收的字符串指定颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从datalist列发送到gridview文本框收到一个字符串值

'TB_Shipping.Text'。在数据列表的运输栏中,可能存在任何一个数据,例如1.'Eligible'2.'Not Eligible'。现在,当我通过文本框TB_Shipping.Text显示Gridview时,如果检索到的数据符合不符合条件,我想以红色显示。



TextBox TB_Shipping =(TextBox)grvcart.Rows [rowIndex] .Cells [6] .FindControl(TBShipping);

TB_Shipping.Text = dt.Rows [i] [Shipping] .ToString();

I receive a string value from a datalist column 'shipping' to a gridview textbox
'TB_Shipping.Text'. In the 'shipping' column of the datalist there would be either of the datas such as 1.'Eligible' 2.'Not Eligible'. Now, when I show the Gridview, through textbox 'TB_Shipping.Text', I want to show in color red if the retrieved data falls in line with 'Not eligible' .

TextBox TB_Shipping = (TextBox)grvcart.Rows[rowIndex].Cells[6].FindControl("TBShipping");
TB_Shipping.Text = dt.Rows[i]["Shipping"].ToString();

推荐答案

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace POC
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("shipping", typeof(string));
                dt.Rows.Add("Eligible");
                dt.Rows.Add("Not Eligible");
                dt.Rows.Add("Eligible");
                dt.Rows.Add("Not Eligible");

                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
        }



        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {

                string shipping = DataBinder.Eval(e.Row.DataItem, "shipping") + "";
                TextBox TB_Shipping = e.Row.FindControl("TB_Shipping") as TextBox;
                TB_Shipping.Text = shipping;
                if (shipping == "Not Eligible")
                    TB_Shipping.ForeColor = System.Drawing.Color.Red;
                 
            }

        } 
    }
}













<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="100%"

        OnRowDataBound="GridView1_RowDataBound">
        <Columns>
            <asp:TemplateField HeaderText=" Candidate name">
                <ItemTemplate>
                    <asp:TextBox ID="TB_Shipping" runat="server"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField HeaderText="shipping" DataField="shipping" />
        </Columns>
    </asp:GridView>


这篇关于如何为要从datalist列接收的字符串指定颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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