62,073
社区成员
发帖
与我相关
我的任务
分享
<asp:GridView ID="gridView" runat="server" AutoGenerateColumns="False" ShowHeader="True"
Width="100%">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<input type='checkbox' id='chkAllSelect' name='chkAllSelect' onclick="CheckAll(this)"
runat="server" />
</HeaderTemplate>
<ItemTemplate>
<input type="checkbox" id="chkSelect" name="chkSelect" runat="server" value='<%# Eval("Did")%> ' />
</ItemTemplate>
<ItemStyle Width="20px" />
</asp:TemplateField>
</Columns>
</asp:GridView>
//创建列的方法
public void CreateGridView(GridView dataview, int year, int month)
{
int i;
int CurMonthDay = DateTime.DaysInMonth(year, month);
int count = dataview.Columns.Count;
for (i = 1; i < count; i++)
{
dataview.Columns.RemoveAt(1);//再次查询时删除所有日期列,重新加载日期列,重一开始是为了保存勾选列
}
for (i = 0; i < CurMonthDay; i++)
{
int day;
DateTime tm = new DateTime(year, month, i + 1);
ImageField newcoltp = new ImageField();
switch (tm.DayOfWeek)
{
case DayOfWeek.Sunday:
day = i + 1;
newcoltp.HeaderText = day.ToString() + "\n日";
break;
case DayOfWeek.Monday:
day = i + 1;
newcoltp.HeaderText = day.ToString() + "\n一";
break;
case DayOfWeek.Tuesday:
day = i + 1;
newcoltp.HeaderText = day.ToString() + "\n二";
break;
case DayOfWeek.Wednesday:
day = i + 1;
newcoltp.HeaderText = day.ToString() + "\n三";
break;
case DayOfWeek.Thursday:
day = i + 1;
newcoltp.HeaderText = day.ToString() + "\n四";
break;
case DayOfWeek.Friday:
day = i + 1;
newcoltp.HeaderText = day.ToString() + "\n五";
break;
case DayOfWeek.Saturday:
newcoltp.HeaderText = ((i + 1)).ToString() + "\n六";
break;
}
newcoltp.ItemStyle.Width = 20;
newcoltp.DataImageUrlField = string.Format("DDay{0}", i + 1);
dataview.Columns.Add(newcoltp);
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<table border="0" cellpadding="0" cellspacing="1" style="width: 100%;">
<tr>
<td style="width: 80px" align="right">
年份:
</td>
<td style="width: 50px" align="left">
<asp:DropDownList ID="drpDYear" runat="server" Width="65px" >
<asp:ListItem Value="2014">2014年</asp:ListItem>
<asp:ListItem Value="2013">2013年</asp:ListItem>
<asp:ListItem Value="2012">2012年</asp:ListItem>
<asp:ListItem Value="2011">2011年</asp:ListItem>
<asp:ListItem Value="2010">2010年</asp:ListItem>
<asp:ListItem Value="2009">2009年</asp:ListItem>
<asp:ListItem Value="2008">2008年</asp:ListItem>
</asp:DropDownList>
</td>
<td style="width: 50px" align="right">
月份:
</td>
<td style="width: 50px" align="left">
<asp:DropDownList ID="drpDMonth" runat="server" Width="50px">
<asp:ListItem Value="1">1月</asp:ListItem>
<asp:ListItem Value="2">2月</asp:ListItem>
<asp:ListItem Value="3">3月</asp:ListItem>
<asp:ListItem Value="4">4月</asp:ListItem>
<asp:ListItem Value="5">5月</asp:ListItem>
<asp:ListItem Value="6">6月</asp:ListItem>
<asp:ListItem Value="7">7月</asp:ListItem>
<asp:ListItem Value="8">8月</asp:ListItem>
<asp:ListItem Value="9">9月</asp:ListItem>
<asp:ListItem Value="10">10月</asp:ListItem>
<asp:ListItem Value="11">11月</asp:ListItem>
<asp:ListItem Value="12">12月</asp:ListItem>
</asp:DropDownList>
</td>
<td align="right">
<asp:Button ID="btnSearch" runat="server" Text="搜索" onclick="btnSearch_Click" ></asp:Button>
</td>
</tr>
</table>
<div>
<asp:GridView ID="gridView" runat="server" AllowPaging="True" AutoGenerateColumns="False"
ShowHeader="True" Width="100%" DataKeyNames="Did" ItemStyle-HorizontalAlign="Center"
EmptyDataText="没有找到符合条件的信息">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<input type='checkbox' id='chkAllSelect' name='chkAllSelect' onclick="CheckAll(this)"
runat="server" />
</HeaderTemplate>
<ItemTemplate>
<input type="checkbox" id="chkSelect" name="chkSelect" runat="server" value='<%# Eval("Did")%> ' />
</ItemTemplate>
<ItemStyle Width="20px" />
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
drpDMonth.SelectedValue = DateTime.Now.Month.ToString();
drpDYear.SelectedValue = DateTime.Now.Year.ToString();
btnSearch_Click(null, null);
}
}
/// <summary>
/// //创建列的方法
/// </summary>
/// <param name="dataview"></param>
/// <param name="year"></param>
/// <param name="month"></param>
private void CreateGridviewInit(GridView dataview, int year, int month)
{
int i;
int CurMonthDay = DateTime.DaysInMonth(year, month);
int count = dataview.Columns.Count;
for (i = 1; i < count; i++)
{
dataview.Columns.RemoveAt(1);
}
BoundField newcol1 = new BoundField
{
HeaderText = "姓名",
DataField = "DUserName"
};
newcol1.HeaderStyle.Width = 55;
dataview.Columns.Add(newcol1);
BoundField newcol2 = new BoundField
{
HeaderText = "考勤类型",
DataField = "DAttendanceTypeName"
};
newcol2.HeaderStyle.Width = 150;
dataview.Columns.Add(newcol2);
for (i = 0; i < CurMonthDay; i++)
{
int day;
DateTime tm = new DateTime(year, month, i + 1);
ImageField newcoltp = new ImageField();
switch (tm.DayOfWeek)
{
case DayOfWeek.Sunday:
day = i + 1;
newcoltp.HeaderText = day.ToString() + "\n日";
break;
case DayOfWeek.Monday:
day = i + 1;
newcoltp.HeaderText = day.ToString() + "\n一";
break;
case DayOfWeek.Tuesday:
day = i + 1;
newcoltp.HeaderText = day.ToString() + "\n二";
break;
case DayOfWeek.Wednesday:
day = i + 1;
newcoltp.HeaderText = day.ToString() + "\n三";
break;
case DayOfWeek.Thursday:
day = i + 1;
newcoltp.HeaderText = day.ToString() + "\n四";
break;
case DayOfWeek.Friday:
day = i + 1;
newcoltp.HeaderText = day.ToString() + "\n五";
break;
case DayOfWeek.Saturday:
newcoltp.HeaderText = ((i + 1)).ToString() + "\n六";
break;
}
newcoltp.ItemStyle.Width = 20;
newcoltp.DataImageUrlField = string.Format("DDay{0}", i + 1);
newcoltp.DataImageUrlFormatString = "~/Holiday/{0}.bmp";
dataview.Columns.Add(newcoltp);
}
BoundField newcol4 = new BoundField
{
HeaderText = "本月排休",
DataField = "DTotalDay"
};
newcol4.HeaderStyle.Width = 30;
dataview.Columns.Add(newcol4);
BoundField newcol5 = new BoundField
{
HeaderText = "累计未休",
DataField = "DNoRestDays"
};
newcol5.HeaderStyle.Width = 30;
dataview.Columns.Add(newcol5);
BoundField newcol6 = new BoundField
{
HeaderText = "",
};
dataview.Columns.Add(newcol6);
}
/// <summary>
/// Grid数据绑定
/// </summary>
/// <param name="year"></param>
/// <param name="month"></param>
private void BindGird(int year, int month)
{
CreateGridviewInit(gridView, year, month);//?
string sql = @" select Did, DUserName, DYear, DMonth, DTotalDay,DNoRestDays,DAttendanceTypeName,
DDay1, DDay2, DDay3, DDay4, DDay5, DDay6, DDay7, DDay8, DDay9, DDay10,
DDay11, DDay12, DDay13, DDay14, DDay15, DDay16, DDay17, DDay18, DDay19, DDay20,
DDay21, DDay22, DDay23, DDay24, DDay25, DDay26, DDay27, DDay28, DDay29, DDay30, DDay31
from TRestInfo
where DYear={0} and DMonth={1}";
sql = string.Format(sql, year, month);
DataTable dt = DbHelperSQL.Query(sql);//查询出数据集合。
gridView.DataSource = dt;
gridView.DataBind();
}
/// <summary>
/// 查询按钮事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnSearch_Click(object sender, EventArgs e)
{
int year = Convert.ToInt32(drpDYear.SelectedValue);
int month = Convert.ToInt32(drpDMonth.SelectedValue);
BindGird(year, month);
}
}
}