Solved

WPF datagrid with radiobuttons added.  How do I get which has been selected?

Posted on 2013-12-16
1
1,550 Views
Last Modified: 2013-12-17
I'm new to wpf.  I'm using vs 2010 and the code behind is vb.net.  I've created a datagrid that gets a description from a SQL Server table and populates the description to a row with 3 radiobuttons. Each row has a unique groupname for the three radiobutons.  The user can then read the description and select the appropriate button.  

My question is how to do I get which radiobutton was selected in each row using  the code behind?

I set the datacontext of the datagrid to a table and the description field  populates correctly and you can only select one radiobutton in the group but I'm not sure how to determine which was selected in each row.

My xaml is below.


                    <DataGrid Width="Auto" VerticalAlignment="Top" Height="Auto" ItemsSource="{Binding}" AutoGenerateColumns="False" CanUserAddRows="False" Name="dgrd1">
                    <DataGrid.Columns>
                        <DataGridTemplateColumn Header="Electrical Safety/Lockout/Tagout(LOTO)" Width="Auto" IsReadOnly="True">
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <TextBox Name="Description1" Text="{Binding Path=DETAIL_DESCRIPTION, Mode=OneWay}" Height="75" Width="250" IsReadOnly="True" TextWrapping="Wrap"></TextBox>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>
                        <DataGridTemplateColumn Header="PASS" Width="Auto" IsReadOnly="True">
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <RadioButton Name="rdbPass1" Content="PASS" Height="Auto" Width="75" GroupName="{Binding Path=GROUP_ID}"></RadioButton>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>
                        <DataGridTemplateColumn Header="FAIL" Width="Auto" IsReadOnly="True">
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                        <RadioButton Name="rdbFail1" Content="FAIL" Height="Auto" Width="75" GroupName="{Binding Path=GROUP_ID}"></RadioButton>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>
                        <DataGridTemplateColumn Header="NA" Width="Auto" IsReadOnly="True">
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                        <RadioButton Name="rdbNA1" Content="NA" Height="Auto" Width="75" GroupName="{Binding Path=GROUP_ID}"></RadioButton>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>
                        <DataGridTemplateColumn Header="Action Require" Width="Auto" IsReadOnly="True">
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                        <TextBox Name="ActionRequired1" Text="" Height="75" Width="150" IsReadOnly="False" TextWrapping="Wrap" AcceptsReturn="True"></TextBox>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>
                        <DataGridTemplateColumn Header="Person Responsible" Width="Auto" IsReadOnly="True">
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                        <TextBox Name="PersonResponsible1" Text="" Height="75" Width="150" IsReadOnly="False" TextWrapping="Wrap" AcceptsReturn="True"></TextBox>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>
                    </DataGrid.Columns>
                </DataGrid>



I've tried:

 For i As Integer = 0 To dgrd1.Items.Count - 1
            For j As Integer = 0 To dgrd1.Columns.Count - 1
                strItem = dgrd1.Items(i).ToString
                Dim s As String = TryCast(dgrd1.Items(i), DataRowView).Row.ItemArray(j).ToString()
            Next
        Next
i can see the fields from my data table, do I need to add columns in my datatable for the radiobuttons?
0
Comment
Question by:bkrbbnsEE
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 

Accepted Solution

by:
bkrbbnsEE earned 0 total points
ID: 39723784
After googling I found a solution that works in case anyone else is interested:


For i As Integer = 0 To dataGrid0.Items.Count - 1
            strValues(0) = CStr(i + lastRowCount)
            For j As Integer = 0 To dataGrid0.Columns.Count - 1
                Dim cell As DataGridCell = GetCell(i, j)

                If j = 1 Or j = 2 Or j = 3 Then
                    Dim myrdButton As RadioButton = GetVisualChildx(Of RadioButton)(cell)
                    If myrdButton.IsChecked Then
                        strValues(j) = "YES"
                    Else
                        strValues(j) = "No"
                    End If
                ElseIf j = 4 Or j = 5 Then
                    Dim mytxtBox As TextBox = GetVisualChildx(Of TextBox)(cell)
                    strValues(j) = mytxtBox.Text
                End If
            Next
            dtValues.Rows.Add(strValues)
        Next

'-----------------------------------------------------------------------------------------------------
 Public Function GetCell(row As Integer, column As Integer) As DataGridCell


        Dim rowContainer As DataGridRow = GetRow(row)

        If rowContainer IsNot Nothing Then


            Dim presenter As DataGridCellsPresenter = GetVisualChildx(Of DataGridCellsPresenter)(rowContainer)

            If presenter Is Nothing Then


                dgrd1.ScrollIntoView(rowContainer, dgrd1.Columns(column))


                presenter = GetVisualChildx(Of DataGridCellsPresenter)(rowContainer)
            End If

            Dim cell As DataGridCell = DirectCast(presenter.ItemContainerGenerator.ContainerFromIndex(column), DataGridCell)


            Return cell
        End If

        Return Nothing

    End Function

'-----------------------------------------------------------------------------------------------------
Public Function GetRow(index As Integer) As DataGridRow


        Dim row As DataGridRow = DirectCast(dgrd1.ItemContainerGenerator.ContainerFromIndex(index), DataGridRow)

        If row Is Nothing Then


            dgrd1.UpdateLayout()

            dgrd1.ScrollIntoView(dgrd1.Items(index))


            row = DirectCast(dgrd1.ItemContainerGenerator.ContainerFromIndex(index), DataGridRow)
        End If

        Return row

    End Function

'-----------------------------------------------------------------------------------------------------
   
 Public Shared Function GetVisualChildx(Of T As Visual)(parent As Visual) As T


        Dim child As T = Nothing

        Dim numVisuals As Integer = VisualTreeHelper.GetChildrenCount(parent)

        For i As Integer = 0 To numVisuals - 1


            Dim v As Visual = DirectCast(VisualTreeHelper.GetChild(parent, i), Visual)

            child = TryCast(v, T)

            If child Is Nothing Then




                child = GetVisualChildx(Of T)(v)
            End If

            If child IsNot Nothing Then



                Exit For

            End If
        Next

        Return child

    End Function
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question