I need help to finish my first little app. I'm trying to do action for checked's rows in a datagrid.
the xaml :
<DataGrid x:Name="dgConnected_Users" AutoGenerateColumns="False" IsReadOnly="True" Height="180" Background="Azure" ScrollViewer.CanContentScroll="True" FontSize="11" BorderBrush="#26BFF0" ScrollViewer.HorizontalScrollBarVisibility="Disabled" HorizontalScrollBarVisibility="Disabled" BorderThickness="2,2,2,2" HeadersVisibility="Column" CanUserAddRows="False" CanUserDeleteRows="False" CanUserResizeRows="False" RowHeight="20" RowBackground="Azure" GridLinesVisibility="Horizontal" HorizontalGridLinesBrush="DarkOrange" Margin="10,0,10,0">
<DataGrid.Columns>
<DataGridTextColumn x:Name="UID_UserName" FontFamily="Segoe UI Bold" Binding="{Binding UID_UserName}" Header="Utilisateur" Width="1*">
<DataGridTextColumn.ElementStyle>
<Style TargetType="TextBlock">
<Setter Property="VerticalAlignment" Value="Center"></Setter>
<Setter Property="Margin" Value="5,0"></Setter>
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
<DataGridTextColumn x:Name="UID_ComputerName" Binding="{Binding UID_ComputerName}" Header="Machine" Width="1*">
<DataGridTextColumn.ElementStyle>
<Style TargetType="TextBlock">
<Setter Property="VerticalAlignment" Value="Center"></Setter>
<Setter Property="Margin" Value="5,0"></Setter>
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
<DataGridTemplateColumn x:Name="UID_Chkbox" Width="24">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock TextAlignment="Center" VerticalAlignment="Center"><CheckBox x:Name="Chk_UID_IsSelected" IsChecked="{Binding IsChecked, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Checked="Chk_UID_IsSelected_Checked" Unchecked="Chk_UID_IsSelected_Unchecked" Click="Chk_UID_IsSelected_Click"/></TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.Header>
<Image Source="components/resources/images/delete-24.png" Height="12" Width="12" StretchDirection="Both" Stretch="Fill"/>
</DataGridTemplateColumn.Header>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
<StackPanel Margin="10,10">
<Button x:Name="UID_Disconnect" Width="270" Click="UID_Disconnect_Click">Déconnecter le(s) utilisateur(s)</Button>
</StackPanel>
<StackPanel Margin="10,0">
<Grid>
<Border BorderBrush="#26BFF0" BorderThickness="0,1,0,0">
<TextBlock HorizontalAlignment="Center" FontFamily="Segoe UI Light" FontSize="10" Padding="5" Foreground="#FF4C4F54">InovaGes SAS · Développé par Fabrice BERTRAND</TextBlock>
</Border>
</Grid>
</StackPanel>
The xaml.cs :
private void UID_Disconnect_Click(object sender, RoutedEventArgs e)
{
/// gestion de la déconnexion des utilisateurs au clic du bouton 'UID_Disconnect'
///
IEnumerable<CheckBox> UsersInDatabase = dgConnected_Users.Items.OfType<CheckBox>().Where(usr => usr.IsChecked == true);
foreach (CheckBox usr in UsersInDatabase)
{
MessageBox.Show("yeah"); /// do stuff here
}
}
I'm testing the code with a MessageBox for now. But nothing happen. An idea to help me to finish my project ?
In fact, I need for each row where checkbox is checked to get the UID_UserName to do a sql delete.
I have tried with DataRowView too. I can get TextColumn but nothing with checkbox.
Edit 1 :
I think a part of the problem is due that the column with checkbox is not in the dt :
private void refresh_Click(object sender, RoutedEventArgs e)
{
try
{
if (Cnx.State == System.Data.ConnectionState.Open)
{
Cnx.Close();
}
if (cmbAuthentification.Text.Equals("Windows"))
{
SQL_Connection.ConnectionString = @"Server = " + txtServerName.Text + "; Integrated Security = SSPI;";
Cnx.ConnectionString = SQL_Connection.ConnectionString;
}
else if (cmbAuthentification.Text.Equals("SQL Server"))
{
SQL_Connection.ConnectionString = @"Server = " + txtServerName.Text + "; User ID =" + txtUserID.Text + "; Password=" + txtUserPwd.Password + ";";
Cnx.ConnectionString = SQL_Connection.ConnectionString;
}
Cnx.Open();
Cmd.Connection = Cnx;
Cmd.CommandText = ("USE \"" + cmbDatabase.Text + "\";SELECT DISTINCT b.nt_username AS [UID_UserName], b.hostname AS [UID_ComputerName] FROM cbUserSession a INNER JOIN master..sysprocesses b ON a.cbSession = b.spid WHERE a.cbUserName IS NOT NULL;");
SqlDataAdapter da = new SqlDataAdapter(Cmd);
dt = new DataTable("Connected_Users");
da.Fill(dt);
/// dt.Columns.Add(new DataColumn("IsChecked", typeof(bool))); <-- maybe need to do something like that ?
dgConnected_Users.ItemsSource = dt.DefaultView;
Cnx.Close();
}
catch (Exception exception)
{
MessageBox.Show("Erreur de chargement des données pour le motif suivant : \n" + exception.Message, "OOoopssss !!!", MessageBoxButton.OK, MessageBoxImage.Error);
}
}