这是要实现的功能:
第一步:拖入GridView控件 并且完成查询所有数据的方法 通过this.GridView1.DataSource 获取集合数据 GridView1.DataBind() 绑定数据
第二步:实现全选功能
1. 页面代码:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
<
asp:TemplateField
HeaderText
="全选"
>
< HeaderTemplate >
< input type ="checkbox" id ="CheckBox1" name ="CheckBox1" onclick ="GetAllCheckBox(this)" />
全选
</ HeaderTemplate >
< ItemTemplate >
< input type ="checkbox" name ="CheckBox2" />
</ ItemTemplate >
< ItemStyle HorizontalAlign ="Center" Width ="100px" />
</ asp:TemplateField >
< HeaderTemplate >
< input type ="checkbox" id ="CheckBox1" name ="CheckBox1" onclick ="GetAllCheckBox(this)" />
全选
</ HeaderTemplate >
< ItemTemplate >
< input type ="checkbox" name ="CheckBox2" />
</ ItemTemplate >
< ItemStyle HorizontalAlign ="Center" Width ="100px" />
</ asp:TemplateField >
2. 实现全选的JS代码:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
<
script type
=
"
text/javascript
"
language
=
"
javascript
"
>
function GetAllCheckBox(checkAll){
var items = document.getElementsByName( " CheckBox2 " );
for ( var i = 0 ;i < items.length;i ++ ){
items[i].checked = checkAll.checked;
}
}
< / script>
function GetAllCheckBox(checkAll){
var items = document.getElementsByName( " CheckBox2 " );
for ( var i = 0 ;i < items.length;i ++ ){
items[i].checked = checkAll.checked;
}
}
< / script>
第三步:实现光棒效果 突出显示被选中的行
编写RowDataBound事件 代码
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
protected
void
GridView1_RowDataBound(
object
sender, GridViewRowEventArgs e)
{
/* *
* 实现光棒效果
* */
if (e.Row.RowType == DataControlRowType.DataRow) // 判断是否是数据行 去除第一行(标题行)
{
e.Row.Attributes.Add( " onmouseover " , " currentcolor=this.style.backgroundColor;this.style.backgroundColor='#6699ff' " );
e.Row.Attributes.Add( " onmouseout " , " this.style.backgroundColor=currentcolor " );
}
}
{
/* *
* 实现光棒效果
* */
if (e.Row.RowType == DataControlRowType.DataRow) // 判断是否是数据行 去除第一行(标题行)
{
e.Row.Attributes.Add( " onmouseover " , " currentcolor=this.style.backgroundColor;this.style.backgroundColor='#6699ff' " );
e.Row.Attributes.Add( " onmouseout " , " this.style.backgroundColor=currentcolor " );
}
}
四。数据绑定的其他知识:
绑定方法:Bind Eval 语法:<%# Bind("字段名") %>
字段格式化:{0:D} 设置显示的内容是货币类型 {0:D}设置显示的内容是数字 {0:yy--mm--dd} 设置显示是日期格式
五。 经验:在查询所有数据的时候如果实体类存在外键关系 建议使用DataTable或DataSet 获取数据 避免出现 使用SqlDataReader 没有关闭的异常
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
///
<summary>
/// 通过SQL语句查询所有的书籍
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public static List < Book > getBooksBySQL( string sql)
{
List < Book > list = new List < Book > ();
DataTable dt = DBHelper.GetDataSet(sql);
foreach (DataRow reader in dt.Rows)
{
Book book = new Book();
book.Author = reader[ " author " ].ToString();
book.AuthorDescription = reader[ " aurhorDescription " ].ToString();
book.Clicks = ( int )reader[ " Clicks " ];
book.ContentDescription = ( string )reader[ " ContentDescription " ];
book.EditorComment = reader[ " EditorComment " ].ToString();
book.Id = ( int )reader[ " Id " ];
book.Isbn = reader[ " Isbn " ].ToString();
book.PublishDate = (DateTime)reader[ " PublishDate " ];
book.Title = reader[ " Title " ].ToString();
book.Toc = reader[ " Toc " ].ToString();
book.UnitPrice = ( decimal )reader[ " UnitPrice " ];
book.WordsCount = ( int )reader[ " WordsCount " ];
book.Category = CategoryService.getCategoryByCategoryId(( int )reader[ " categoryid " ]); // PK 外键
book.Publisher = PublisherService.getPublisherByPublisherId(( int )reader[ " publisherid " ]); // PK 外键
list.Add(book);
}
return list;
}
/// 通过SQL语句查询所有的书籍
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public static List < Book > getBooksBySQL( string sql)
{
List < Book > list = new List < Book > ();
DataTable dt = DBHelper.GetDataSet(sql);
foreach (DataRow reader in dt.Rows)
{
Book book = new Book();
book.Author = reader[ " author " ].ToString();
book.AuthorDescription = reader[ " aurhorDescription " ].ToString();
book.Clicks = ( int )reader[ " Clicks " ];
book.ContentDescription = ( string )reader[ " ContentDescription " ];
book.EditorComment = reader[ " EditorComment " ].ToString();
book.Id = ( int )reader[ " Id " ];
book.Isbn = reader[ " Isbn " ].ToString();
book.PublishDate = (DateTime)reader[ " PublishDate " ];
book.Title = reader[ " Title " ].ToString();
book.Toc = reader[ " Toc " ].ToString();
book.UnitPrice = ( decimal )reader[ " UnitPrice " ];
book.WordsCount = ( int )reader[ " WordsCount " ];
book.Category = CategoryService.getCategoryByCategoryId(( int )reader[ " categoryid " ]); // PK 外键
book.Publisher = PublisherService.getPublisherByPublisherId(( int )reader[ " publisherid " ]); // PK 外键
list.Add(book);
}
return list;
}
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
/// <summary>
/// 根据分类ID查询分类信息
/// </summary>
/// <param name="categoryId"></param>
/// <returns></returns>
public static Category getCategoryByCategoryId( int categoryId)
{
string sql = " select * from categories where id = @id " ;
Category cate = new Category();
SqlParameter[] para = new SqlParameter[]
{
new SqlParameter( " @id " ,categoryId)
};
DataTable table = DBHelper.GetDataSet(sql,para);
foreach (DataRow reader in table.Rows)
{
cate.Id = ( int )reader[ " id " ];
cate.Name = ( string )reader[ " name " ];
}
return cate;
}
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
/// <summary>
/// 根据出版社编号ID查询出版社信息
/// </summary>
/// <param name="publisherId"></param>
/// <returns></returns>
public static Publisher getPublisherByPublisherId( int publisherId)
{
string sql = " select * from publishers where id = @id " ;
Publisher pub = new Publisher();
SqlParameter[] para = new SqlParameter[]
{
new SqlParameter( " @id " ,publisherId)
};
DataTable table = DBHelper.GetDataSet(sql,para);
foreach (DataRow reader in table.Rows)
{
pub.Id = ( int )reader[ " id " ];
pub.Name = ( string )reader[ " name " ];
}
return pub;
}
还未总结完。