WPF gets data from datagrid something stored procedure

I have a text box in which you enter a number, this works.

I have a datagrid that retrieves data from an MSSQL database using a stored procedure based on a number from a text field. It works.

NOW I want to click on a line and write a specific value from the database to the message box, this will not work.

Here is the code:

Text field:

    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);
    }

Data:

    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));            
    }

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 the following:

DataClasses1DataContext context = new DataClasses1DataContext();

The problem is that when I run the program and click on the line, I get this message:

You have chosen: CompanyRapport.GetRapport3Result

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

WPF ( ), , , . .

:

, , , / . "CompanyRapport.GetRapport3Result". , , , .

+4
1

.

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

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

, . , Id . .

: , , . , ( Selected.ToString()), , CompanyRapport.GetRapport3Result.

, , . , . ( Id), .

dataGrid1.SelectedItem , .

+2

Source: https://habr.com/ru/post/1687389/


All Articles