如何在消息框中显示特定列的数据表行的选定值 [英] how to display selected value of data table row of particular column in message box

查看:62
本文介绍了如何在消息框中显示特定列的数据表行的选定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据网格有五列,例如图标,位置,名称,性别,状态.并且我已经在该数据网格上分配了上下文菜单.每当我右键单击上下文菜单时,我想在消息框中显示选定的行学生姓名..以在数据网格中增加价值,我正在使用数据表..我该怎么办..
我的代码是
window1.xaml

I have data grid with five column such as Icon,position,name,gender,status..and i have assign the context menu on thata data grid..whenever i right click on context menu i want to display in message box selected row student name..to add value in data grid i am using data table..what shoul i do..
my code is
window1.xaml

<pre lang="xml"><Window x:Class="datagridimg.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="347" Width="457" xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit">
    <Grid>
        <my:DataGrid  MouseRightButtonUp="dgData_MouseRightButtonUp" AutoGenerateColumns="False" Margin="12,12,26,105" Name="dataGrid1" >
            <my:DataGrid.ContextMenu>

                <ContextMenu>
                    <MenuItem Header="Add" Click="Ass_Game">
                    </MenuItem>
                </ContextMenu>
            </my:DataGrid.ContextMenu>
            <my:DataGrid.Columns>
                <my:DataGridTemplateColumn Header="Icon" Width="50" IsReadOnly="True" >
                    <my:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Image Source="{Binding Path=ImgPath}" Width="20" Height="20"/>
                        </DataTemplate>
                    </my:DataGridTemplateColumn.CellTemplate>
                </my:DataGridTemplateColumn>
                <my:DataGridTextColumn Header="Position"  Binding="{Binding Path=PO}"   />
                <my:DataGridTextColumn Header=" Name" Binding="{Binding Path=NA}"  />
                <my:DataGridTextColumn Header="Gender" Binding="{Binding Path=GE}"  />
                <my:DataGridTextColumn Header="Status" Binding="{Binding Path=ST}"  />
                <my:DataGridTextColumn Header="Machine" Binding="{Binding Path=MA}"  />
            </my:DataGrid.Columns>
        </my:DataGrid>
        <Image HorizontalAlignment="Left" Margin="50,0,0,39" Name="image1" Stretch="Fill" Width="49" Height="42" VerticalAlignment="Bottom" Source="/datagridimg;component/image/offline.png" />
    </Grid>
</Window


>



window1.xaml.cs


>



window1.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;

namespace datagridimg
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        DataTable dt;
        public Window1()
        {
            InitializeComponent();
           
            dt = new DataTable();
            dt.Columns.Add("PO");
            dt.Columns.Add("NA");
            dt.Columns.Add("GE");
            dt.Columns.Add("ST");
            dt.Columns.Add("MA");
            dt.Columns.Add("ImgPath", typeof(BitmapImage));

            DataRow dr = dt.NewRow();
            dr[0] = "5";
            dr[1] = "SAM";
            dr[2] = "Male";
            dr[3] = "Offline";
            dr[4] = "Online";
            dr[5] = new BitmapImage(new Uri("/datagridimg;component/image/offline.png", UriKind.RelativeOrAbsolute));
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr[0] = "3";
            dr[1] = "RAM";
            dr[2] = "Male";
            dr[3] = "Offline";
            dr[4] = "Online";
            dr[5] = new BitmapImage(new Uri("/datagridimg;component/image/offline.png", UriKind.RelativeOrAbsolute));
            dt.Rows.Add(dr);


           
            dataGrid1.ItemsSource = dt.DefaultView;
        }
        private void dgData_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
        {
            ContextMenu cxMenu = new ContextMenu();


        }
        private void Ass_Game(object sender, RoutedEventArgs e)
        {
           
           MessageBox.Show("Name of Student???????");
         
  
        }


           
        }//context menu

    }
}

推荐答案

将DataGrid绑定到DataTable时,需要将DataGrid的SelectedValuePath设置为Name列,例如

When you bind the DataGrid to the DataTable you need to set the DataGrid''s SelectedValuePath to the Name column like this

dr[5] = new BitmapImage(new Uri("/datagridimg;component/image/offline.png", UriKind.RelativeOrAbsolute));
           dt.Rows.Add(dr);
           dataGrid1.ItemsSource = dt.DefaultView;
           dataGrid1.SelectedValuePath = "NA";
       }


然后,您可以像这样从ContextMenu引用SelectedValue


Then you can just reference the SelectedValue from the ContextMenu like this

private void Ass_Game(object sender, RoutedEventArgs e)
        {
            MessageBox.Show(dataGrid1.SelectedValue.ToString());
        }



请注意,如果您的DataGrid的SelectionMode设置为Extended,则将返回第一个选定行的Name.如果SelectionMode设置为single,则显然将仅返回所选行的名称.

希望这对您有帮助



Be aware that if your DataGrid''s SelectionMode is set to Extended then this will return the Name of the first selected row. If the SelectionMode is set to single, it obviously will return only the selected row''s name.

Hope this helps


嗨.

我可以提出这个解决方案:

1.将上下文菜单移至DataGrid资源并放置相对源绑定.
2.使用DataGrid.ItemContainerStyle将上下文菜单分配给DataGridRow.
3.相应地更改Ass_Game处理程序.
4.删除dgData_MouseRightButtonUp事件处理程序

请参见下面的xaml示例:

Hi.

I can propose this solution:

1. Move context menu to the DataGrid Resources and put relativesource binding.
2. Assign context menu to the DataGridRow using DataGrid.ItemContainerStyle.
3. Change Ass_Game handler accordingly.
4. Remove dgData_MouseRightButtonUp event handler

See xaml example below:

<my:DataGrid  AutoGenerateColumns="False" Margin="12,12,26,105" Name="dataGrid1" >
        <my:DataGrid.Resources>
            <ContextMenu x:Key="contextMenu">
                <MenuItem Header="Add" Click="Ass_Game"

                          Tag="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ContextMenu}}, Path=PlacementTarget.Item}">
                </MenuItem>
            </ContextMenu>
        </my:DataGrid.Resources>
        <my:DataGrid.Columns>
            <my:DataGridTemplateColumn Header="Icon" Width="50" IsReadOnly="True" >
                <my:DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Image Source="{Binding Path=ImgPath}" Width="20" Height="20"/>
                    </DataTemplate>
                </my:DataGridTemplateColumn.CellTemplate>
            </my:DataGridTemplateColumn>
            <my:DataGridTextColumn Header="Position"  Binding="{Binding Path=PO}"   />
            <my:DataGridTextColumn Header=" Name" Binding="{Binding Path=NA}"  />
            <my:DataGridTextColumn Header="Gender" Binding="{Binding Path=GE}"  />
            <my:DataGridTextColumn Header="Status" Binding="{Binding Path=ST}"  />
            <my:DataGridTextColumn Header="Machine" Binding="{Binding Path=MA}"  />
        </my:DataGrid.Columns>
        <my:DataGrid.ItemContainerStyle>
            <Style>
                <Setter Property="my:DataGridRow.ContextMenu" Value="{StaticResource ResourceKey=contextMenu}"></Setter>
            </Style>
        </my:DataGrid.ItemContainerStyle>
    </my:DataGrid>




Ass_Game处理程序示例:




Ass_Game Handler example:

private void Ass_Game(object sender, RoutedEventArgs e)
{
   MenuItem item = sender as MenuItem;
   if (item == null)
   {
     return;
   }

   DataRowView view = item.Tag as DataRowView;
   if (view == null)
   {
     return;
   }

   MessageBox.Show(view["NA"].ToString());
}



玩得开心!



Have fun!!


这篇关于如何在消息框中显示特定列的数据表行的选定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆