给GridView绑定基本数据:
1
protected
void
Page_Load(
object
sender, EventArgs e)
2

{
3
if (!IsPostBack)
4

{
5
GridView_DataBind();
6
}
7
}
8
private
void
GridView_DataBind()
9

{
10
DataTable DT = new DataTable();
11
string DBString =
12
"select account_phone,account_goods from t_account_all";
13
DT = SqlHelper.ExecuteDataTable(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text,DBString);
14
Click_GridView.DataSource = DT;
< /span>15< span style="color: #000000;">
//这里要绑定的GridView 是:Click_GridView
16
Click_GridView.DataBind();
17
}
给GridView的行和单元格绑定数据,在RowDataBound事件中进行,对于需要根据具体数据值绑定的时候效果最好:
1
protected
void
Click_GridView_RowDataBound(
object
sender, GridViewRowEventArgs e)
2

{
3
if ((e.Row.RowIndex != -1) && (e.Row.RowType == DataControlRowType.DataRow))
4

{
< /span> 5 
/**/ /* 单元格绑定:
6< span style="color: #008000;">
* 绑定属性onclick
< span style="color: #008080;"> 7
* 如果值为self.location那么表示在当前窗口打开新页面< br /> & nbsp;8
* 如果值为window.open那么表示在新的窗口打开页面
9
*/
10
e.Row.Cells[0].Attributes.Add("onclick", /**//*"self.location='"*/"window.open('" + "../Default.aspx?" + HttpUtility.UrlEncode("Phone=" + ((HyperLink)(e.Row.Cells[0].Controls[1])).Text+"')"));
11
e.Row.Cells[0].Attributes.Add("onmouseover", "this.style.background='#f70'");
12
e.Row.Cells[0].Attributes.Add("onmouseout", "this.style.background='#fff'");
< /span>13< span style="color: #000000;">
/**/ /* 行级绑定:
< span style="color: #008080;">14
* 点击行就执行跳转
15
*/
16
e.Row.Attributes.Add(
"
onclick
"
,
"
window.open('../Default.aspx?
"
+
HttpUtility.UrlEncode(
"
Id=
"
+
((HyperLink)(e.Row.Cells[
1
].Controls[
1
])).Text)
+
"
')
"
);
< /span>17< span style="color: #000000;">
/**/
/* 在行级和单元格级同时绑定
18
* 在点击单元格1时,
< span style="color: #008080;">19
* 同时激发两个事件
< span style="color: #008080;">20
* 其中,单元格自己的事件在前
21
* 行级事件在后
22
* 两者不发生冲突
23
*/
24
1
protected
void
DataBound_GridView_RowDataBound(
object
sender, GridViewRowEventArgs e)
2

{
< /span> 3 
实现1#region 实现1
4
if ((e.Row.RowIndex != -1) && (e.Row.RowType == DataControlRowType.DataRow))
5

{
< /span> 6
//获取特定项的值< /span>
7
string CellString_1 =((HyperLink)(e.Row.FindControl("Link1"))).Text;
8
if ((CellString_1 != "") && (CellString_1 != "0"))
9

{
< /span>10< span style="color: #000000;">
//绑定链接,如需让在新窗口打 开,则将HyperLink的属性Target设置为"_blank"
11
((HyperLink)(e.Row.FindControl("Link1"))).NavigateUrl = "../Default.aspx?" + HttpUtility.UrlEncode("Phone=" + CellString_1, Encoding.UTF32);
12
UsefullCount_1++;
13
}
14
}
15
#endregion
16
< /span>17< span style="color: #000000;">
实现2#region 实现2
18
if (e.Row.RowIndex != -1 && e.Row.RowType == DataControlRowType.DataRow)
19

{
< /span>20< span style="color: #000000;">
//获取特定项的值< /span>
21
string CellString_2 =((HyperLink)(e.Row.Cells[0].Controls[1])).Text;
22
if ((CellString_2 != "") && (CellString_2 != "0"))
23

{
< /span>24< span style="color: #000000;">
//绑定链接< /span>
25
/**/ /* GridView的行级单元格的序列:
< /span>26< span style="color: #008000;">
* Cells的索引从0开始
27
* Cell中的Controls的索引从1开始,0的位置是系统的 Control
28
*/
29
((HyperLink)(e.Row.Cells[1].Controls[1])).NavigateUrl = "../Default.aspx?" + HttpUtility.UrlEncode("Good=" + CellString_2, Encoding.UTF32);
30
UsefullCount_2++;
31
}
32
}
33
#endregion
34
< /span>35< span style="color: #000000;">
添加属性:高亮+事件OnClick#region 添加属性:高亮+事件OnClick
36
DataRowView DRV = e.Row.DataItem as DataRowView;
37
if (DRV != null)
38

{
39
string thisUrl = "../Default.aspx?" + HttpUtility.UrlEncode(e.Row.RowIndex.ToString(), Encoding.UTF32);
40
e.Row.Attributes.Add("style", "cusor:hand");
41
e.Row.Attributes.Add("onmouseover", "this.style.background='#f70'");
42
e.Row.Attributes.Add("onmouseout", "this.style.background='#fff'");
43
e.Row.Attributes.Add("onclick", "self.location='" + thisUrl + "'");
44
}
45
#endregion
46
}
}
25
}
GridView的RowDataBound另一个示例: