lundi 9 novembre 2015

Values from DataGrid in c# WPF Application using checkbox

I have a WPF Application and have populated a DataGrid with values from a SQL query. I have added a DataGridCheckBoxColumn and button to the window. When the button is clicked I want to grab the data from the first selected row and insert into a text box or store as a variable. Please explain the answer in a way that someone who is new to c# developing would understand so I can learn rather than copy and paste.

Here is my XAML:

  <Window x:Class="WpfApplication7.Window1"
    xmlns="http://ift.tt/o66D3f"
    xmlns:x="http://ift.tt/mPTqtT"
    Title="SCID SELECT" 
    SizeToContent="WidthAndHeight"
    >
<Grid >
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <DataGrid x:Name="selectDataGrid" HorizontalAlignment="Left" VerticalAlignment="Top" Height="92" Width="288" AutoGenerateColumns="False">
        <Style  TargetType="{x:Type DataGridRow}" >

        </Style>
        <DataGrid.Columns>
            <DataGridCheckBoxColumn x:Name="dgCheckBox" Header="Select" Width="45" Binding="{Binding IsChecked}"/>
            <DataGridTextColumn Header="FIRST NAME" Width="125" Binding="{Binding FNAME}"/>
            <DataGridTextColumn Header="LAST NAME" Width="125" Binding="{Binding LNAME}"/>
        </DataGrid.Columns>


    </DataGrid>
    <Button Content="Update Fields" HorizontalAlignment="Left" Margin="193,97,0,0" VerticalAlignment="Top" Width="95" Click="Button_Click"/>

</Grid>

Here is my c# behind:

          private void Button_Click(object sender, RoutedEventArgs e)
    {
        for (int i = 0; i < selectDataGrid.Items.Count -1; i++) 
        {
            DataGridRow cell = GetRow(i);
            CheckBox tb = cell.value as CheckBox;
            System.Windows.MessageBox.Show(tb.IsChecked.ToString());
        }

    }
          public DataGridRow GetRow(int index)
    {
        DataGridRow row = (DataGridRow)selectDataGrid.ItemContainerGenerator.ContainerFromIndex(index);
        if (row == null)
        {
            selectDataGrid.UpdateLayout();
            selectDataGrid.ScrollIntoView(selectDataGrid.Items[index]);
            row = (DataGridRow)selectDataGrid.ItemContainerGenerator.ContainerFromIndex(index);
        }
        return row;
    }

If I could get help to the point of displaying the selected result to a message box I could go from there. I'm only interested in the FIRST instance of a selected row. So if multiple rows are selected I'm only interested in the first row selected.

I have read all the solutions to anything that had my keywords and I cannot find anything that fits my specific need of only wanting the first row. I'm not experienced enough to be able to make leaps and jumps or to alter code for something else to work for me. If you are going to link me to another solution please explain what I would need to change on my code, or what parts I would need from the solution. Ideally working code in a solution is what would be best.




Aucun commentaire:

Enregistrer un commentaire