Change Background Color of ASP:GridView Based On Text

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite












I am using the OnRowDataBound() event to change the Row.BackColor based on the text. Now my issue is, this is extremely slow. I have roughly 100 rows in the ASP:Gridview and only need to evaluate the first row or index[0]. What optimization techniques do you guru's have on how to speed this process up?



protected void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e)

if (e.Row.RowType == DataControlRowType.DataRow)
question == "Check the state information below:")

e.Row.BackColor = System.Drawing.Color.Gray;









share|improve this question



















  • Do you have to use c# for this? It would probably be worth using css/javascript.
    – Sean T
    Jan 4 at 21:38










  • @SeanT - no, either of those are viable solutions. I am just not adapt enough to do such in css/js
    – Smith Stanley
    Jan 4 at 23:09










  • If it's always the first row, you can use DataBound event instead of RowDataBound. DataBound will fire only once - once all rows are bound. I'll post an example shortly.
    – Sean T
    Jan 5 at 14:35
















up vote
1
down vote

favorite












I am using the OnRowDataBound() event to change the Row.BackColor based on the text. Now my issue is, this is extremely slow. I have roughly 100 rows in the ASP:Gridview and only need to evaluate the first row or index[0]. What optimization techniques do you guru's have on how to speed this process up?



protected void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e)

if (e.Row.RowType == DataControlRowType.DataRow)
question == "Check the state information below:")

e.Row.BackColor = System.Drawing.Color.Gray;









share|improve this question



















  • Do you have to use c# for this? It would probably be worth using css/javascript.
    – Sean T
    Jan 4 at 21:38










  • @SeanT - no, either of those are viable solutions. I am just not adapt enough to do such in css/js
    – Smith Stanley
    Jan 4 at 23:09










  • If it's always the first row, you can use DataBound event instead of RowDataBound. DataBound will fire only once - once all rows are bound. I'll post an example shortly.
    – Sean T
    Jan 5 at 14:35












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I am using the OnRowDataBound() event to change the Row.BackColor based on the text. Now my issue is, this is extremely slow. I have roughly 100 rows in the ASP:Gridview and only need to evaluate the first row or index[0]. What optimization techniques do you guru's have on how to speed this process up?



protected void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e)

if (e.Row.RowType == DataControlRowType.DataRow)
question == "Check the state information below:")

e.Row.BackColor = System.Drawing.Color.Gray;









share|improve this question











I am using the OnRowDataBound() event to change the Row.BackColor based on the text. Now my issue is, this is extremely slow. I have roughly 100 rows in the ASP:Gridview and only need to evaluate the first row or index[0]. What optimization techniques do you guru's have on how to speed this process up?



protected void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e)

if (e.Row.RowType == DataControlRowType.DataRow)
question == "Check the state information below:")

e.Row.BackColor = System.Drawing.Color.Gray;











share|improve this question










share|improve this question




share|improve this question









asked Jan 4 at 20:06









Smith Stanley

234




234











  • Do you have to use c# for this? It would probably be worth using css/javascript.
    – Sean T
    Jan 4 at 21:38










  • @SeanT - no, either of those are viable solutions. I am just not adapt enough to do such in css/js
    – Smith Stanley
    Jan 4 at 23:09










  • If it's always the first row, you can use DataBound event instead of RowDataBound. DataBound will fire only once - once all rows are bound. I'll post an example shortly.
    – Sean T
    Jan 5 at 14:35
















  • Do you have to use c# for this? It would probably be worth using css/javascript.
    – Sean T
    Jan 4 at 21:38










  • @SeanT - no, either of those are viable solutions. I am just not adapt enough to do such in css/js
    – Smith Stanley
    Jan 4 at 23:09










  • If it's always the first row, you can use DataBound event instead of RowDataBound. DataBound will fire only once - once all rows are bound. I'll post an example shortly.
    – Sean T
    Jan 5 at 14:35















Do you have to use c# for this? It would probably be worth using css/javascript.
– Sean T
Jan 4 at 21:38




Do you have to use c# for this? It would probably be worth using css/javascript.
– Sean T
Jan 4 at 21:38












@SeanT - no, either of those are viable solutions. I am just not adapt enough to do such in css/js
– Smith Stanley
Jan 4 at 23:09




@SeanT - no, either of those are viable solutions. I am just not adapt enough to do such in css/js
– Smith Stanley
Jan 4 at 23:09












If it's always the first row, you can use DataBound event instead of RowDataBound. DataBound will fire only once - once all rows are bound. I'll post an example shortly.
– Sean T
Jan 5 at 14:35




If it's always the first row, you can use DataBound event instead of RowDataBound. DataBound will fire only once - once all rows are bound. I'll post an example shortly.
– Sean T
Jan 5 at 14:35










1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










Generally, styling should be handled using client side methods such as using css/javascript so I would recommend you do some brushing up on that side. Never the less I was like you when I first started and just jumped straight into c# so here is a purely c# based solution :).



If it's only ever going to be the first row that needs to be shaded grey you can just reference that row when the binding in complete. The event you're currently subscribing to OnRowDataBound will fire for every row. There is another event that can be accessed once all rows are completed called OnDataBound.



First thing to do would be to register this event in the html markup of your gridview, using the same approach you used to register the OnRowDataBound event.



<asp:gridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnDataBound="GridView1_OnDataBound"></asp:gridView>


Then you can code up the event in your c#.



protected void GridView1_OnDataBound(object sender, EventArgs e)

if(GridView1.Rows.Count > 0) //Check there are rows bound

var Row = GridView1.Rows[0];// Get The First Row - you may have a header row. If this is the case change 0's to 1's!

Row.BackColor = System.Drawing.Color.Gray;// Assign the back color







share|improve this answer





















    Your Answer




    StackExchange.ifUsing("editor", function ()
    return StackExchange.using("mathjaxEditing", function ()
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
    );
    );
    , "mathjax-editing");

    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "196"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );








     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f184306%2fchange-background-color-of-aspgridview-based-on-text%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    Generally, styling should be handled using client side methods such as using css/javascript so I would recommend you do some brushing up on that side. Never the less I was like you when I first started and just jumped straight into c# so here is a purely c# based solution :).



    If it's only ever going to be the first row that needs to be shaded grey you can just reference that row when the binding in complete. The event you're currently subscribing to OnRowDataBound will fire for every row. There is another event that can be accessed once all rows are completed called OnDataBound.



    First thing to do would be to register this event in the html markup of your gridview, using the same approach you used to register the OnRowDataBound event.



    <asp:gridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnDataBound="GridView1_OnDataBound"></asp:gridView>


    Then you can code up the event in your c#.



    protected void GridView1_OnDataBound(object sender, EventArgs e)

    if(GridView1.Rows.Count > 0) //Check there are rows bound

    var Row = GridView1.Rows[0];// Get The First Row - you may have a header row. If this is the case change 0's to 1's!

    Row.BackColor = System.Drawing.Color.Gray;// Assign the back color







    share|improve this answer

























      up vote
      1
      down vote



      accepted










      Generally, styling should be handled using client side methods such as using css/javascript so I would recommend you do some brushing up on that side. Never the less I was like you when I first started and just jumped straight into c# so here is a purely c# based solution :).



      If it's only ever going to be the first row that needs to be shaded grey you can just reference that row when the binding in complete. The event you're currently subscribing to OnRowDataBound will fire for every row. There is another event that can be accessed once all rows are completed called OnDataBound.



      First thing to do would be to register this event in the html markup of your gridview, using the same approach you used to register the OnRowDataBound event.



      <asp:gridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnDataBound="GridView1_OnDataBound"></asp:gridView>


      Then you can code up the event in your c#.



      protected void GridView1_OnDataBound(object sender, EventArgs e)

      if(GridView1.Rows.Count > 0) //Check there are rows bound

      var Row = GridView1.Rows[0];// Get The First Row - you may have a header row. If this is the case change 0's to 1's!

      Row.BackColor = System.Drawing.Color.Gray;// Assign the back color







      share|improve this answer























        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted






        Generally, styling should be handled using client side methods such as using css/javascript so I would recommend you do some brushing up on that side. Never the less I was like you when I first started and just jumped straight into c# so here is a purely c# based solution :).



        If it's only ever going to be the first row that needs to be shaded grey you can just reference that row when the binding in complete. The event you're currently subscribing to OnRowDataBound will fire for every row. There is another event that can be accessed once all rows are completed called OnDataBound.



        First thing to do would be to register this event in the html markup of your gridview, using the same approach you used to register the OnRowDataBound event.



        <asp:gridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnDataBound="GridView1_OnDataBound"></asp:gridView>


        Then you can code up the event in your c#.



        protected void GridView1_OnDataBound(object sender, EventArgs e)

        if(GridView1.Rows.Count > 0) //Check there are rows bound

        var Row = GridView1.Rows[0];// Get The First Row - you may have a header row. If this is the case change 0's to 1's!

        Row.BackColor = System.Drawing.Color.Gray;// Assign the back color







        share|improve this answer













        Generally, styling should be handled using client side methods such as using css/javascript so I would recommend you do some brushing up on that side. Never the less I was like you when I first started and just jumped straight into c# so here is a purely c# based solution :).



        If it's only ever going to be the first row that needs to be shaded grey you can just reference that row when the binding in complete. The event you're currently subscribing to OnRowDataBound will fire for every row. There is another event that can be accessed once all rows are completed called OnDataBound.



        First thing to do would be to register this event in the html markup of your gridview, using the same approach you used to register the OnRowDataBound event.



        <asp:gridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnDataBound="GridView1_OnDataBound"></asp:gridView>


        Then you can code up the event in your c#.



        protected void GridView1_OnDataBound(object sender, EventArgs e)

        if(GridView1.Rows.Count > 0) //Check there are rows bound

        var Row = GridView1.Rows[0];// Get The First Row - you may have a header row. If this is the case change 0's to 1's!

        Row.BackColor = System.Drawing.Color.Gray;// Assign the back color








        share|improve this answer













        share|improve this answer



        share|improve this answer











        answered Jan 5 at 14:54









        Sean T

        1813




        1813






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f184306%2fchange-background-color-of-aspgridview-based-on-text%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            Greedy Best First Search implementation in Rust

            Function to Return a JSON Like Objects Using VBA Collections and Arrays

            C++11 CLH Lock Implementation