John KilgoShajahan Kakkattil ("Shaji")






'This function is the heart of this application.
'It reads each grid rows,get the edited data
'and serialize back to the dataset.
'Yes the reverse process of "datagrid.databind", hence the name.
'The grid row
Dim gridrow As DataGridItem
'Typed data set row
Dim datarow As Dataset1.WorkExperianceDataRow
'Loop thru each grid row and synchronize the edited data
'with the corresponding data set.
'In this demo the typed data set has a one to one mapping
'with the templated columns for the sake of simplicity.
For Each gridrow In DataGrid1.Items
'"DatsetIndex" property of the gridrow gives the
'refernce to the datrow used for binding
datarow = _dataSet.WorkExperianceData(gridrow.DataSetIndex)
'get the data from grid row element and update the column in the 'data row
datarow.ExperiancePeriod = CType(gridrow.FindControl("ExperiancePeriod"), TextBox).Text
'(Note:the hardcoded control names is not a good programming 'practice, better to use
'a constant instead.)
'reverse bind the other fields
datarow.TotalYears = CShort(CType(gridrow.FindControl("TotalYears"), TextBox).Text)
datarow.CompanyName = CType(gridrow.FindControl("CompanyName"), TextBox).Text
datarow.JobDescription = CType(gridrow.FindControl("JobDescription"), TextBox).Text
'Update the data tabe with new values.
_dataSet.WorkExperianceData(gridrow.DataSetIndex).ItemArray = datarow.ItemArray
Next
End Sub
DataGrid1" is used for entering and editing the data. The definition of DataGrid1 is as follows:
<Columns>
<asp:TemplateColumn HeaderText="Period">
<ItemTemplate>
<asp:TextBox id="ExperiancePeriod" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="No of Years">
<ItemTemplate>
<asp:TextBox id="TotalYears" runat="server" Width="70px"></asp:TextBox>
<asp:CompareValidator id="CompareValidator1" runat="server" Type="Integer"
Operator="DataTypeCheck" ControlToValidate="TotalYears"
ErrorMessage="No of Years - Numeric value expected">!
</asp:CompareValidator>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Name of the Company">
<ItemTemplate>
<asp:TextBox id="CompanyName" runat="server" Width="256px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Job Description">
<ItemTemplate>
<asp:TextBox id="JobDescription" runat="server" Width="224px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Delete">
<ItemTemplate>
<asp:LinkButton id="DeleteRow" runat="server">X</asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle NextPageText="Next" PrevPageText="Previous"
HorizontalAlign="Center" Mode="NumericPages">
</PagerStyle>
</asp:datagrid>
<xs:element name="Dataset1" msdata:IsDataSet="true">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="WorkExperianceData">
<xs:complexType>
<xs:sequence>
<xs:element name="ExperiancePeriod" type="xs:string" minOccurs="0" />
<xs:element name="TotalYears" type="xs:short" minOccurs="0" />
<xs:element name="CompanyName" type="xs:string" minOccurs="0" />
<xs:element name="JobDescription" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
AddRows; Link button to add additional rows
Save; Command button. The data is not saved to anywhere instead it is displayed in "DataGrid2" in read-only mode
DataGrid2; datagrid to show the edited data.
'Put user code to initialize the page here
If Not IsPostBack Then
'Load the data for the first time
InitializeData()
BindGrid()
Else
'Get the edited data, and populate back to the data holder
' (dataset)
ReverseBind()
End If
End Sub
'Initializing the data for the first time.
'This is just a "stub" to load some startup data for demo purpose only.
'Normally the data will be loaded from data provider (database) or it can be empty initially.
With _dataSet.WorkExperianceData
.AddWorkExperianceDataRow("1/Jan/1988 to 31/Dec/2000", 3, "IBM", "Bulding IBM Mainframe systems")
etc……
etc……
End With
End Sub
MyBase.LoadViewState(savedState)
'Get the dataset from the viewstate. This is the data before last editing
If (Not Me.ViewState("Data") Is Nothing) Then
_dataSet = CType(Me.ViewState("Data"), Dataset1)
End If
If (Not Me.ViewState("LastEditedPage") Is Nothing) Then
'Just a variable to keep the last edited page index for the purpose of this application
_lastEditedPage = CType(Me.ViewState("LastEditedPage"), Integer)
End If
End Sub
Protected Overrides Function SaveViewState() As Object
'The datset used for editing needs to be persisted across postbacks.
'The performance will be affected if the dataset is huge in size.Please
'refer the tutorial for more details
Me.ViewState("Data") = _dataSet
Me.ViewState("LastEditedPage") = _lastEditedPage
Return (MyBase.SaveViewState())
End Function
'Add the number of rows specified by "RowCount" to grid
If (IsNumeric(RowCount.Text)) Then
Dim i As Integer
For i = 1 To CInt(RowCount.Text)
'Adding a blank record to the dataset.
_dataSet.WorkExperianceData.AddWorkExperianceDataRow("", 0, "", "")
'In our case the typed datset has only a few fileds, so that it is
'easy to pass the default data as arguments. If there are many
'fields (that is the case usually in real life scenarios)
'instatiate a data row and load initialization data one by one. Then
'plug it into the data table.
'You can even pass blank row with out initialization data, but in
'that case you need to handle "DBNull" situation during grid row
'binding either by checking "IsDBNull" or by
'setting default value property for the field in the the dataset.
Next
End If
'Rebind the grid to show the newly added row(s).
BindGrid()
End Sub
'This event handler is executed once per each row of the datagrid
'while binding the dataset records.
'The templated controls get populated with data here from
'the "workexperiance" data table
If (e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem) Then
Dim datarow As Dataset1.WorkExperianceDataRow
datarow = CType(CType(e.Item.DataItem, DataRowView).Row, Dataset1.WorkExperianceDataRow)
CType(e.Item.FindControl("ExperiancePeriod"), TextBox).Text = datarow.ExperiancePeriod
CType(e.Item.FindControl("TotalYears"), TextBox).Text = datarow.TotalYears.ToString
CType(e.Item.FindControl("CompanyName"), TextBox).Text = datarow.CompanyName
CType(e.Item.FindControl("JobDescription"), TextBox).Text = datarow.JobDescription
End If
End Sub
You may download the code here.