Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF get data from datagrid something stored procedure

I have a textbox where you input a number, this works.

I have a datagrid that pulls data from an MSSQL database utilizing a stored procedure based on the number from the textbox, this works.

NOW I want to click on a row and it writes a specific value from the database to a messagebox, this does not work.

Here is code:

The textbox:

    private void TextChanged(object sender, TextChangedEventArgs e)
    {            
        int parsedValue;

        if (!int.TryParse(textBox.Text, out parsedValue))
        {
            return;
        }
        int a = Convert.ToInt32(textBox.Text);
        dataGrid1.ItemsSource = context.GetRapport3(a);
    }

The datagrid:

    private void MouseDC(object sender, MouseButtonEventArgs e)
    {
        if (dataGrid1.SelectedItem == null)
        {
            return;
        }
        var Selected = dataGrid1.SelectedItem;
        MessageBox.Show(string.Format("You have chosen: {0}", Selected));            
    }

The XAML:

 <Window x:Class="NolekRapport.MainWindow"
    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:NolekRapport"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <DataGrid x:Name="dataGrid1" HorizontalAlignment="Left" Margin="83,104,0,0" VerticalAlignment="Top" MouseDoubleClick="MouseDC" />
    <TextBox x:Name="textBox" HorizontalAlignment="Left" Height="23" Margin="83,65,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="120" TextChanged="TextChanged"/>
    <Label x:Name="label" Content="Search:" HorizontalAlignment="Left" Margin="19,65,0,0" VerticalAlignment="Top"/>
</Grid>

Also there is this:

DataClasses1DataContext context = new DataClasses1DataContext();

Problem is that when I run the program and clicks on a row I get this messagebox:

You have chosen: CompanyRapport.GetRapport3Result

GetRapport3 is the name of the stored procedure I use to find data to populate the datagrid. I would like to just get a value from a specific cell instead.

I am very new to the whole WPF(really coding as a whole) thing so please explain it simply. Thank you for your time and patience.

Edit:

Basically the output is exactly the same no matter what cell/row is clicked on. It's always "CompanyRapport.GetRapport3Result". I would like for it to be a specific attribute from a specific table in the database instead, or atleast a value from the cell clicked.

like image 318
Der Kejser Avatar asked Oct 30 '22 00:10

Der Kejser


1 Answers

Your code is almost correct. Just replace the lines below from your code

var Selected = dataGrid1.SelectedItem;
MessageBox.Show(string.Format("You have chosen: {0}", Selected));   

with the following three lines of code

var Selected = (GetRapport3Result) dataGrid1.SelectedItem;
MessageBox.Show(string.Format("You have chosen: {0}", Selected));
MessageBox.Show(string.Format("You have chosen: {0}", Selected.Id));

This should be enough for you to see the data returned by the row on the second message box. I have assumed you have a column named Id from your stored procedure. You should use an existing property of the returned data from your stored procedure.

Explanation: you are passing to the message box, as a parameter, your entire object. To be able to display your object in a message box, your object Selected was cast as a string (or Selected.ToString()), which will output the name of the class of the output of your stored procedure, which in this case is CompanyRapport.GetRapport3Result.

Passing the object as parameter will not show the data of the row, and this is why you think the correct data from the row is never shown. However, the data is there, but you passed the whole object to the message box. Just pass the object with the property specified (in the example above Id) and you should see the data.

I have cast the dataGrid1.SelectedItem as the same type of the result of your stored procedure to make all properties from the data accessible easily.

like image 105
Daniel Scain Avatar answered Nov 29 '22 14:11

Daniel Scain