从dataGrid中的选定行获取值

时间:2012-11-22 21:41:26

标签: c# wpf datagrid row

我正在将学生加载到我的数据网格中,然后想要将它们分配给一个类。我真的不在乎究竟是怎么回事,但我无法使用任何方法。

我尝试使用上下文菜单,但没有设法获取事件(之前从未使用过) 然后我在每一行中添加了一个按钮,但后来我无法弄清楚如何获得按钮所在的行。 我的最后一次尝试是获取selectedItem并在组合框中使用ToString(),但这也不起作用。

检查出来:

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

请帮助我,我即将发疯..):

1 个答案:

答案 0 :(得分:4)

您可以从网格中获取所选项目,如下所示:

<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>

事件处理程序:

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

简单的学生班:

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

如果您想使用上下文菜单,请尝试以下方法:

<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>

在代码隐藏中向MenuItem添加两个事件处理程序:

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

将学生班级更改为:

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