关于if语句和数组C#.Net [英] about if statement and arrays C#.Net

查看:84
本文介绍了关于if语句和数组C#.Net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的html

My html

<tr <%# FormatColorRow(DataBinder.Eval(Container.DataItem,"bugstoreid").ToString()) %>  id=''A1''     ><tr<%#>


我想突出显示包含bugstoreid的行,该行可能是多个
称我的功能为


I want to highlight rows containing bugstoreid which could be multiple
called my function as

protected string FormatColorRow(string bugstoreid)
    {       
       string[] names = new string[] { "170", "171"};
       //*my problem goes here.

       if (bugstoreid == names[0].ToString() || bugstoreid == names[1].ToString())
       {
          return "style=''backGround-color:LightBlue''";
       }  
       
       return "style=''backgroundColor=''''''";
    }



名称尽可能多,可以是{"170","171",..",......}
我不知道如何在此处声明if语句以一次返回所有名称的样式



Names are as multiple as possible can be {"170","171",......."",...........}
I don''t know how to declare if statement here to return style with all names at once

string names=new string{"170,"171",....}

以逗号分隔的表实体的值...请帮助我.
提前谢谢!

[edit]添加了代码块,禁用了忽略HTML ..."选项,整洁-OriginalGriff [/edit]

value of table entity seperated by comma ...please help me.
Thanks in advance!

[edit]Code block added, "Ignore HTML..." option disabled, general tidy - OriginalGriff[/edit]

推荐答案

我将使用通用列表字符串而不是数组:

I''d use a generic list of strings instead of an array:

protected string FormatColorRow(string bugstoreid)
    {
       List<string> names = new List<string> { "170", "171"};
       //*my problem goes here.
       if (names.Contains(bugstoreid))
       {
          return "style='backGround-color:LightBlue'";
       }
       return "style='backgroundColor='''";
    }
</string></string>



希望它会帮助您.



Hope it hepls.


嗨 Preeti,

您可以从后面的代码中设置行颜色.使用以下方法

Hi Preeti,

You can set Row Color from code behind. using following method

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string bugstoreid = GridView1.DataKeys[0].Value.ToString();
            string[] names = new string[] { "170", "171" };
            if (bugstoreid == names[0].ToString() || bugstoreid == names[1].ToString())
            {
                e.Row.BackColor = "LightBlue";
            }
        }
    }



谢谢,
Imdadhusen



Thanks,
Imdadhusen


我怀疑(尽管很难告诉)您要替换:
I suspect (though it is difficult to tell) that you want to replace:
if (bugstoreid == names[0].ToString() || bugstoreid == names[1].ToString())
   {
   return "style='backGround-color:LightBlue'";
   }  

with

with

foreach (string name in names)
   {
   if (bugstoreid == name)
      {
      return "style='backGround-color:LightBlue'";
      }
   }

BTW:您不需要在字符串上使用ToString:它已经是一个...

BTW: you do not need to use ToString on a string: it is one already...


这篇关于关于if语句和数组C#.Net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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