Datagrid MappingName

I have created a TabControl with multiple datagrids, each on an individual tab.

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

[3895 byte] By [codefund.com] at [2007-12-16]
# 1
You keep adding additional table styles with the same mapping name even though one already exists. The quick solution is when you do:

dgrdGifts.TableStyles.Add(dgtsMyStyle)

do this before hand:

dgrdGifts.TableStyles.Clear()

There is no need to keep adding them though, might want to figure out another way of doing it. You can configure the styles through the designer. That might be your best option.

Paul

codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 2
Thanks Paul - that worked!

I know I need to re-think the whole process, but that worked for now - I really appreciate it!

codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 3
There may be a bug in your code. You never update the value of "gridFilledTrans" so your code keeps adding table styles w/ the same mapping name to the data grid.
codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...