博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[置顶] WPF数据修改demo
阅读量:6383 次
发布时间:2019-06-23

本文共 3824 字,大约阅读时间需要 12 分钟。

今天晚上研究了下wpf,现在把代码贴出来供大家学习参考

sql语句:

create table userinfos

(

 ContactID  int primary key identity(1,1),

FirstName varchar(100),
LastName varchar(100),
EmailAddress varchar(100)
)
go
insert into  userinfos values('wanglei','aa','72121222@qq.com');
insert into  userinfos values('qss','bb','23432354@qq.com');
insert into  userinfos values('gdgg','cc','rgter@qq.com');
insert into  userinfos values('cvvd','dd','5446567576@qq.com');
select ContactID,FirstName,LastName,EmailAddress from userinfos where ContactID<=100

MainWindow.xaml前台代码

 

<Window x:Class="WpfApplication1.MainWindow"

        xmlns=""
        xmlns:x="" Closed="Window_Closed"
        Title="MainWindow" Height="350" Width="525">
    <Grid>

        <Grid.RowDefinitions>

            <RowDefinition Height="*" />

            <RowDefinition Height="Auto" />

            <RowDefinition Height="22" />

        </Grid.RowDefinitions>

        <ListView Name="listView1" MinWidth="280" >

            <ListView.View>

                <GridView x:Name="gridView1">

                    <GridViewColumn Header="ContactID" DisplayMemberBinding="{Binding Path=ContactID}"></GridViewColumn>

                    <GridViewColumn Header="FirstName" DisplayMemberBinding="{Binding Path=FirstName}"></GridViewColumn>

                    <GridViewColumn Header="LastName" DisplayMemberBinding="{Binding Path=LastName}"></GridViewColumn>

                    <GridViewColumn Header="EmailAddress" DisplayMemberBinding="{Binding Path=EmailAddress}"></GridViewColumn>

</GridView>

            </ListView.View>

        </ListView>

 

 

        <WrapPanel Grid.Row="1" Orientation="Horizontal"  DataContext="{Binding ElementName=listView1,Path=SelectedItem}">

            <StackPanel Orientation="Horizontal" Margin="5,2,5,2">

              <TextBlock Name="textBlock_ContactID" Text="ContactID:" />

  <TextBox Name="textBox_ContactID" MinWidth="100" Text="{Binding ContactID}" />

    </StackPanel>

 

            <StackPanel Orientation="Horizontal" Margin="5,2,5,2">

                <TextBlock Name="textBlock_FirstName" Text="FirstName:" />

 <TextBox Name="textBox_FirstName" MinWidth="100" Text="{Binding FirstName,UpdateSourceTrigger=PropertyChanged }" />

 

 

            </StackPanel>

            <StackPanel Orientation="Horizontal" Margin="5,2,5,2">

                <TextBlock Name="textBlock_LastName" Text="LastName:" />

 

                <TextBox Name="textBox_LastName" MinWidth="100" Text="{Binding LastName}" />

 

            </StackPanel>

            <StackPanel Orientation="Horizontal" Margin="5,2,5,2">

                <TextBlock Name="textBlock_EmailAddress" Text="EmailAddress:" />

 

                    <TextBox Name="textBox_EmailAddress" MinWidth="100" Text="{Binding EmailAddress}" />

            </StackPanel>

        </WrapPanel>

 

        <Button Grid.Row="2" HorizontalAlignment="Right" Click="button1_Click" Name="button1">Refresh</Button>

    </Grid>

</Window>

 

后台代码

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.SqlClient;
using System.Data;
namespace WpfApplication1
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            getData();

        }
        SqlDataAdapter sda;
        DataTable dt;
      //关闭窗体
        private void Window_Closed(object sender, EventArgs e)
        {

            sda.Update(dt);

        }

 

       

        void getData()

        {
          

          

    

            SqlConnectionStringBuilder connbuilder = new SqlConnectionStringBuilder();

            connbuilder.DataSource = ".";

            connbuilder.IntegratedSecurity = true;

            connbuilder.InitialCatalog = "wl";

 

           

     SqlConnection conn = new SqlConnection(connbuilder.ConnectionString);

            sda = new SqlDataAdapter("select * from userinfos;", conn);

            SqlCommandBuilder commbuilder = new SqlCommandBuilder(sda);

            sda.UpdateCommand = commbuilder.GetUpdateCommand();

            dt = new DataTable();

            sda.AcceptChangesDuringUpdate = true;

            sda.Fill(dt);

 

            listView1.ItemsSource = dt.DefaultView;

        }

       //更新

        private void button1_Click(object sender, RoutedEventArgs e)
        {
     

            sda.Update(dt);

            getData();

        }

    }
}

 

 

 

 

转载地址:http://uawha.baihongyu.com/

你可能感兴趣的文章
(随笔)js获取当前时间并格格式化当前日期 获取date天后的日期(2014年11月27日 16:31:49)...
查看>>
Gradle升仙路之Groovy(1)
查看>>
项目思考:需求讲解可以重复一次
查看>>
ubuntu "sudo: add-apt-repository: command not fou"
查看>>
医药吧开放平台的替代者-天狗云平台
查看>>
盲注代码
查看>>
redis监控状态connect命令
查看>>
斐波那契数列求青蛙跳台阶问题
查看>>
java控制语句
查看>>
2014-11-24~11-28 工作备忘
查看>>
不需要创建临时变量,交换两个数的值。
查看>>
Nginx常用配置实例
查看>>
备份oracle数据库
查看>>
vimgdb的安装过程
查看>>
【初級篇】华为三层交换的配置
查看>>
【Linux--中级篇】服务器监控Cacti
查看>>
hibernate4 spring4 spring代理事务配置 Error creating bea
查看>>
『ExtJS』01 004. ExtJS 4 确定函数的作用域
查看>>
yii2开发总结(二)
查看>>
selinux
查看>>