1

Here's the scenario. I have 2 gridviews. Gridview 1 is like this for example

 __________________________________
| select | cell 2 | cell 3         |

Then when you click on the select in cell 1 it populates Gridview 2.

  _______________________________
 | cell 1 | cell 2 | BUTTON      |

And then when the second gridview loads up I am trying to disabled the button if the value in cell 2 is true.

I've tried to disable the button using the rowdatabound event. Grabbing the cell text info with the following.

On the aspx page

<asp:GridView ID="GridView10" runat="server" AutoGenerateColumns="False" 
                Font-Size="10px" CellPadding="2" 
                DataKeyNames="ID" DataSourceID="SqlDataSource35" Width="100%" 
                BorderColor="#CCCCCC" OnRowCommand="GridView10_RowCommand" 
                onrowdatabound="GridView10_RowDataBound"

in code behind

protected void GridView10_RowDataBound(object sender, GridViewRowEventArgs e)
{
    string cellt = e.Row.Cells[5].Text;

    if (cellt == "True")
    {
        Button btn = (Button)e.Row.Cells[6].Controls[1];
        btn.Enabled = false;
    }
}

Normally this works just spiffy. But for whatever reason the event doesn't seem to be firing in this scenario. Anyone has any insight into this issue?

5
  • So you're saying the RowDataBound event you've shown doesn't fire at all? Is that event attached to the GridView declaritively (in the markup) or programmatically (in the code behind)? May 15, 2013 at 15:55
  • You can use a templatefield and just set Enabled on the button = <%# Eval("dbColumnThatPopulatesSecondGridViewColumn").ToString().ToLowerInvariant().Equals("True") %> May 15, 2013 at 17:42
  • Judging by the code you copied in the onrowdatabound isn't being picked up by intellisense .... could be as easy as fixing the casing to OnRowDataBound
    – urbanlemur
    May 15, 2013 at 17:46
  • @urbanlemur Oddly enough, those event delegate properties are not case-sensitive (though the actual Control properties are). May 15, 2013 at 18:22
  • urbanlemur: Good eye man. That did the trick. +1 for you.
    – Prescient
    May 15, 2013 at 19:16

1 Answer 1

0

Are you sure you have the correct Gridview cell? You might be using the wrong cell ID Where

protected void GridView10_RowDataBound(object sender, GridViewRowEventArgs e)
{
    string cellt = e.Row.Cells[CORRECT CELL?].Text;

     if (cellt == "True")
    {
        Button btn = (Button)e.Row.Cells[CORRECT CELL?].Controls[1];
        btn.Enabled = false;
    }
}

If this is the correct cell, You can just change the visibility property

Buttonname.visible = false

Also, You can do this

protected void GridView10_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        string cellt = e.Row.Cells[CORRECT CELL?].Tostring;
         if (cellt != "True") continue;
        {
            Button btn = (Button)e.Row.Cells[CORRECT CELL?].Controls[1];
            buttonname.visible = false;
            break;
        }
    }

Remember gridviews start from cell 0

So if you have it as cell 6 , make sure you're starting from cell 0 - so cell 6 might be cell 5..

EDIT, I've noticed your asking if a cell has a specific text, Use .Tostring shown above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.