Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    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
Posted
Updated 9-Feb-21 2:08am
v2
Comments
CHill60 9-Feb-21 7:40am    
Share the code you used to read an element and explain what "s**t happens" actually means
PIEBALDconsult 9-Feb-21 8:54am    
Yes, Database --> DataTable --> DataGrid , then read the DataTable, not the DataGrid.
Also bear in mind that the DataTable has a DefaultView which you might want to utilize for sorting and filtering.
Database --> DataTable , DataTable.DefaultView --> DataGrid
FGonzalezGenta 9-Feb-21 14:08pm    
Thank you very much,

I did that finally, Database --> DataTable --> DataGrid.

This is the solution I found to achieve what i needed.

In a while I'll be sharing the solution
[no name] 9-Feb-21 12:15pm    
Assigning a "data reader" to an ItemSource guarantees that "sh*t" happens ... cause they ain't compatible.
#realJSOP 18-Feb-21 14:49pm    
Do yourself a favor and learn about the MVVM pattern.

Rule of thumb - never bind directly to the data source.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900