Hello people,
It's been a while since I started with this and couldn't achieve anything yet...
I have a dataGrid, filled from an access database.
Now, I really need to read the rows of the dataGrid, to make a selection of items.
What I have tried:
XAML code:
<Window x:Class="LabTest_V00.LoadTransformer"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:LabTest_V00"
mc:Ignorable="d"
Title="Load Transformer" WindowState="Maximized" Icon="/Images/PowerTransformer.png" WindowStartupLocation="CenterScreen" Height="715" Width="1032">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="50"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="20"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Label Content="Seleccionar tipo de transformador:" Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="5" FontSize="26"></Label>
<DataGrid Name="Grid1" Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="6" Height="500" SelectionChanged="Grid1_SelectionChanged">
</DataGrid>
<Button x:Name="BtnVolver" Content="Volver" Grid.Row="10" Grid.Column="1" Width="100" Height="35" Margin="0,0,100,0" Grid.ColumnSpan="3" FontSize="16" Click="BtnVolver_Click" />
<Button x:Name="BtnEnsayar" Content="A ensayo" Grid.Row="10" Grid.Column="6" HorizontalAlignment="Right" Width="100" Height="40" Margin="0,0,0,0" Click="BtnEnsayar_Click" />
<Label Content="Introducir ID deseada" Grid.Row="10" Grid.Column="6" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="0,0,200,0"></Label>
<TextBox x:Name="TxtBoxID" Grid.Column="6" Grid.Row="10" HorizontalAlignment="Right" VerticalAlignment="Center" TextAlignment="Center" FontSize="24" Width="70" Height="40" Margin="0,0,125,0" />
</Grid>
</Window>
This is my C# code:
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.OleDb;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using System.Windows.Forms;
using LabTest_V00;
namespace LabTest_V00
{
public partial class LoadTransformer : Window
{
public LoadTransformer()
{
InitializeComponent();
loadgrid();
}
private void loadgrid()
{
OleDbConnection con = new OleDbConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["Connection"].ToString();
con.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.CommandText = "Select * from [TipoTrafo]";
cmd.Connection = con;
OleDbDataReader rd = cmd.ExecuteReader();
Grid1.ItemsSource = rd;
}
private void BtnVolver_Click(object sender, RoutedEventArgs e)
{
this.Hide();
MainPage MainPg = new MainPage();
MainPg.Show();
}
private void BtnEnsayar_Click(object sender, RoutedEventArgs e)
{
OleDbCommand cmd = new OleDbCommand();
cmd.CommandText = "Select * from [TipoTrafo]";
string IDtrafo = cmd.CommandText;
this.Hide();
MainTest Ensayo = new MainTest();
Ensayo.Show();
}
}
}
But when I try to read an element from it, s**t happens...
I'm thinking about making a dataTable behind the code, to select data from the datatable and not from the dataGrid. But I just don't want to do that
Please help me.
Looking forward to hearing from you