⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ifrowcolor.aspx.cs

📁 asp.net专家200问(含源代码解决法案
💻 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;
using System.Configuration;
namespace CommonFunction.数据绑定控件
{
	/// <summary>
	/// IfRowColor 的摘要说明。
	/// </summary>
	public class IfRowColor : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.DataGrid dgRowColor;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			//页面初试化时进行数据绑定
			if(!IsPostBack)
				DataGridDataBind();
		}

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

		}
		#endregion

		private void SetIfRowColor(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
		{
			//如果TitleOfCourtesy列的值为"Mr."则设置该行的背景色为蓝色
			if((e.Item.Cells[3].Text)=="Mr.")
				e.Item.BackColor=Color.Blue;
			//如果TitleOfCourtesy列的值为"Ms."则设置该行的背景色为红色
			if((e.Item.Cells[3].Text)=="Ms.")
				e.Item.BackColor=Color.Red;
		}

		private void DataGridDataBind()
		{
			//定义数据连接对象,其中数据库连接字符串是在Web.Config文件中定义的
			SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionSqlServer"].ToString());
			//创建数据适配器对象
			SqlDataAdapter da = new SqlDataAdapter("select LastName,FirstName,Title,TitleOfCourtesy,City from Employees",conn);
			//创建DataSet对象
			DataSet ds = new DataSet();
			try
			{
				//填充数据集
				da.Fill(ds,"testTable");
				//进行数据绑定
				dgRowColor.DataSource = ds.Tables["testTable"];
				dgRowColor.DataBind();
			}
			catch(Exception error)
			{
				Response.Write(error.ToString());
			}
		}

	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -