GridView的操作:基本操作(行和单元格绑定)
给GridView绑定基本数据:
1protected void Page_Load(object sender, EventArgs e)
2{
3if (!IsPostBack)
4{
5GridView_DataBind();
6}
7}
8private void GridView_DataBind()
9{
10DataTable DT = new DataTable();
11string DBString =
12"select account_phone,account_goods from t_account_all";
13DT = SqlHelper.ExecuteDataTable(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text,DBString);
14Click_GridView.DataSource = DT;
< /span>15< span style="color: #000000;"> //这里要绑定的GridView 是:Click_GridView
16Click_GridView.DataBind();
17}
给GridView的行和单元格绑定数据,在RowDataBound事件中进行,对于需要根据具体数据值绑定的时候效果最好:2{
3if (!IsPostBack)
4{
5GridView_DataBind();
6}
7}
8private void GridView_DataBind()
9{
10DataTable DT = new DataTable();
11string DBString =
12"select account_phone,account_goods from t_account_all";
13DT = SqlHelper.ExecuteDataTable(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text,DBString);
14Click_GridView.DataSource = DT;
< /span>15< span style="color: #000000;"> //这里要绑定的GridView 是:Click_GridView
16Click_GridView.DataBind();
17}
1protected void Click_GridView_RowDataBound(object sender, GridViewRowEventArgs e)
2{
3if ((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*/
10e.Row.Cells[0].Attributes.Add("onclick", /*"self.location='"*/"window.open('" + "../Default.aspx?" + HttpUtility.UrlEncode("Phone=" + ((HyperLink)(e.Row.Cells[0].Controls[1])).Text+"')"));
11e.Row.Cells[0].Attributes.Add("onmouseover", "this.style.background='#f70'");
12e.Row.Cells[0].Attributes.Add("onmouseout", "this.style.background='#fff'");
< /span>13< span style="color: #000000;"> /* 行级绑定:
< span style="color: #008080;">14* 点击行就执行跳转
15*/
16e.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
25}
GridView的RowDataBound另一个示例:2{
3if ((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*/
10e.Row.Cells[0].Attributes.Add("onclick", /*"self.location='"*/"window.open('" + "../Default.aspx?" + HttpUtility.UrlEncode("Phone=" + ((HyperLink)(e.Row.Cells[0].Controls[1])).Text+"')"));
11e.Row.Cells[0].Attributes.Add("onmouseover", "this.style.background='#f70'");
12e.Row.Cells[0].Attributes.Add("onmouseout", "this.style.background='#fff'");
< /span>13< span style="color: #000000;"> /* 行级绑定:
< span style="color: #008080;">14* 点击行就执行跳转
15*/
16e.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
1protected void DataBound_GridView_RowDataBound(object sender, GridViewRowEventArgs e)
2{
< /span> 3 实现1
}2{
< /span> 3 实现1
25}