One of DataGrid skills: Code controls the color of the row

zhaozj2021-02-16  51

Everyone knows that DataGrid is a high frequency of use. When writing ASP.NET code, we always want to make the user searched the specified row, let the line show different colors, although the DataGrid style has this feature, But how do we write a code implementation?

In this example, we first dynamically generate 1000 rows, then when the user selects a certain line in the DataGrid, the line will become blue. code show as below:

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 {///

/// Demostrate HOW To Have a DataGrid Server Control Be Bookmarked /// public class data data: 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 Not The page is bookmarked #Region DGProducts EventHandlers

private void DGProducts_ItemDataBound (object source, DataGridItemEventArgs e) {if (bookMark) {LiteralControl anchor = new LiteralControl (); anchor.Text = ""; itemCount ; E.Item.cells [0] .Controls.add (anchor);}}

Private Void DGProducts_ItemCommand (Object Source, DataGridCommandeventAndArgs E) {if (e.commandname == "SELECT") {E.Item.backcolor = color.blue;

IF (Bookmark) {BookmarkIndex = E.Item.itemindex; this.insertscriptblock ();}}}

#ndregion

#Region EventHandlers Private Void Page_Load (Object Sender, System.EventArgs E) {

THIS.BIND ();

}

#ndregion

#region User Definedprivate void InsertScriptBlock () {System.Text.StringBuilder jScript = new System.Text.StringBuilder (); jScript.Append ( "