注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

天涯倦客的博客

祝福你朋友永远快乐!

 
 
 

日志

 
 

关于Silverlight4 dataGrid的使用  

2011-09-16 14:52:11|  分类: Silverlight |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
Silverlight4 dataGrid的使用

关于Silverlight4 dataGrid的使用 - 海里的贝壳 - apple的博客下面讲述的是dataGrid的内容有:

1.行右键菜单 2.行数的按钮操作(这里以删除为例)3.分页,这里使用datapager分页控件进行分页,5.采用的数据库连接方式是用wcf异步调用

一: 前台:

<navigation:Page xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" x:Class="SLDome.ListUser"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
d:DesignWidth="800" d:DesignHeight="600"
Title="ListUser Page">

<Grid x:Name="DContent" Height="500" VerticalAlignment="Bottom">
<Grid.RowDefinitions>
<RowDefinition Height="0.809*"/>
<RowDefinition Height="0.191*"/>
</Grid.RowDefinitions>

<sdk:DataPager x:Name="dpage" PageSize="5" HorizontalAlignment="Center" Margin="-190,0,0,0" VerticalAlignment="Top" DisplayMode="FirstLastPreviousNextNumeric" Grid.Row="1"/>

<sdk:DataGrid x:Name="grd_lisuser" Width="800" AutoGenerateColumns="False" Margin="3" HorizontalAlignment="Center" Grid.Row="0"
Grid.Column="0"

VerticalAlignment="Center" RowHeaderStyle="{Binding}">


<sdk:DataGrid.Columns>

<sdk:DataGridTemplateColumn Header="选择" Width="80" >
<sdk:DataGridTemplateColumn.HeaderStyle>
<Style x:Name="chk_style" TargetType="sdk:DataGridColumnHeader">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<CheckBox HorizontalAlignment="Left" x:Name="chkAll" Content="全选" Click="chkAll_Click" Loaded="chkAll_Loaded" />
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</sdk:DataGridTemplateColumn.HeaderStyle>
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox x:Name="chk_chird"></CheckBox>

</DataTemplate>

</sdk:DataGridTemplateColumn.CellTemplate>

</sdk:DataGridTemplateColumn>

<sdk:DataGridTextColumn Width="100" Header="用户代码" Binding="{Binding usercode,Mode=OneWay}"></sdk:DataGridTextColumn>
<sdk:DataGridTextColumn Width="100" Header="登陆名称" Binding="{Binding longid,Mode=OneWay}"></sdk:DataGridTextColumn>

<sdk:DataGridTextColumn Width="100" Header="用户名称" Binding="{Binding userName,Mode=OneWay}"></sdk:DataGridTextColumn>
<sdk:DataGridTextColumn Width="100" Header="地址" Binding="{Binding userAddress ,Mode=OneWay}"></sdk:DataGridTextColumn>
<sdk:DataGridTemplateColumn Header="操作" Width="110" >
<sdk:DataGridTemplateColumn.CellTemplate >
<DataTemplate >
<Button x:Name="btn_del" Width="90" Content="删除" Click="btn_del_Click"></Button>

</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>

</sdk:DataGrid.Columns>


</sdk:DataGrid>

</Grid>


</navigation:Page>

二:后台

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;
using System.Windows.Navigation;
using SLModel;
using System.Collections.ObjectModel;
using System.Windows.Data;
using System.Windows.Controls.Data.Input;


namespace SLDome
{
public partial class ListUser : Page
{
CheckBox IscheckALl = null;//定义一个全局变量
WcfService.UserServiceClient wcfclient = new WcfService.UserServiceClient();
public ListUser()
{
InitializeComponent();
grd_lisuser.LoadingRow += new EventHandler<DataGridRowEventArgs>(grd_lisuser_LoadingRow);
GetList();
}
/// <summary>
/// 加载行
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void grd_lisuser_LoadingRow(object sender, DataGridRowEventArgs e)
{
DataGridRow grd = e.Row;
UserInfo us = (UserInfo)e.Row.DataContext;//获取上的数据源
Button btn1 = this.grd_lisuser.Columns[5].GetCellContent(e.Row).FindName("btn_del") as Button;
btn1.Tag = us;//存储用户代码
//下面为行添加右键删除
//记住添加一个引用System.Windows.Controls.Input.Toolkit.dll;
//右键添加引用
ContextMenu cm = new ContextMenu();
MenuItem mi = new MenuItem();
mi.Header = "删除 " + us.usercode;
mi.Click+=new RoutedEventHandler(mi_Click);
cm.Items.Add(mi);
ContextMenuService.SetContextMenu(grd,cm);


}
/// <summary>
/// 右键菜单项
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void mi_Click(object sender, RoutedEventArgs e)
{
///执行右键删除工作

}
public void GetList()
{

wcfclient.GetUserListCompleted += new EventHandler<WcfService.GetUserListCompletedEventArgs>(wcfclient_GetUserListCompleted);

wcfclient.GetUserListAsync("1=1");
}
// 当用户导航到此页面时执行。
protected override void OnNavigatedTo(NavigationEventArgs e)
{

}
/// <summary>
/// 获取数据源进行绑定
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void wcfclient_GetUserListCompleted(object sender, WcfService.GetUserListCompletedEventArgs e)
{ //分页核心代码
PagedCollectionView piv = new PagedCollectionView(e.Result);

dpage.Source = piv;

grd_lisuser.ItemsSource = dpage.Source;

}
/// <summary>
/// 删除
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_del_Click(object sender, RoutedEventArgs e)
{
Button btn = (Button)e.OriginalSource;
UserInfo us1 = (UserInfo)btn.Tag;


MessageBoxResult result = MessageBox.Show(string.Format("您确定要“{0}”删除吗?", us1.userName), "提示", MessageBoxButton.OKCancel);
if (result == MessageBoxResult.OK)
{
wcfclient.DeluserCompleted += new EventHandler<WcfService.DeluserCompletedEventArgs>(wcfclient_DeluserCompleted);
wcfclient.DeluserAsync(us1.usercode);
GetList();//从新加载

}


}

void wcfclient_DeluserCompleted(object sender, WcfService.DeluserCompletedEventArgs e)
{
if (e.Result > 0)
{
MessageBox.Show("删除成功!");
}
else
{
MessageBox.Show(e.Error.ToString());
}
}
/// <summary>
/// 全选按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void chkAll_Click(object sender, RoutedEventArgs e)
{
CheckBox chk1 = sender as CheckBox;


if (this.grd_lisuser.ItemsSource != null)
{

foreach (Object obj in this.grd_lisuser.ItemsSource)
{
CheckBox ck = this.grd_lisuser.Columns[0].GetCellContent(obj).FindName("chk_chird") as CheckBox;
ck.IsChecked = chk1.IsChecked;
}

}

}
/// <summary>
/// 按钮加载事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void chkAll_Loaded(object sender, RoutedEventArgs e)
{
CheckBox chk = sender as CheckBox;
if (IscheckALl == null)
IscheckALl = chk;
}

}
}

3实体类

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Runtime.Serialization;
using System.ServiceModel;


namespace SLModel
{

public class UserInfo
{

#region Model
private string _usercode;
private string _longid;
private string _username;
private string _userpowd;
private int _sex;
private DateTime? _birth;
private string _useraddress;
private string _usertel;
private DateTime? _auditingtime;
private int _state;
private string _corpcode;
private string _auditingname;
/// <summary>
/// 用户代码
/// </summary>
///

public string usercode
{
set { _usercode = value; }
get { return _usercode; }
}

/// <summary>
/// 登录ID
/// </summary>
public string longid
{
set { _longid = value; }
get { return _longid; }
}
/// <summary>
/// 用户姓名
/// </summary>
public string userName
{
set { _username = value; }
get { return _username; }
}
/// <summary>
/// 密码
/// </summary>
public string userpowd
{
set { _userpowd = value; }
get { return _userpowd; }
}
/// <summary>
/// 性别
/// </summary>
public int Sex
{
set { _sex = value; }
get { return _sex; }
}
/// <summary>
/// 出生日期
/// </summary>
public DateTime? birth
{
set { _birth = value; }
get { return _birth; }
}
/// <summary>
/// 用户联系地址
/// </summary>
public string userAddress
{
set { _useraddress = value; }
get { return _useraddress; }
}
/// <summary>
/// 电话
/// </summary>
public string usertel
{
set { _usertel = value; }
get { return _usertel; }
}
/// <summary>
/// 审核时间
/// </summary>
public DateTime? auditingtime
{
set { _auditingtime = value; }
get { return _auditingtime; }
}
/// <summary>
/// 状态
/// </summary>
public int state
{
set { _state = value; }
get { return _state; }
}
/// <summary>
/// 所属公司代码
/// </summary>
public string corpCode
{
set { _corpcode = value; }
get { return _corpcode; }
}
/// <summary>
/// 审核人
/// </summary>
public string auditingName
{
set { _auditingname = value; }
get { return _auditingname; }
}
#endregion


}
}

四:wcf:访问数据库代码

using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using SLModel;
using System.Text;
namespace SLDome.Web
{
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class UserService
{
[OperationContract]
public void DoWork()
{
// 在此处添加操作实现
return;
}

[OperationContract]
public bool Login(string loginid,string pwd)
{
string sql = "select * from UserInfo where longid=@longid and userpowd=@userpowd";
SqlParameter[] param = new SqlParameter[] {
new SqlParameter("@longid",loginid),
new SqlParameter("@userpowd",pwd)

};
SqlDataReader datareader= DBHper.GetReader(sql, CommandType.Text, param);
if (datareader.Read())
{
datareader.Close();
return true;
}
else
{
datareader.Close();
return false;
}
}
[OperationContract]
public int Deluser(string usercode)
{
string sql = "delete from UserInfo where usercode='" + usercode+"'";
return DBHper.GetExcute(sql.ToString(), CommandType.Text, null);

}
/// <summary>
/// 查询数据列表
/// </summary>
/// <returns></returns>
///
[OperationContract]
public List<UserInfo> GetUserList(string wherestr)
{
List<UserInfo> lisu = new List<UserInfo>();

StringBuilder sqlstr = new StringBuilder("select * from UserInfo ");
if (wherestr != null)
{
sqlstr.Append(" where " + wherestr);
}
DataTable tb = DBHper.GetTable(sqlstr.ToString(), CommandType.Text, null);
foreach (DataRow row in tb.Rows)
{
UserInfo us = new UserInfo();
us.auditingName = Convert.ToString(row["auditingName"]);
us.auditingtime = Convert.ToDateTime(row["auditingtime"]);
us.birth = Convert.ToDateTime(row["birth"]);
us.longid = Convert.ToString(row["longid"]);
us.Sex = Convert.ToInt32(row["Sex"]);
us.state = Convert.ToInt32(row["state"]);
us.userAddress = Convert.ToString(row["userAddress"]);
us.userpowd = Convert.ToString(row["userpowd"]);
us.usertel = Convert.ToString(row["usertel"]);
us.usercode = Convert.ToString(row["usercode"]);
us.userName = Convert.ToString(row["userName"]);
lisu.Add(us);

}
return lisu;


}


}
}

最后运行效果图

  评论这张
 
阅读(1104)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017