I have a GridView control that I bind to a dataset that contains two tables. I then create the relationship and display the hierarchy fine. I want to control the way that new rows are added in my grid so the AllowAddNewRow for both master and child tables is set to false. I have a custom form that creates new rows. To simulate this, I just have a command column that inserts some hardcoded values into the grid.
The problem is that I cannot get the grid to refresh with the newly added records in the child table. When I click the "Add" command column, a vertical scroll bar appears in the child table but I cannot see the rows. Below is the sample code that I have. Any ideas will be greatly appreciated. Thanks.
Private
Sub frmCustomProperties_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim adpProperties As New SqlDataAdapter()
' ... here I populate the database with two tables named cntCustomProperties and PropertyValues
adpProperties.Fill(dsProperties)
' set the master
grdProperties.DataSource = dsProperties.Tables(
"cntCustomProperties")
grdProperties.MasterGridViewTemplate.AllowAddNewRow =
False
grdProperties.MasterGridViewTemplate.AllowDeleteRow =
False
grdProperties.MasterGridViewTemplate.AllowEditRow =
False
' template for values
Dim ValuesTemplate As Telerik.WinControls.UI.GridViewTemplate = New Telerik.WinControls.UI.GridViewTemplate
ValuesTemplate.DataSource = dsProperties.Tables(
"PropertyValues")
ValuesTemplate.AllowAddNewRow =
False
ValuesTemplate.AllowDeleteRow =
False
ValuesTemplate.AllowEditRow =
False
grdProperties.MasterGridViewTemplate.ChildGridViewTemplates.Add(ValuesTemplate)
' configure relation between two tables
Dim relation As Telerik.WinControls.UI.GridViewRelation = New Telerik.WinControls.UI.GridViewRelation(grdProperties.MasterGridViewTemplate)
relation.ChildTemplate = ValuesTemplate
relation.RelationName =
"p2v"
relation.ParentColumnNames.Add(
"PropertyId")
relation.ChildColumnNames.Add(
"PropertyId")
grdProperties.Relations.Add(relation)
' now configure the two templates
grdProperties.MasterGridViewTemplate.Columns(
"PropertyId").IsVisible = False
grdProperties.MasterGridViewTemplate.Columns(
"PropertyName").BestFit()
Dim cmdColumnAdd As New GridViewCommandColumn()
cmdColumnAdd.UniqueName =
"cmdColumnAdd"
cmdColumnAdd.UseDefaultText =
True
cmdColumnAdd.DefaultText =
"Add"
cmdColumnAdd.FieldName =
"PropertyId"
cmdColumnAdd.HeaderText =
"Add"
cmdColumnAdd.BestFit()
grdProperties.MasterGridViewTemplate.Columns.Add(cmdColumnAdd)
ValuesTemplate.Columns(
"PropertyId").IsVisible = False
ValuesTemplate.Columns(
"PropertyValueId").IsVisible = False
ValuesTemplate.Columns(
"CompanyCode").IsVisible = False
ValuesTemplate.Columns(
"EffectiveFrom").BestFit()
ValuesTemplate.Columns(
"Value").BestFit()
Dim cmdColumnEdit As New GridViewCommandColumn
cmdColumnEdit.UniqueName =
"cmdColumnEdit"
cmdColumnEdit.UseDefaultText =
True
cmdColumnEdit.DefaultText =
"Edit"
cmdColumnEdit.FieldName =
"PropertyValueId"
cmdColumnEdit.HeaderText =
"Edit"
cmdColumnEdit.BestFit()
ValuesTemplate.Columns.Add(cmdColumnEdit)
AddHandler grdProperties.CommandCellClick, AddressOf grdProperties_CommandCellClick
End Sub
Sub
grdProperties_CommandCellClick(ByVal sender As Object, ByVal e As EventArgs)
Dim cmdCell As GridCommandCellElement
cmdCell =
TryCast(sender, GridCommandCellElement)
Dim tName As String = CType(cmdCell.ViewTemplate.DataSource, DataTable).TableName
If tName = "cntCustomProperties" Then
AddCustomProperty(cmdCell.Value)
ElseIf tName = "PropertyValues" Then
'-- code for the edit here
End If
End Sub
Private Sub AddCustomProperty(ByVal pid As Integer)
Dim nr As DataRow = dsProperties.Tables("PropertyValues").NewRow
nr.BeginEdit()
nr(
"propertyid") = pid
nr(
"companycode") = "10"
nr("effectivefrom") = "1900/1/1"
nr(
"value") = "some new value here"
nr.EndEdit()
dsProperties.Tables(
"PropertyValues").Rows.Add(nr)
dsProperties.Tables(
"PropertyValues").AcceptChanges()
' I have tried many combinations but none seems to work.
grdProperties.GridElement.Update(GridUINotifyAction.AddRow)
'grdProperties.MasterGridViewTemplate.Update(GridUINotifyAction.BatchDataChanged)
'grdProperties.MasterGridViewTemplate.ChildGridViewTemplates(0).Update(GridUINotifyAction.BatchDataChanged))
End Sub