Click here to Skip to main content
15,902,189 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
<DataGrid x:Name="GrdSubject" AutoGenerateColumns="False" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" MaxWidth="600" Background="{x:Null}" Margin="10,130,10,10" SelectionChanged="GrdSubject_SelectionChanged">
            <DataGrid.BorderBrush>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="Black" Offset="0"/>
                    <GradientStop Color="#FFFFF2F2" Offset="1"/>
                </LinearGradientBrush>
            </DataGrid.BorderBrush>

            <DataGrid.Columns>
                <DataGridTextColumn x:Name="ClId"   Header="ID"  Width="100" Visibility="Visible" Binding="{Binding SubId}"/>
                <DataGridTextColumn x:Name="ClSubject" Header="Subject"  Width="250"  Binding="{Binding SubName}"/>
                <DataGridTextColumn x:Name="ClStatus" Header="Status"  Width="100"  Binding="{Binding SubStatus}"/>

            </DataGrid.Columns>

        </DataGrid>



public class Subject
   {
       public int SubId { set; get; }
       public string SubName { set; get; }
       public string SubStatus { set; get; }
   }


What I have tried:

string id = "";
string sub=";
            var sub = new Subject();
            if (GrdSubject.SelectedItem!=null)
            {
                sub = GrdSubject.SelectedItem as Subject;
                if (sub!=null)
                {
                    id = sub.SubId.ToString();
                    MessageBox.Show(id.Length.ToString()); //just to check the word results always 200 but in real is 2 or 3 
                    MessageBox.Show(sub.SubName.Length.ToString()); //just to check the word results always 200 but in real is 5 or 6 
 and this is the problem "id                      " many spaces after the word to reach 200 as length
                    
                    
                }
            }
Posted
Updated 3-Jan-19 20:20pm

1 solution

Your question is lacking in detail and your code is incomplete, so I'll guess as to what you are attempting to do.

I prefer to work with MVVM design Pattern, but here is a solution using tightly bound code behind.

C#
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        LoadData();
    }

    private ObservableCollection<Subject> Subjects { get; } = new ObservableCollection<Subject>();
    private void LoadData()
    {
        for (int i = 0; i < 10; i++)
        {
            Subjects.Add(new Subject
            {
                SubId = i,
                SubName = "Name " + i,
                SubStatus = "Status " + (10 - i)
            });
        }

        SubjectDataGrid.ItemsSource = Subjects;

    }

    private void SubjectDataGrid_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (SubjectDataGrid.SelectedItem != null)
        {
            var item = SubjectDataGrid.SelectedItem as Subject;
            Id.Text = item.SubId.ToString();
            Name.Text = item.SubName;
            Status.Text = item.SubStatus;
        }
        else
        {
            Id.Text = "";
            Name.Text = "";
            Status.Text = "";
        }
    }
}

And here is the view:
XML
<Window x:Class="DataGridSelectedItemCodeBehind.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:DataGridSelectedItemCodeBehind"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <DataGrid Name="SubjectDataGrid"
                  SelectionChanged="SubjectDataGrid_OnSelectionChanged"/>
        <Grid Column="1" VerticalAlignment="Center" Margin="20">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <Grid.Resources>
                <Style TargetType="StackPanel">
                    <Setter Property="Orientation" Value="Horizontal"/>
                </Style>
                <Style TargetType="Label">
                    <Setter Property="Margin" Value="10"/>
                    <Setter Property="Width" Value="50"/>
                </Style>
                <Style TargetType="Border">
                    <Setter Property="Padding" Value="2"/>
                    <Setter Property="Margin" Value="0 4"/>
                    <Setter Property="Width" Value="200"/>
                    <Setter Property="BorderBrush" Value="Silver"/>
                    <Setter Property="BorderThickness" Value="2"/>
                </Style>
                <Style TargetType="TextBlock">
                    <Setter Property="VerticalAlignment" Value="Center"/>
                </Style>
            </Grid.Resources>
            <StackPanel>
                <Label Content="ID:"/>
                <Border>
                    <TextBlock Name="Id"/>
                </Border>
            </StackPanel>
            <StackPanel Grid.Row="1">
                <Label Content="Name:"/>
                <Border>
                    <TextBlock Name="Name"/>
                </Border>
            </StackPanel>
            <StackPanel Grid.Row="2">
                <Label Content="Status:"/>
                <Border>
                    <TextBlock Name="Status"/>
                </Border>
            </StackPanel>
        </Grid>
    </Grid>
</Window>
 
Share this answer
 

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