c#数据绑定之将datatabel的data添加listView

时间:2022-01-02 03:02:33 

c#数据绑定之将datatabel的data添加listView

功能:通过响应UI Textbox 的值向ListView 绑定新添加的纪录。

UI XAML 代码


<Grid Margin="5">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition  Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <TextBlock  Grid.Row="0" Grid.Column="0" Text="商品名称" HorizontalAlignment="Right" VerticalAlignment="Center" />
            <TextBox Grid.Row="0" Grid.Column="1" Margin="2" VerticalAlignment="Center" Text="{Binding Path=ComName}" Name="tbkName"/>
            <TextBlock  Grid.Row="1" Grid.Column="0" Text="商品进货单价" HorizontalAlignment="Right" VerticalAlignment="Center" />
            <TextBox Grid.Row="1" Grid.Column="1" Margin="2" VerticalAlignment="Center" Text="{Binding Path=ComInPrice}" Name="tbkInPrice"/>
            <TextBlock  Grid.Row="2" Grid.Column="0" Text="商品零售价格" HorizontalAlignment="Right" VerticalAlignment="Center" />
            <TextBox Grid.Row="2" Grid.Column="1" Margin="2" VerticalAlignment="Center" Text="{Binding Path=ComOutPrice}" Name="tbkOutPrice"/>
            <TextBlock  Grid.Row="3" Grid.Column="0" Text="进货数量" HorizontalAlignment="Right" VerticalAlignment="Center" />
            <TextBox Grid.Row="3" Grid.Column="1" Margin="2" VerticalAlignment="Center" Text="{Binding Path=ComOutPrice}" Name="tbkComCount"/>

            <Button Grid.Row="4" Grid.Column="1" Content="添加新纪录" Margin="2" Padding="2" Name="btnAdd" Click="btnAdd_Click" />

            <TextBlock Grid.Row="5" Grid.ColumnSpan="2"  HorizontalAlignment="Stretch"  VerticalAlignment="Center"  Background="Black" />

            <ListView Name="lstDisplay" ItemsSource="{Binding}" Grid.Row="6" Grid.ColumnSpan="2">
                <ListView.View>
                    <GridView>
                        <GridView.Columns>
                            <GridViewColumn  Header="AutoID" DisplayMemberBinding="{Binding AutoID}"/>
                            <GridViewColumn Header="商品名称" DisplayMemberBinding="{Binding ComName}"/>
                            <GridViewColumn Header="进货价格" DisplayMemberBinding="{Binding Path=ComInPrice,StringFormat={}{0:c}}"/>
                            <GridViewColumn Header="零售价格" DisplayMemberBinding="{Binding Path=ComOutPrice,StringFormat={}{0:c}}"/>
                            <GridViewColumn Header="进货数量" DisplayMemberBinding="{Binding ComCount}"/>
                        </GridView.Columns>
                    </GridView>
                </ListView.View>
            </ListView>
        </Grid>

c#后代码


DataTable Commodity = newTable();

        public MainWindow()
        {
            InitializeComponent();
            //DataTable Commodity = newTable();

        }

      

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {

            addNewMember(Commodity);
            lstDisplay.DataContext = Commodity;

        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            Button b = sender as Button;
            Int32 AutoID = Convert.ToInt32(b.CommandParameter);
            delMember(Commodity, AutoID);
            lstDisplay.DataContext = Commodity;
            lstDisplay.Items.Refresh();

        }

        private DataTable delMember(DataTable Table,Int32 laction)
        {
            //Deleting  a existed row in datatable Commodity;
            DataRow delRow = Table.Rows.Find(laction);
            Table.Rows.Remove(delRow);
            return Table;
        }

        private  DataTable addNewMember(DataTable Table)
        {
           

            //inserting a new row in datatable Commodity;

            DataRow newRow = Table.NewRow();
            newRow["ComName"] = tbkName.Text;
            newRow["ComInPrice"] = tbkInPrice.Text;
            newRow["ComOutPrice"] = tbkOutPrice.Text;
            newRow["ComCount"] = tbkComCount.Text;
            Table.Rows.Add(newRow);
            return Table;
        }

 

        private static DataTable newTable()
        {
            //Create a new table named Commodity
            DataTable commodity = new DataTable("Commodity");


            //instert new columns
            DataColumn auto = new DataColumn("AutoID", typeof(Int32));
            //setting the column auto increment
            auto.AutoIncrement = true;
            commodity.Columns.Add(auto);
            commodity.Columns.Add("ComName", typeof(string));
            commodity.Columns.Add("ComInPrice", typeof(double));
            commodity.Columns.Add("ComOutPrice", typeof(double));
            commodity.Columns.Add("ComCount", typeof(System.Int32));
            commodity.PrimaryKey = new DataColumn[] { commodity.Columns[0] };
            return commodity;
        }

标签:c#,数据绑定,datatabel,listView
0
投稿

猜你喜欢

  • 原生Java操作兔子队列RabbitMQ

    2022-03-12 21:27:25
  • Java聊天室之使用Socket实现通信功能

    2022-03-08 09:46:27
  • 游戏开发之随机概率的选择算法

    2022-08-26 13:21:09
  • Mybatis plus 配置多数据源的实现示例

    2023-10-04 15:57:33
  • Android圆形头像拍照后“无法加载此图片”的问题解决方法(适配Android7.0)

    2023-11-18 23:26:56
  • 深入浅析SpringBoot中的自动装配

    2021-08-21 17:45:11
  • Java Socket 编程详解

    2022-06-13 12:06:57
  • Mybatis Trim标签用法简单介绍

    2021-10-11 03:04:00
  • Java mongodb连接配置实践

    2022-05-02 11:50:56
  • android事件总线EventBus3.0使用方法详解

    2022-02-17 16:30:41
  • java对象序列化与反序列化的默认格式和json格式使用示例

    2021-12-07 18:22:23
  • C#中抽象类与接口的区别详解

    2023-08-12 22:33:01
  • android 大图片拖拽并缩放实现原理

    2022-11-10 05:59:55
  • spring获取bean的源码解析

    2023-10-11 22:15:43
  • 宝塔面板配置及部署javaweb教程(全网最全)

    2023-11-10 15:26:27
  • windows下C#定时管理器框架Task.MainForm详解

    2021-06-06 13:59:06
  • Java 函数式编程梳理

    2023-01-17 04:16:34
  • 使用注解@Validated效验VO参数是否合规

    2023-10-27 20:13:01
  • Java Selenium实现多窗口切换的示例代码

    2022-01-22 22:48:08
  • 详解Spring Data JPA使用@Query注解(Using @Query)

    2023-11-29 14:49:34
  • asp之家 软件编程 m.aspxhome.com