Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8882453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:34:56+00:00 2026-06-14T20:34:56+00:00

I’m loading students into my datagrid and then want to assign them to a

  • 0

I’m loading students into my datagrid and then want to assign them to a class. I really don’t care how exactly, but I can’t get any method working.

I tried it with a context menu, but didn’t manage to get the event (never worked with it before)
Then I added a button in each row, but then I couldn’t figure out how to get the row in which the button is.
My last try was to get the selectedItem and use ToString() like on a combobox, but that didn’t work either.

Check this out:

    private void dataGrid4_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        DataGrid dg = new DataGrid();
        dg = (sender as DataGrid);
        MessageBox.Show(dataGrid4.Items.GetItemAt(1).ToString());
        MessageBox.Show(dataGrid4.CurrentItem.ToString() + "\n" + dataGrid4.CurrentCell.Column.ToString() + "\n" + dataGrid4.CurrentCell.Item.ToString() + "\n" + dataGrid4.SelectedItem.ToString() + "\n" + dataGrid4.SelectedValue.ToString() + "\n" + dataGrid4.SelectedIndex.ToString());
        MessageBox.Show(dg.CurrentItem.ToString() + "\n" + dg.CurrentCell.Column.ToString() + "\n" + dg.CurrentCell.Item.ToString() + "\n" + dg.SelectedItem.ToString() + "\n" + dg.SelectedValue.ToString() + "\n" + dg.SelectedIndex.ToString());
    }

Please help me out, I’m about to freak out.. ):

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T20:34:57+00:00Added an answer on June 14, 2026 at 8:34 pm

    You can get selected items from grid like this:

    <DataGrid Name="dgStudents" 
                      AutoGenerateColumns="False"
                      SelectionChanged="dgStudents_SelectionChanged">
                <DataGrid.Columns>
                    <DataGridTextColumn Header="ID" Binding="{Binding ID}" />
                    <DataGridTextColumn Header="Name" Binding="{Binding Name}" />
                    <DataGridTextColumn Header="Surname" Binding="{Binding Surname}" />
                </DataGrid.Columns>
            </DataGrid>
    

    Event handler:

    private void dgStudents_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                if (e.AddedItems != null && e.AddedItems.Count != 0)
                {
                    foreach (Student item in e.AddedItems)
                    {
                        Console.WriteLine(item.ID);
                    }
                }
            }
    

    Simple student class:

    class Student
        {
            public int ID { get; set; }
            public string Name { get; set; }
            public string Surname { get; set; }
        }
    

    If you want use context menu try this:

    <DataGrid Name="dgStudents" 
                      AutoGenerateColumns="False">
                <DataGrid.ContextMenu>
                    <ContextMenu>
                        <MenuItem Header="ClassA" Click="ClassA_Click" />
                        <MenuItem Header="ClassB" Click="ClassB_Click" />
                    </ContextMenu>
                </DataGrid.ContextMenu>
    
                <DataGrid.Columns>
                    <DataGridTextColumn Header="ID" Binding="{Binding ID}" />
                    <DataGridTextColumn Header="Name" Binding="{Binding Name}" />
                    <DataGridTextColumn Header="Surname" Binding="{Binding Surname}" />
                    <DataGridTextColumn Header="Class" Binding="{Binding ClassName}" />
                </DataGrid.Columns>
            </DataGrid>
    

    Add two event handler to MenuItem in code-behind:

    private void ClassA_Click(object sender, RoutedEventArgs e)
            {
                if (dgStudents.SelectedItems != null && dgStudents.SelectedItems.Count != 0)
                {
                    foreach (Student item in dgStudents.SelectedItems)
                    {
                        item.ClassName = "ClassA";
                    }
                    dgStudents.Items.Refresh();
                }
            }
    
            private void ClassB_Click(object sender, RoutedEventArgs e)
            {
                if (dgStudents.SelectedItems != null && dgStudents.SelectedItems.Count != 0)
                {
                    foreach (Student item in dgStudents.SelectedItems)
                    {
                        item.ClassName = "ClassB";
                    }
                    dgStudents.Items.Refresh();
                }
            }
    

    Change student class to this:

     class Student
        {
            public int ID { get; set; }
            public string Name { get; set; }
            public string Surname { get; set; }
            public string ClassName { get; set; }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I want to construct a data frame in an Rcpp function, but when I
I used javascript for loading a picture on my website depending on which small
this is what i have right now Drawing an RSS feed into the php,
I want use html5's new tag to play a wav file (currently only supported
I'm interested in microtypography issues on the web. I want a tool to fix:
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.