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();
The best thing to do is implement the
OnDataBindingmethod for aButtonin aTemplateColumn.Eg:
Then in your codebehind implement your logic:
The advantage to doing it in this manner over the other posted answers:
Buttoncontrol and can be reused if otherButtonsrequire the same functionality.DataSourcevalue and not what the visual output is (this could tie into business logic for both rendering and checking).Hope that helps.