Conditional Gridview Text - Checkboxes

Posted by David Wier on 05/7/10 | Code Samples

This code sample shows how to either show or make invisible, a checkbox in each row of the Gridview, along with making text conditional, based on certain criteria. In this case, if the Postal code starts with a non-numeric character, we change it to "Alt Text", and we set the Visible property of the checkbox in that row to "False"

Code Behind:
Imports ystem.Drawing 
    Protected Sub doDataCheck(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
        Dim chkExp as CheckBox
        If e.Row.RowType = DataControlRowType.DataRow Then 
        Dim sCode as String=e.Row.Cells(7).text
            If Not isNumeric(sCode.Substring(1,1)) Then 
                e.Row.Cells(7).text="<i style='color:red'>(Alt Text)</i>" 
            chkExp= CType(e.row.FindControl("ck1"), Checkbox) 
            chkExp.Visible="False" 
            End If 
        End If 
    End Sub

HTML Source:
        <asp:GridView OnRowDataBound="doDataCheck" AutoGenerateColumns="False"
            DataSourceID="SqlDataSource1" ID="GridView2" runat="Server"> 
            <Columns> 
                <asp:TemplateField> 
                    <ItemTemplate> 
                        <asp:CheckBox ID="ck1" runat="server"></asp:CheckBox> 
                    </ItemTemplate> 
                </asp:TemplateField> 
                <asp:BoundField DataField="FirstName" HeaderText="First" /> 
                <asp:BoundField DataField="LastName" HeaderText="Last" /> 
                <asp:BoundField DataField="Title" HeaderText="Title" /> 
                <asp:BoundField DataField="Address" HeaderText="Address" /> 
                <asp:BoundField DataField="City" HeaderText="City" /> 
                <asp:BoundField DataField="Region" HeaderText="Region" /> 
                <asp:BoundField DataField="PostalCode" HeaderText="PostalCode" /> 
                <asp:BoundField DataField="country" HeaderText="country" /> 
            </Columns> 
            <HeaderStyle Backcolor="Blue" Font-Bold="True" Forecolor="White" /> 
        </asp:GridView> 
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:NWConn2 %>" 
            SelectCommand="Select FirstName, LastName, Title, Address, City, Region, PostalCode, country from Employees"> 
        </asp:SqlDataSource>























0 Comments

COMMENTS

Name:
URL:
Comment:

Comments are disabled for this article.