Private dsProject As DataSet
Private dsProjectGroupType As DataTable
Private dsProjectGroup As DataTable
Private projectGroupsAndTypes As ProjectGroupsAndTypes
Private strprojectGroupTypeID As String
Private typeCount As Integer
' <summary>
' Handles the Load event of the Page control.
' </summary>
' <param name="sender">The source of the event.</param>
' <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
Try
projectGroupsAndTypes = New ProjectGroupsAndTypes
dsProjectGroupType = New DataTable
dsProjectGroup = New DataTable
typeCount = 0
strprojectGroupTypeID = String.Empty
If Not IsPostBack Then
LoadData()
End If
Catch exception As System.Exception
Throw exception
End Try
End Sub
' <summary>
' Loads the data.
' </summary>
Private Sub LoadData()
Try
dsProjectGroupType = projectGroupsAndTypes.projectsGroupType
dsProjectGroupType.TableName = "Master"
dsProjectGroup = projectGroupsAndTypes.projectGroup
dsProjectGroup.TableName = "Detail"
dsProject = New DataSet
dsProject.Tables.Add(dsProjectGroupType)
dsProject.Tables.Add(dsProjectGroup)
Dim relProjectGroups As DataRelation = New DataRelation("relProjectGroups", New DataColumn() {dsProject.Tables("Master").Columns("Project_Group_Type_ID")}, New DataColumn() {dsProject.Tables("Detail").Columns("Project_Group_Type_ID")})
dsProject.Relations.Add(relProjectGroups)
dgProjectGroupType.DataSource = dsProject.Tables("Master").DefaultView
dgProjectGroupType.DataBind()
Catch exception As System.Exception
Throw exception
End Try
End Sub
' <summary>
' Handles the ItemDataBound event of the dgProjectGroupType control.
' <param name="sender">The source of the event.</param>
' <param name="e">The <see cref="T:System.Web.UI.WebControls.DataGridItemEventArgs"/> instance containing the event data.</param>
'Here is where the detail grid is binded with data Source.
'Here there are two cases when it is an item or edititem.
'dgProjectGroup gets binds, when grid is not in edit mode.
'dgProjectGroup_Edit gets bind, when the gird is in edit mode.
'Note dgProjectGroup is again datagrid as dgProjectGroup.
Protected Sub dgProjectGroupType_ItemDataBound(ByVal sender As Object, ByVal e As DataGridItemEventArgs)
If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem OrElse e.Item.ItemType = ListItemType.EditItem Then
Dim drvDesc As DataRowView = CType(e.Item.DataItem, DataRowView)
If e.Item.ItemType = ListItemType.EditItem Then
CType(e.Item.FindControl("dgProjectGroup_Edit"), DataGrid).DataSource = drvDesc.CreateChildView("relProjectGroups")
CType(e.Item.FindControl("dgProjectGroup_Edit"), DataGrid).DataBind()
Else
CType(e.Item.FindControl("dgProjectGroup"), DataGrid).DataSource = drvDesc.CreateChildView("relProjectGroups")
CType(e.Item.FindControl("dgProjectGroup"), DataGrid).DataBind()
End If
End If
End Sub
' Handles the ItemCommand event of the dgProjectGroupType control.
' <param name="sender">The source of the event.</param>
' <param name="e">The <see cref="T:System.Web.UI.WebControls.DataGridCommandEventArgs"/> instance containing the event data.</param>
Public Sub dgProjectGroupType_ItemCommand(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
If e.CommandName = "InsertCategory" Then
Dim txtDescription As String = CType(e.Item.Cells(0).FindControl("addDescription"), TextBox).Text
Dim valid As Boolean = True
If (txtDescription.Equals(String.Empty)) Then
valid = False
End If
LoadData()
End If
If e.CommandName = "DeleteCategory" Then
Dim ProjectGroupTypeID As Integer = Convert.ToInt32(CType(e.Item.FindControl("projectGroupTypeID"), Label).Text)
LoadData()
End If
If e.CommandName = "EditCategory" Then
dgProjectGroupType.EditItemIndex = e.Item.ItemIndex
LoadData()
End If
If e.CommandName = "SaveCategory" Then
Dim ProjectGroupTypeID As Integer = Convert.ToInt32(CType((e.Item.Cells(1).Controls(1)), Label).Text)
Dim ProjectGroupType As String = CType(e.Item.Cells(0).Controls(1), TextBox).Text
dgProjectGroupType.EditItemIndex = -1
LoadData()
End If
If e.CommandName = "CancelCategory" Then
dgProjectGroupType.EditItemIndex = -1
LoadData()
End If
End Sub
'This is the important Procedure.Because the master ProjectTypeID is always binded with details Datagrid on the footer.
'So, whenever the a new record is added to the details grid the, we have the foreign key(ProjectTypeID).
'But there is no way around, other than this which I found to be quite simple and effective.
'On the footer event of ItemDataBoung, I am adding the ProjectTypeID to textBox(ID = addProjectTypeID).
'Since this events fires for every record od master table.
'I am increamenting the count(typeCount) such that detail table are matched to the master table.
'And this happens for the first time.But this event is fired whenever other edit event is fired on detials datagrid.
'And value stored in the textbox(ProjectTypeID) might be corrupted.
'So solution to this is else part.assiging the value which is already available in the details dataGrid.
Public Sub dgProjectGroup_OnItemDataBound(ByVal sender As Object, ByVal e As DataGridItemEventArgs)
If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
Dim drvDesc As DataRowView = CType(e.Item.DataItem, DataRowView)
strprojectGroupTypeID = drvDesc.Row("Project_Group_Type_Id").ToString
Else
If e.Item.ItemType = ListItemType.Footer Then
If strprojectGroupTypeID.Equals(String.Empty) Then
CType(e.Item.FindControl("addProjectTypeID"), TextBox).Text = Convert.ToString((CType(projectGroupsAndTypes.projectsGroupType(programmeID), DataTable).Rows(typeCount))(0))
Else
CType(e.Item.FindControl("addProjectTypeID"), TextBox).Text = strprojectGroupTypeID
strprojectGroupTypeID = ""
End If
System.Math.Min(System.Threading.Interlocked.Increment(typeCount), typeCount - 1)
End If
End If
End Sub
' Handles the ItemCommand event of the dgProjectGroup control.
' </summary>
' <param name="sender">The source of the event.</param>
' <param name="e">The <see cref="T:System.Web.UI.WebControls.DataGridCommandEventArgs"/>
' instance containing the event data.</param>
Public Sub dgProjectGroup_ItemCommand(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
'insert new record
If e.CommandName = "InsertGroup" Then
Dim projectGroupTypeID As Integer = Convert.ToInt32(CType(e.Item.FindControl("addProjectTypeID"), TextBox).Text)
Dim txtinnerDescription As String = CType(e.Item.FindControl("addProject"), TextBox).Text
Dim valid As Boolean = True
If txtinnerDescription.Equals(String.Empty) Then
valid = False
End If
'Do ever is req here.or something like this.
'projectGroupsAndTypes.AddProjectGroup
'(projectGroupTypeID, txtinnerDescription);
'Load and rebind the data
LoadData()
'Delete the Record
ElseIf e.CommandName = "DeleteGroup" Then
Dim projectGroupID As Integer = Convert.ToInt32(CType(e.Item.FindControl("projectGroupID"), Label).Text)
'SomeCode to delete the projectGroupID
projectGroupsAndTypes.DeleteProjectGroup(projectGroupID)
'Bind and rebind the data
LoadData()
'edits inner group
ElseIf e.CommandName = "EditGroup" Then
'Another UserCode Goes here
'Declare a temporary dataGrid and get referred to details datagrid.
'Assingn the dataGird editItemIdex with Itemindex.
Dim tmpDg As DataGrid = CType(sender, DataGrid)
tmpDg.EditItemIndex = CInt(e.Item.ItemIndex)
'delcare A dataview and get all records of the details //table.
'And now filter the dataview with the //Project_Group_Type_ID (foreign key in the details //table and primary in the master table)
Dim dv As New DataView()
dv = projectGroupsAndTypes.projectGroup(programmeID).DefaultView
dv.Sort = "Project_Group_Type_ID"
'The Project_Group_Type_ID is obtained by the following code.
Dim objExpr As Object = CType(e.Item.FindControl("projectGroupTypeID"), Label).Text
'Now assigning the filtered dataview back to the datagrid(ref as detail dataGrid)
tmpDg.DataSource = dv.FindRows(objExpr)
tmpDg.DataBind()
' saves edited data
ElseIf e.CommandName = "SaveGroup" Then
Dim emp_id As Integer = e.Item.ItemIndex
Dim tmpDg As DataGrid = CType(sender, DataGrid)
'retriving the ProjectGroupID from hidden control
Dim ProjectGroupID As Integer = Convert.ToInt32(CType(e.Item.FindControl("projectGroupID"), Label).Text)
'retriving the ProjectGroup from hidden control
Dim ProjectGroup As String = CType(e.Item.FindControl("txtGroup"), TextBox).Text
'performing some action to save
projectGroupsAndTypes.updateProjectGroup(ProjectGroupID, ProjectGroup)
'cancelling the edit eventby setting edititemindex to -1
tmpDg.EditItemIndex = -1
'delcare A dataview and get all records of the details //table.
Dim dv As DataView = projectGroupsAndTypes.projectGroup(programmeID).DefaultView
dv.Sort = "Project_Group_Type_ID"
'The Project_Group_Type_ID is obtained by the following code.
Dim objExpr As Object = CType(e.Item.FindControl("projectGroupTypeID"), Label).Text
'Now assigning the filtered dataview back to the datagrid(ref as detail dataGrid)
tmpDg.DataSource = dv.FindRows(objExpr)
tmpDg.DataBind()
'cancel the action
ElseIf e.CommandName = "CancelGroup" Then
Dim tmpDg As DataGrid = CType(sender, DataGrid)
tmpDg.EditItemIndex = -1
'delcare A dataview and get all records of the details //table.
Dim dv As DataView = projectGroupsAndTypes.projectGroup(programmeID).DefaultView
dv.Sort = "Project_Group_Type_ID"
'The Project_Group_Type_ID is obtained by the following code.
Dim objExpr As Object = CType(e.Item.FindControl("projectGroupTypeID"), Label).Text
'Now assigning the filtered dataview back to the datagrid(ref as detail dataGrid)
tmpDg.DataSource = dv.FindRows(objExpr)
tmpDg.DataBind()
End If
End Sub 'dgProjectGroup_ItemCommand
NOTE: THIS ARTICLE IS CONVERTED FROM C# TO VB.NET USING A CONVERSION TOOL. ORIGINAL ARTICLE CAN BE FOUND ON C# CORNER (http://www.c-sharpcorner.com/).