Click here to Skip to main content
15,891,864 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
My html
<tr <%# FormatColorRow(DataBinder.Eval(Container.DataItem,"bugstoreid").ToString()) %>  id='A1'     ><tr<%#>

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='''";
    }


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",....}
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]
Posted
Updated 6-Mar-11 21:27pm
v3

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

C#
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.
 
Share this answer
 
v2
Comments
Manfred Rudolf Bihy 7-Mar-11 3:43am    
Nice solution. 5+
I like it that you wrapped it into a function. I'd make one minor adjustment by making the list of names also a parameter to the function. (Reuse!)
Sunasara Imdadhusen 7-Mar-11 4:44am    
My vote is 5,
good call!!
Hi Preeti,

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

C#
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";
            }
        }
    }


Thanks,
Imdadhusen
 
Share this answer
 
Comments
Manfred Rudolf Bihy 7-Mar-11 3:40am    
My vote of 4:
What if OP decided to add more entries to the names array? Why not use Array.IndexOf(T[], T) to detect if the element was in the array? Even though your solution works it could be rewritten to be more generic.
I suspect (though it is difficult to tell) that you want to replace:
C#
if (bugstoreid == names[0].ToString() || bugstoreid == names[1].ToString())
   {
   return "style='backGround-color:LightBlue'";
   }  
with
C#
foreach (string name in names)
   {
   if (bugstoreid == name)
      {
      return "style='backGround-color:LightBlue'";
      }
   }
BTW: you do not need to use ToString on a string: it is one already...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900