Question

I am building a WPF D.A.L. generator.

In my main page, I have a DataGrid populated with a list of the tables in my database.

I also have an extra checkbox column called ShouldInclude? I intend to use this to determine whether or not the table should be included in the generation... yes if checked, no if un-checked.

Because I am populating the DataGrid's ItemSource with a strongly typed list of some basic info TableName, Schema, Columns, I am now finding myself at a loss as to how I can get the checked value of the checkbox so I can make that determination on whether to include it or not.

Here are my functions that build out my table typing class code files:

Private Sub GenerateTyping(ByVal _DG As DataGrid)
    For Each i As TableTyping In _DG.Items
        'check if should be generated

        Dim _TString As String = String.Empty
        Using _sr As New StreamReader(Common.GetPath() & "Class Templates\CSharp\Typing\XXX_Typing.txt")
            _TString = _sr.ReadToEnd()
            _sr.Close()
        End Using
        Dim _FN As String = i.Name & "_Typing.cs"
        Dim _Props As New StringBuilder()
        Dim _CL As List(Of ColumnTyping) = i.Columns
        For Each col In _CL
            With _Props
                Dim _PropStr As String = "public " & Common.GetClrType(col.Type) & " " & col.Name & " { get; set; }"
                .AppendLine("       " & _PropStr)
            End With
        Next
        'Write the new class files
        _TString = _TString.Replace("##TABLENAME##", If(i.Schema.Length > 0, i.Schema & "_", "") & i.Name).Replace("##THE_PROPERTIES##", _Props.ToString())
        If Not Directory.Exists(FilePath & "\Typing\") Then
            Directory.CreateDirectory(FilePath & "\Typing\")
        End If
        Using _sw As New StreamWriter(FilePath & "\Typing\" & If(i.Schema.Length > 0, i.Schema & "_", "") & i.Name & "_Typing.cs", False)
            _sw.Write(_TString)
            _sw.Close()
        End Using
        _TString = String.Empty
        _Props.Clear()

    Next
End Sub

Partial Public Class TableTyping
    Public Property ID As Integer
    Public Property Name As String
    Public Property Schema As String
    Public Property Columns As List(Of ColumnTyping)
End Class

Partial Public Class ColumnTyping
    Public Property ID As Integer
    Public Property Name As String
    Public Property Type As SqlDataType
    Public Property Length As Integer
    Public Property DefaultValue As String
    Public Property Precision As Integer
    Public Property Scale As Integer
End Class

My datagrid simply consists of 3 columns. Include?, Table Schema, Table Name, which is populated via:

<DataGrid EnableRowVirtualization="True" Name="dgTables" IsReadOnly="True" AutoGenerateColumns="False" FontFamily="Calibri" FontSize="14" IsEnabled="False">
    <DataGrid.Columns>
        <DataGridTemplateColumn Header="Include?">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <CheckBox Tag="{Binding ID}" HorizontalAlignment="Center" VerticalAlignment="Center" IsChecked="True" Name="ckTblInclude" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
        <DataGridTextColumn Binding="{Binding Schema}" Header="Schema"/>
        <DataGridTextColumn Binding="{Binding Name}" Header="Name"/>
    </DataGrid.Columns>
</DataGrid>

AND:

_tg.ItemsSource = _Table

Private Sub GrabTables()
    Dim _Db As Database = Srv.Databases(DBName)
    Dim _Tbls As TableCollection = _Db.Tables
    Dim _tct As Integer = _Tbls.Count
    Dim _i As Integer = 0
    For i = 0 To _tct - 1
        If Not _Tbls(i).IsSystemObject Then
            _i += 1
            _Tables.Add(New TableTyping() With {
                        .ID = _i,
                        .Name = _Tbls(i).Name,
                        .Schema = _Tbls(i).Schema,
                        .Columns = ProcessColumns(_Tbls(i).Columns)})

        End If
    Next
    _TCount = _Tables.Count
End Sub

_Tables is a List(of TableTyping)

How can I do the Include? check inside the GenerateTyping procedure?

Was it helpful?

Solution

I had to change my checkbox template to the following:

<CheckBox Tag="{Binding ID}" HorizontalAlignment="Center" VerticalAlignment="Center" IsChecked="{Binding Include, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Name="ckTblInclude" />

And change the _Tables list to populate via:

Private Sub GrabTables()
    Dim _Db As Database = Srv.Databases(DBName)
    Dim _Tbls As TableCollection = _Db.Tables
    Dim _tct As Integer = _Tbls.Count
    Dim _i As Integer = 0
    For i = 0 To _tct - 1
        If Not _Tbls(i).IsSystemObject Then
            _i += 1
            _Tables.Add(New TableTyping() With {
                        .ID = _i,
                        .Name = _Tbls(i).Name,
                        .Schema = _Tbls(i).Schema,
                        .Columns = ProcessColumns(_Tbls(i).Columns),
                        .Include = True})

        End If
    Next
    _TCount = _Tables.Count
End Sub
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top