datagrid技巧之一:代码控制选中行的颜色

大家都知道datagrid是一个使用频率很高的控件,在编写ASP.NET代码的时候,我们总希望能让用户选中指定的行以后,让那一行用不同的颜色显示,虽然datagrid样式也有这个功能,但是我们如何编写代码实现呢?

在本例子中,我们首先动态产生1000行,然后当用户选中datagrid中的某一行的时候,那一行就会变为蓝色。代码如下:

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;

namespace WebApplication_rd
{
///

1<summary>   
2/// Demostrates how to have a datagrid server control be bookmarked   
3/// </summary>

public class datagrid : System.Web.UI.Page
{

protected System.Web.UI.WebControls.DataGrid DGProducts;
int bookmarkIndex = 0; // The index of the row that should be scrolled to
int itemCount = 0; // Counter for the amount of items on the page
bool bookMark = true; // Controls whether or not the page is bookmarked

#region DGProducts EventHandlers

private void DGProducts_ItemDataBound(object source, DataGridItemEventArgs e) {

if (bookMark) {

LiteralControl anchor = new LiteralControl();
anchor.Text = "

  1<a "\"="" +="" itemcount.tostring()="" name='\""'>";   
  2itemCount ++;   
  3e.Item.Cells[0].Controls.Add(anchor);   
  4  
  5}   
  6  
  7} 
  8
  9private void DGProducts_ItemCommand(object source, DataGridCommandEventArgs e) {   
 10  
 11if (e.CommandName == "Select") {   
 12  
 13e.Item.BackColor = Color.Blue; 
 14
 15if (bookMark) {   
 16bookmarkIndex = e.Item.ItemIndex;   
 17this.InsertScriptBlock();   
 18}   
 19}   
 20  
 21} 
 22
 23  
 24#endregion 
 25
 26#region EventHandlers   
 27private void Page_Load(object sender, System.EventArgs e)   
 28{ 
 29
 30this.Bind(); 
 31
 32} 
 33
 34#endregion 
 35
 36#region User Defined 
 37
 38private void InsertScriptBlock() {   
 39  
 40System.Text.StringBuilder jScript = new System.Text.StringBuilder();   
 41jScript.Append("<script language='\"JavaScript\"'>");   
 42jScript.Append("location.href=\"#");   
 43jScript.Append(this.bookmarkIndex.ToString());   
 44jScript.Append("\";");   
 45jScript.Append("</script>"); 
 46
 47this.RegisterClientScriptBlock("Bookmark", jScript.ToString());   
 48  
 49} 
 50
 51  
 52private void Bind()   
 53{   
 54  
 55DGProducts.DataSource = this.CreateDataSource(1000);   
 56DGProducts.DataBind();   
 57  
 58} 
 59
 60  
 61private DataTable CreateDataSource(int count) {   
 62  
 63DataTable table = new DataTable();   
 64DataColumn column = null;   
 65DataRow row = null; 
 66
 67// Create 5 columns   
 68for (int iCol = 0; iCol &lt; 5; iCol++) {   
 69  
 70column = new DataColumn("Column: " + iCol.ToString(), typeof(string));   
 71table.Columns.Add(column); 
 72
 73} 
 74
 75//Create Rows based on count variable   
 76for (int iRows = 0; iRows &lt; count; iRows ++) {   
 77  
 78row = table.NewRow();   
 79  
 80for (int iCol = 0; iCol &lt; 5; iCol ++) {   
 81  
 82row[iCol] = "Value: " + iCol.ToString();   
 83  
 84  
 85} 
 86
 87table.Rows.Add(row);   
 88  
 89}   
 90  
 91return table;   
 92  
 93} 
 94
 95  
 96#endregion 
 97
 98#region Web Form Designer generated code   
 99override protected void OnInit(EventArgs e)   
100{   
101InitializeComponent();   
102base.OnInit(e);   
103}   
104  
105private void InitializeComponent()   
106{   
107this.DGProducts.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DGProducts_ItemCommand);   
108this.DGProducts.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DGProducts_ItemDataBound);   
109this.Load += new System.EventHandler(this.Page_Load); 
110
111}   
112#endregion   
113} 
114
115}</a>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus