• 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
  • 相关阅读:
    如何节省 1TB 图片带宽?解密极致图像压缩
    微信亿级用户异常检测框架的设计与实践
    一文带你深度解析腾讯云直播答题方案
    kafka数据迁移实践
    揭密微信跳一跳小游戏那些外挂
    从蓝光到4K,腾讯视频高码率下载背后的技术
    GridControl列自动匹配宽度
    access的逻辑类型
    NPOI Excel类
    Access sql语句创建表及字段类型
  • 原文地址:https://www.cnblogs.com/wangzhe688/p/9323772.html
Copyright © 2020-2023  润新知