Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

It is fairly involved. You do it using templates. personally I don't like using pseudohtml templates in aspx pages - I like to build my own template classes. Here is an example of one I made to show either true or false in a dropdown on edit:

 


Public Class MyDropDownTemplate 
Implements ITemplate 

Private m_data As String 
Private m_read As Boolean 

Public Sub New(ByVal DataSource As String, ByVal [ReadOnly] As Boolean) 
   m_data = DataSource 
   m_read = [ReadOnly] 
End Sub 

Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements System.Web.UI.ITemplate.InstantiateIn 
   If m_read Then 
       Dim l As New System.Web.UI.WebControls.Literal 
       AddHandler l.DataBinding, AddressOf Me.DataBind 
       container.Controls.Add(l) 
   Else 
       Dim dl As New System.Web.UI.WebControls.DropDownList 
       AddHandler dl.DataBinding, AddressOf Me.DataBind 
        'now add the items to the list
       dl.Items.Add("True") 
       dl.Items.Add("False") 
       container.Controls.Add(dl) 
   End If 
End Sub 

Private Sub DataBind(ByVal sender As System.Object, ByVal e As System.EventArgs) 
   If m_read Then 
       Dim l As System.Web.UI.WebControls.Literal = CType(sender, System.Web.UI.WebControls.Literal) 
       Dim container As DataGridItem = CType(l.NamingContainer, DataGridItem) 
       Dim DRV As DataRowView = CType(container.DataItem, DataRowView) 
       l.Text = CType(DRV(m_data), String) 
   Else 
       Dim dl As System.Web.UI.WebControls.DropDownList = CType(sender, System.Web.UI.WebControls.DropDownList) 
       Dim container As DataGridItem = CType(dl.NamingContainer, DataGridItem) 
       Dim DRV As DataRowView = CType(container.DataItem, DataRowView) 
        'determine the selected item
       If CType(DRV(m_data), String).ToLower = "true" Then 
           dl.SelectedIndex = 0 
       Else 
           dl.SelectedIndex = 1 
       End If 
   End If 
    
End Sub 


End Class 

 

You then add the column to your datagrid using:

 

Dim t As New System.Web.UI.WebControls.TemplateColumn 
With t 
   .HeaderText = "Col Header" 
   .ItemTemplate = New MyDropDownTemplate("DataFieldName", True) 'displaying items is always read only
   .EditItemTemplate = New MyDropDownTemplate("DataFieldName", False) 
   .Visible = True 
End With 
.Columns.Add(t)

 

:)

Please check the Knowledge Base before you post.

"Computers are useless. They can only give you answers." - Pablo Picasso

The Code Net

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...