Insert the DataGrid onto the DEFAULT.ASPX screen
In my example, I am displaying two columns, the first will hold the element name, the second will hold the instance value. At this point, we will leave the grid empty. In other instances, you may wish to populate the screen at design time with controls which are data independent.
Collapse
<asp:datagrid BorderWidth="1" id="dg1"
style="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 8px"
runat="server" AutoGenerateColumns="False" ShowHeader="False"
BorderStyle="None" GridLines="Both">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<ItemTemplate>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>
Step 3 ? Bind the data to the DataGrid
It is important to bind the control even though we do not have any data elements within the control, to raise a binding event for each line of the DataSet.
Collapse
dg1.DataSource = myDS
dg1.DataMember = "myDetails"
DataBind()
Step 4 - Inserting the controls into the DataGrid
The trick to dynamically inserting the controls is to use the ItemDataBound event associated with the onscreen DataGrid control. The ItemDataBound event will be called for each row of data in the DataSet. When each row event is raised, the appropriate control is created and inserted into the DataGrid. This example uses the e.Item.DataSetIndex to link the DataGrid rows and the DataSource rows.
Collapse
Private Sub dg1_ItemDataBound(ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) _
Handles dg1.ItemDataBound
'ensure we are looking at an item being bound
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = _
ListItemType.AlternatingItem Then
'always insert the dataelement name into the first column as a label.
Dim myLBL As New Label
myLBL.EnableViewState = True
myLBL.Text = dgTable(dg1).Rows(e.Item.DataSetIndex)(0)
e.Item.Cells(0).Controls.Add(myLBL)
If dgTable(dg1).Rows(e.Item.DataSetIndex)(2) = "txtbox" Then
'insert textbox and load with data
Dim myTB As New TextBox
If Not IsPostBack Then
myTB.Text = dgTable(dg1).Rows(e.Item.DataSetIndex)(1)
End If
e.Item.Cells(1).Controls.Add(myTB)
End If
If dgTable(dg1).Rows(e.Item.DataSetIndex)(2) = "checkbox" Then
'insert checkbox and load with data
Dim myCB As New CheckBox
If Not IsPostBack Then
If dgTable(dg1).Rows(e.Item.DataSetIndex)(1).tolower = "t" Then
myCB.Checked = True
Else
myCB.Checked = False
End If
End If
e.Item.Cells(1).Controls.Add(myCB)
End If
If dgTable(dg1).Rows(e.Item.DataSetIndex)(2) = "ddl" Then
'insert dropdownlist and load with data
Dim myDDL As New DropDownList
'for simplicity I will assume the ?CarMake? table
'always as the dropdownlist data source
For yy As Int16 = 0 To _
dg1.DataSource.Tables("CarMake").Rows.Count - 1
myDDL.Items.Add(dg1.DataSource.Tables("CarMake").Rows(yy)(0))
Next
e.Item.Cells(1).Controls.Add(myDDL)
If Not IsPostBack Then
myDDL.SelectedIndex = _
CType(dgTable(dg1).Rows(e.Item.DataSetIndex)(1), Int16)
End If
End If
End If
End Sub
'This function returns the table bound to the datagrid
Private Function dgTable(ByVal dg As DataGrid) As DataTable
Return dg1.DataSource.tables(dg1.DataMember)
End Function
No comments:
Post a Comment