CSDN博客

img zhangzs8896

改变DataGrid特定单元格属性

发表于2004/9/23 10:29:00  832人阅读

calendar.aspx
[.cs] 代码:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient ;
namespace Pu1_Erp.practice
{
 /// <summary>
 /// calendar1 的摘要说明。
 /// </summary>
 public class calendar1 : System.Web.UI.Page
 {
  protected System.Web.UI.WebControls.TextBox My_TBox;
  protected System.Web.UI.WebControls.Button My_Btn;
  protected System.Web.UI.WebControls.Label My_Lab;
  protected System.Web.UI.WebControls.DataGrid MyDataGrid;
 
  private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此处放置用户代码以初始化页面
   if(!IsPostBack)
   {
    My_TBox.Text=System.DateTime.Now.ToString("yyyy/MM/dd");
    BindGrid();   
   }
  }
  private void BindGrid()
  {
 try
   {
    string Sdt=My_TBox.Text.Trim();
    practice pt=new practice();
    DataSet ds=new DataSet();
    ds=pt.calendar(Sdt);
    MyDataGrid.DataSource=ds;
    MyDataGrid.DataBind();}
   catch{
       Response.Write("<script>window.alert(/"输入有误,请检查...../");</script>");
     }  }

  #region Web 窗体设计器生成的代码
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {   
   this.My_Btn.Click += new System.EventHandler(this.My_Btn_Click);
   this.MyDataGrid.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.MyDataGrid_ItemDataBound);
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion

  private void My_Btn_Click(object sender, System.EventArgs e)
  {
    BindGrid();
  }

  //寻找一个值,改变这个值所在单元格的背景颜色,为了显眼
  private void MyDataGrid_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
   if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)
   {
    
     for(int i=1;i<e.Item.Cells.Count;i++)
     {
      if(AddAtrr(e.Item.Cells[i].Text))
      {
        e.Item.Cells[i].BackColor =System.Drawing.Color.Red;
        e.Item.Cells[i].Text  ="√"+e.Item.Cells[i].Text; 
      }
     }
    
     
   }
  }
    
       //
  private bool AddAtrr(string nowDate)
  {
   if(nowDate==My_TBox.Text.Trim())
    return true;
   else
    return false;
  }
 }
}

[html] 代码如下:

<%@ Page language="c#" Codebehind="calendar.aspx.cs" AutoEventWireup="false" Inherits="Pu1_Erp.practice.calendar1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
 <HEAD>
  <title>calendar</title>
  <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
  <meta content="C#" name="CODE_LANGUAGE">
  <meta content="JavaScript" name="vs_defaultClientScript">
  <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
 </HEAD>
 <body background="../image/MainEarth.gif" MS_POSITIONING="GridLayout">
  <form id="Form1" method="post" runat="server">
   <FONT face="宋体"></FONT><FONT face="宋体"></FONT><FONT face="宋体"></FONT>
   <asp:textbox id="My_TBox" style="Z-INDEX: 101; LEFT: 96px; POSITION: absolute; TOP: 64px" runat="server"
    Width="72px"></asp:textbox><asp:button id="My_Btn" style="Z-INDEX: 102; LEFT: 184px; POSITION: absolute; TOP: 64px" runat="server"
    Text="查询"></asp:button><asp:label id="My_Lab" style="Z-INDEX: 103; LEFT: 48px; POSITION: absolute; TOP: 64px" runat="server">日期</asp:label></form>
  <FONT face="宋体"></FONT>
  <asp:datagrid id="MyDataGrid" style="Z-INDEX: 104; LEFT: 48px; POSITION: absolute; TOP: 96px"
   runat="server" AlternatingItemStyle-BackColor="#eeeeee" HeaderStyle-BackColor="#aaaadd" Font-Size="8pt"
   Font-Name="Verdana" CellPadding="3" BorderWidth="1px" BorderColor="Black" PagerStyle-HorizontalAlign="Right"
   PagerStyle-Mode="NumericPages" PageSize="20" Font-Names="Verdana" AutoGenerateColumns="False">
   <AlternatingItemStyle BackColor="#EEEEEE"></AlternatingItemStyle>
   <HeaderStyle BackColor="#AAAADD"></HeaderStyle>
   <Columns>
    <asp:BoundColumn DataField="月份" HeaderText="月份"></asp:BoundColumn>
    <asp:BoundColumn DataField="周/年" HeaderText="周/年"></asp:BoundColumn>
    <asp:BoundColumn DataField="周/月" HeaderText="周/月"></asp:BoundColumn>
    <asp:BoundColumn DataField="星期一" HeaderText="星期一"></asp:BoundColumn>
    <asp:BoundColumn DataField="星期二" HeaderText="星期二"></asp:BoundColumn>
    <asp:BoundColumn DataField="星期三" HeaderText="星期三"></asp:BoundColumn>
    <asp:BoundColumn DataField="星期四" HeaderText="星期四"></asp:BoundColumn>
    <asp:BoundColumn DataField="星期五" HeaderText="星期五"></asp:BoundColumn>
    <asp:BoundColumn DataField="星期六" HeaderText="星期六"></asp:BoundColumn>
    <asp:BoundColumn DataField="星期日" HeaderText="星期日"></asp:BoundColumn>
   </Columns>
   <PagerStyle HorizontalAlign="Right" Mode="NumericPages"></PagerStyle>
  </asp:datagrid>
  <div>
   <div style="BACKGROUND-COLOR: transparent" align="center">
    <marquee onmouseover="this.stop()" style="WIDTH: 482px; HEIGHT: 40px" onmouseout="this.start()"
     scrollAmount="3" scrollDelay="20" direction="left">
     <div><font style="COLOR: #006600; FONT-STYLE: italic" face="隶书" size="4">按格式输入日期,将会查询出上月、本月、下月的日历部分!字段解释:'周/年'指的是本周是本年度的第几周,同理
       '周/月'指的是本周是本月份的第几周!</font></div>
     <DIV></DIV>
     <DIV></DIV>
     <DIV></DIV>
     <DIV></DIV>
     <DIV></DIV>
     <DIV></DIV>
     <DIV></DIV>
     <DIV></DIV>
    </marquee>
   </div>
  </div>
 </body>
</HTML>

---效果:



作者: 小二

0 0

相关博文

我的热门文章

img
取 消
img