zoukankan      html  css  js  c++  java
  • 得到DataGrid的某一行列的值(转载)

    转载自[孟子E章]

    http://blog.csdn.net/net_lover/archive/2008/11/26/3379002.aspx

    <UserControl xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"  x:Class="SilverlightApplication8.Page"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Width="600" Height="Auto">
        <Grid x:Name="LayoutRoot" Background="White">
            <ScrollViewer VerticalScrollBarVisibility="Auto" BorderThickness="0">
                <StackPanel Margin="10,10,10,10">
                    <data:DataGrid x:Name="dataGrid1"
          Height="200" Margin="0,5,0,10"
          AutoGenerateColumns="False" LoadingRow="dataGrid1_LoadingRow">
                        <data:DataGrid.Columns>
                            <data:DataGridTextColumn  Header="序号" Width="120"  Binding="{Binding IndexName}" />
                            <data:DataGridTextColumn  Header="姓名" Width="120"  Binding="{Binding UserName}" />
                            <data:DataGridTextColumn  Header="地址" Width="120"  Binding="{Binding Address}" />
                            <data:DataGridTemplateColumn Header="功能按钮"  Width="80">
                                <data:DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <StackPanel Orientation="Horizontal">
                                            <Button Content="初始化内容" x:Name="Button1" Click="Button1_Click"  Tag=""></Button>
                                        </StackPanel>
                                    </DataTemplate>
                                </data:DataGridTemplateColumn.CellTemplate>
                            </data:DataGridTemplateColumn>
                        </data:DataGrid.Columns>
                    </data:DataGrid>
                    <TextBlock x:Name="msg"></TextBlock>
                </StackPanel>
            </ScrollViewer>

        </Grid>
    </UserControl>

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;

    namespace SilverlightApplication8
    {
        public partial class Page : UserControl
        {
            public Page()
            {
                InitializeComponent();
                dataGrid1.ItemsSource = Customer.GetSampleCustomerList();

            }

            private void dataGrid1_LoadingRow(object sender, DataGridRowEventArgs e)
            {
                Customer bindData = (Customer)e.Row.DataContext;
                Button btn = dataGrid1.Columns[3].GetCellContent(e.Row).FindName("Button1") as Button;
                btn.Content = bindData.UserName;
                btn.Tag = bindData.IndexName + "," + e.Row.GetIndex();
            }

            private void Button1_Click(object sender, RoutedEventArgs e)
            {
                Button b = sender as Button;
                string[] t = b.Tag.ToString().Split(',');
                msg.Text = "你选择的值是:" + t[0] + " 是 DataGrid 的第 " + t[1] + " 行";
            }
        }
        /// <summary>
        /// 数据对象
        /// </summary>
        public class Customer
        {
            public Int32 IndexName { get; set; }
            public String UserName { get; set; }
            public String Address { get; set; }

            public Customer(Int32 indexName, String userName, String address)
            {
                this.IndexName = indexName;
                this.UserName = userName;
                this.Address = address;
            }

            public static List<Customer> GetSampleCustomerList()
            {
                //示例数据
                List<Customer> data = new List<Customer>();
                for (int i = 0; i < 10; i++)
                {
                    data.Add(new Customer(i * i, "孟宪会之" + i.ToString(), "地址之" + i.ToString()));
                }
                return data;
            }
        }
    }

    关于作者: 王昕(QQ:475660) 在广州工作生活30余年。十多年开发经验,在Java、即时通讯、NoSQL、BPM、大数据等领域较有经验。
    目前维护的开源产品:https://gitee.com/475660
  • 相关阅读:
    怎樣在不同DB環境生成其它DB的存儲過程
    XML之sql:column用法对性能影响
    XML之sql:variable性能比较
    环回链接服务器
    动态列名数据视图实现
    一起学习win8应用1构建我们的第一个应用
    linux 限制root SSH登陆和限制su
    nginx 直接在配置文章中设置日志分割
    linux建立ssh信任关系
    linux系统meminfo详解(待补充)
  • 原文地址:https://www.cnblogs.com/starcrm/p/1351310.html
Copyright © 2011-2022 走看看