4

I have added a button column to a gridview. I am populating the gridview with a datable through code then binding the datable to the grid.

I need to now look at the first column of the data and check if the text of the column is "NA" if it is the button in that column has to be disabled.....

How can I accomplish this? I am populating the data from code and the button is preadded to the grid in the markup

<asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView1_RowCommand">
    <Columns>
        <asp:ButtonField Text="Delete" />
    </Columns>
</asp:GridView>


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

5 Answers 5

5

The best thing to do is implement the OnDataBinding method for a Button in a TemplateColumn.

Eg:

<asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView1_RowCommand">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Button runat="server" ID="btnDelete" CommandName="Delete" 
                    Text="Delete" OnDataBinding="btnDelete_DataBinding" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Then in your codebehind implement your logic:

protected void btnDelete_DataBinding(object sender, System.EventArgs e)
{
    Button btn = (Button)(sender);
    btn.Enabled = !Eval("TheFieldInYourDataSourceToCompare").ToString().Equals("NA");
}

The advantage to doing it in this manner over the other posted answers:

  1. No code in your markup
  2. Code is localized to the Button control and can be reused if other Buttons require the same functionality.
  3. Comparing to the DataSource value and not what the visual output is (this could tie into business logic for both rendering and checking).

Hope that helps.

Sign up to request clarification or add additional context in comments.

3 Comments

Just a note the the above C# has a syntax error ...missing () after toString
@mickey Thanks. Old post but I fixed it :) Hope it was helpful.
I tried to implement as Kelsey suggests, but I have a DataGrid instead of a GridView. I get this run-time error: Parser Error Message: System.Web.UI.WebControls.DataGridColumnCollection must have items of type 'System.Web.UI.WebControls.DataGridColumn'. 'asp:TemplateField' is of type 'System.Web.UI.WebControls.TemplateField'., so I think I must use <asp:TemplateColumn> instead
1

Try this in the DataBound event handler:

protected void GridView1_DataBound(object sender, EventArgs e)
{

    for (int i = 0; i < GridView1.Rows.Count; i++)
    {

        if (GridView1.Rows[i].Cells[1].Text == "NA")
        {
          // Disable the button
        }
    }
}

This is just a general idea. You'll have to modify the code for your application.

Don't forget to add OnDataBound="GridView1_DataBound" to the markup for the GridView.

Comments

1

Maybe something like this. Did the button a little differently

<asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView1_RowCommand">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Button runat="server" ID="Btn_Delete" CommandName="Delete" Text="delete" 
                    Enabled='<%# GridView1.Rows[0].Cells[1].Text != "NA" %>' />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

You'll want to you Eval("someColumn") most likely instead of GridView1.Rows[0].Cells[1].Text

3 Comments

Good alternative. One thing to point out though - you've hardcoded Rows[0]. You'll probably want to base the enable/disable on the corresponding row, not the first row every time :)
Yeah, definitely. I did that since Nick didn't provide us with the full code.
So Eval("DatabaseColumn") would probably work depending on the situation I guess
0

Perhaps you can use OnRowDataBound="GridViewRowEventHandler" to set the value to enabled or disabled?

Comments

0

you can try from markup as,

 <asp:TemplateField HeaderText="QA signature">
                                     <EditItemTemplate>
                                         <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Column6") %>'></asp:TextBox>
                                     </EditItemTemplate>
                                     <ItemTemplate>
                                         <asp:Label ID="Label3" runat="server" Text='<%# Eval("Column6") %>' Visible='<%# Eval("Column6") != "" %>'  ></asp:Label>
                                         <asp:Button ID="Button2" runat="server" Text="Sign Off" CssClass="cmdButton" Visible='<%# Eval("Column6") == "" %>'  />
                                     </ItemTemplate>
                                 </asp:TemplateField>

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.