zoukankan      html  css  js  c++  java
  • wpf--------------datagrid全选反选 多选进行删除操作 前后台

    前台绑定
    <DataGrid.Columns>
                        <DataGridTemplateColumn >
                            <DataGridTemplateColumn.HeaderTemplate>
                                <DataTemplate>
                                    <CheckBox Click="HeaderCellLoad" Content="全选"   IsChecked="False" Unchecked="UnChoice_Checked" Checked="Choice_Checked"></CheckBox>
                                    </DataTemplate>
                            </DataGridTemplateColumn.HeaderTemplate>
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <CheckBox Click="checkbox_checked" HorizontalAlignment="Center" x:Name="Choices"  IsChecked="{Binding CHOICE, Mode=TwoWay}" VerticalAlignment="Center" />
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>  
                        </DataGridTemplateColumn>  
                        <DataGridTextColumn Header="商品条码" Binding="{Binding commodity_barcode}" Width="20*" HeaderStyle="{StaticResource ColumnHeaderStyle}"/>
                        <DataGridTextColumn Header="商品名称" Binding="{Binding commodity_name}" Width="18*" HeaderStyle="{StaticResource ColumnHeaderStyle}"/>
                        <DataGridTextColumn Header="单位" Binding="{Binding commodity_unit}" Width="10*" HeaderStyle="{StaticResource ColumnHeaderStyle}"/>
                        <DataGridTextColumn Header="规格" Binding="{Binding commodity_rule}" Width="10*" HeaderStyle="{StaticResource ColumnHeaderStyle}"/>
                        <DataGridTextColumn Header="售价" Binding="{Binding commodity_retailprice}" Width="10*" HeaderStyle="{StaticResource ColumnHeaderStyle}" />
                        <DataGridTextColumn Header="会员价" Binding="{Binding commodity_memberprice}" Width="10*" HeaderStyle="{StaticResource ColumnHeaderStyle}"/>
                     <DataGridTemplateColumn Header="操作" Width="10*">
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>  
                                    <StackPanel Orientation="Horizontal">
                                        <Button Content="修改"  Click="Update_Click" Margin="15,0,5,0" >
                                            <Button.Template>
                                                <ControlTemplate TargetType="Button">
                                                    <TextBlock TextDecorations="Underline" Name="btnInput" >  
                                                <ContentPresenter />
                                                    </TextBlock>
                                                </ControlTemplate>
                                            </Button.Template>
                                            <Button.Style>
                                                <Style TargetType="Button">
                                                    <Setter Property="Foreground" Value="Blue" />
                                                    <Style.Triggers>
                                                        <Trigger Property="IsMouseOver" Value="true">
                                                            <Setter Property="Foreground" Value="Red" />
                                                        </Trigger>
                                                    </Style.Triggers>
                                                </Style>
                                            </Button.Style>
                                        </Button>
                                        <Button Content="删除" Click="Delete_Click" Margin="15,0,5,0">
                                            <Button.Template>
                                                <ControlTemplate TargetType="Button">
                                                    <TextBlock TextDecorations="Underline" Name="btnCancel" >  
                                        <ContentPresenter />
                                                    </TextBlock>
                                                </ControlTemplate>
                                            </Button.Template>
                                            <Button.Style>
                                                <Style TargetType="Button">
                                                    <Setter Property="Foreground" Value="Red" />
                                                    <Style.Triggers>
                                                        <Trigger Property="IsMouseOver" Value="true">
                                                            <Setter Property="Foreground" Value="Red" />
                                                        </Trigger>
                                                    </Style.Triggers>
                                                </Style>
                                            </Button.Style>
                                        </Button>
                                    </StackPanel>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>
                    </DataGrid.Columns> 
    View Code

    后台事件

        /// <summary>
            /// Checked UnChecked事件
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param> 
            private void Choice_Checked(object sender, RoutedEventArgs e)
            {
                DataTable dt = ((DataView)CommodityData.ItemsSource).Table;
                foreach (DataRow row in dt.Rows)
                {
                    row["CHOICE"] = "1";
                    GetCount();
                }
            }
            private void UnChoice_Checked(object sender, RoutedEventArgs e)
            {
                DataTable dt = ((DataView)CommodityData.ItemsSource).Table;
                foreach (DataRow row in dt.Rows)
                {
                    row["CHOICE"] = "0";
                    GetCount();
                }
            }
    
            private void checkbox_checked(object sender, RoutedEventArgs e)
            {
                //首先获取DataGridTemplateColumn所在列     
                DataGridTemplateColumn tempColumn = this.CommodityData.Columns[0] as DataGridTemplateColumn;
                //然后获取DataGridTemplateColumn单元格元素
                FrameworkElement element = this.CommodityData.Columns[0].GetCellContent(this.CommodityData.Items[CommodityData.SelectedIndex]);
                if (element != null)
                {
                    //把单元格元素转换为相应的控件,再从该控件中取值
                    System.Windows.Controls.CheckBox ck = tempColumn.CellTemplate.FindName("Choices", element) as System.Windows.Controls.CheckBox; 
                    if (ck.IsChecked == true)
                    {
                        DataRowView item = CommodityData.Items[CommodityData.SelectedIndex] as DataRowView;
                        DataRow dr = item.Row;
                        dr["CHOICE"] = "1";
                        GetCount();
                    }
                    else if (ck.IsChecked == false)
                    {
                        DataRowView item = CommodityData.Items[CommodityData.SelectedIndex] as DataRowView;
                        DataRow dr = item.Row;
                        dr["CHOICE"] = "0";
                        GetCount();
                    } 
                }
            }
    View Code
  • 相关阅读:
    作业程序
    第十三周完成情况 学生选课管理系统 王杰 孙乾 蔚晓青
    各周任务
    课程设计 学生选课管理 王杰 孙乾 蔚晓青
    第六次作业
    第五次作业
    第四次笔记
    第三次作业
    第二周学习笔记
    JAVA作业
  • 原文地址:https://www.cnblogs.com/wangzhe688/p/9323772.html
Copyright © 2011-2022 走看看