•      Powered by
 

Conditional DataGrid Item and using checkboxes

Using datagrid in ASP.NET and having one bound column with checkboxes. This code check certain certain conditions before displaying the grid.

Here is the code:


<%@ Page EnableSessionState="false" EnableViewState="false" Language="vb" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>

<script runat="server">
    Dim conn as SQLConnection = new SQLConnection(ConfigurationSettings.AppSettings("DSN_Northwind"))
   
    Sub Page_Load(sender As Object, e As EventArgs)
     Dim sqlText as String
     Dim ds AS DataSet
     Dim dbComm AS SQLDataAdapter
     Dim sqlServer as String
       
         If Not IsPostBack Then
          sqlText = "select EmployeeID , firstname,lastname  from employees"
   ds = new DataSet()
          dbComm = New SQLDataAdapter(sqlText,conn)
          dbComm.Fill(ds,"Employees")
   
       dbGrid.DataSource = ds.Tables("Employees").DefaultView
           dbGrid.DataBind()
     End if   
    End Sub
   
    Function myFunc(myInt as Integer) as Boolean
         If myInt >3 and myInt <7 then
             Return True
         Else
             Return False
         End if
    End Function
</script>

<html>
<head>
    <title></title>
</head>
<body bgcolor="#FFFFFF">
    <form runat=server id=form1 name=form1>
      <ASP:DataGrid id="MyDataGrid" runat="server"
        BorderColor="black"
        BorderWidth="1"
        GridLines="none"
        CellPadding="4"
        Font-Name="Verdana"
        Font-Size="8pt"
        HeaderStyle-BackColor="#aaaadd"
        AutoGenerateColumns="False"
        >
       <Columns>
       <asp:TemplateColumn HeaderText="Quantity">
         <ItemTemplate>
         <asp:checkbox id="mycheckbox" Checked=<%# myFunc(Container.DataItem("EmployeeID") ) %> runat="server" />
  </ItemTemplate>
       </asp:TemplateColumn>
         <asp:BoundColumn HeaderText="EmployeeID" DataField="EmployeeID"/>
         <asp:BoundColumn HeaderText="firstname" DataField="firstname"/>
         <asp:BoundColumn HeaderText="lastname" DataField="lastname"/>
       </Columns>
      </asp:DataGrid>
    </form>             
</body>
</html> 

Web.Config file

<configuration>
     <appSettings>
           <add key="DSN" value="server=localhost;uid=sa;pwd=;database=yourdb"></add>
           <add key="DSN_NorthWind" value="server=localhost;database=Northwind;Trusted_Connection=Yes" />
     </appSettings>  
</configuration>

 

tio

Terms of Use | Privacy Statement ©2005-2006 IISLogs.com. All rights reserved - Powered by IIS7 - info @ www.IIS.net