Datagrid MappingName
In the code for each datagrid, I process the following code to
If Not firstTime And Not gridFilledTrans Then
dgtsMyStyle.MappingName = "Stuff"
End If
For some reason, when I search for another record and then return to this tab for a second time, it errors saying that the MappingName already exists.
Does anyone know how I can do a check to see if the mapping name already exists? Here is the entire piece of code that fills the datagrid.
Private Sub FillTransGrid()
Dim myConnectionString As String
Dim mySQL As String
Dim i As Int16
Dim tabName As String = tabDonorInfo.SelectedTab.Name
If Not recordRestricted Then
mySQL = "USP_DonorGiftsGridByID @DonorID = " & Format(g_DonorBlock.DonorID)
Me.Cursor = Windows.Forms.Cursors.WaitCursor
myConnectionString = "Data Source=" & Trim(g_TargetServer) & "; Initial Catalog=" & Trim(g_Database) & "; User ID=demo; Password=demo_pass"
Dim myConnection As New SqlConnection(myConnectionString)
Dim sdaTransaction As SqlDataAdapter = New SqlDataAdapter(mySQL, myConnection)
Dim dtblScanStuff As New DataTable("Stuff")
Dim theQueryString As SqlCommand = New SqlCommand(mySQL, myConnection)
dvwScanStuff = dtblScanStuff.DefaultView()
dvwScanStuff.AllowEdit = False
dvwScanStuff.AllowNew = False
Dim sdaGridData As SqlDataAdapter = New SqlDataAdapter(theQueryString)
i = sdaGridData.Fill(dtblScanStuff) 'brings the data thru data adapter to "table"
If i > 0 Then
dgrdGifts.SetDataBinding(dvwScanStuff, "") ' connect display to "table's view"
dgrdGifts.Visible = True
' let's change the look of things. Add a table style
Dim dgtsMyStyle As DataGridTableStyle = New DataGridTableStyle()
dgtsMyStyle.AlternatingBackColor = dgtsMyStyle.AlternatingBackColor.Lavender
dgtsMyStyle.BackColor = dgtsMyStyle.BackColor.WhiteSmoke
dgtsMyStyle.SelectionBackColor = dgtsMyStyle.SelectionBackColor.CornflowerBlue
dgrdGifts.TableStyles.Add(dgtsMyStyle)
' now we add display columns
'Dim tcolEdit As New DataGridTextBoxColumn()
'tcolEdit.MappingName = "TranDate"
'tcolEdit.HeaderText = "Edit"
'tcolEdit.Width = 75
'tcolEdit.Alignment = HorizontalAlignment.Right
'dgtsMyStyle.GridColumnStyles.Add(tcolEdit)
Dim tcolDate As New DataGridTextBoxColumn()
tcolDate.MappingName = "TranDate"
tcolDate.HeaderText = "Gift Date"
tcolDate.Width = 75
tcolDate.Alignment = HorizontalAlignment.Right
dgtsMyStyle.GridColumnStyles.Add(tcolDate)
Dim tcolBatch As New DataGridTextBoxColumn()
tcolBatch.MappingName = "TranSourceCode"
tcolBatch.HeaderText = "Source"
tcolBatch.Width = 75
tcolBatch.Alignment = HorizontalAlignment.Right
dgtsMyStyle.GridColumnStyles.Add(tcolBatch)
Dim tcolAmt As New DataGridTextBoxColumn()
tcolAmt.MappingName = "TranAmount"
tcolAmt.HeaderText = "Gift Amount"
tcolAmt.Width = 75
tcolAmt.Alignment = HorizontalAlignment.Right
tcolAmt.Format = "c"
dgtsMyStyle.GridColumnStyles.Add(tcolAmt)
'need to add that only if this is first time through, do
If Not firstTime And Not gridFilledTrans Then
dgtsMyStyle.MappingName = "Stuff" ' and this makes it actually reconfigure onscreen
End If
Me.Refresh()
Me.Cursor = Windows.Forms.Cursors.Default()
dgrdGifts.Visible = True
lblShow.Visible = False
Else
dgrdGifts.Visible = False
lblShow.Visible = True
lblShow.Text = "(Non-Donor. No Gifts yet.)"
End If
End If
End Sub

