GridView 总结

1、GridView1_RowDataBound

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //这里的处理是为了回显之前选中的情况
        if (e.Row.RowIndex > -1 && this.SelectedItems != null)
        {
            DataRowView row = e.Row.DataItem as DataRowView;
            CheckBox cb = e.Row.FindControl("CheckBox1") as CheckBox;
            if (this.SelectedItems.Contains(row["file_id"].ToString()))
                cb.Checked = true;
            else
                cb.Checked = false;
        }

        //改变颜色
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //得到正在被创建的项的称呼
            string uploadState = (string)DataBinder.Eval(e.Row.DataItem, "file_uploadstate");
            //变颜
            if (uploadState == "1")
            {
                e.Row.BackColor = System.Drawing.Color.LightPink;
                e.Row.ForeColor = System.Drawing.Color.Maroon;
            }
            else if (uploadState == "2")
            {
                e.Row.BackColor = System.Drawing.Color.LightCyan;
                e.Row.ForeColor = System.Drawing.Color.DarkBlue;
            }
        }
    }

2、分页

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        InitPage();    

  }

//重新绑定

 private void InitPage()
    {
        GridViewBind2();
    }

 3、行删除

首先在GridView的<Columns></Columns>内添加

<asp:CommandField ShowDeleteButton="True" HeaderText="操作" />

后添加GridView1_RowDeleting(行删除事件)

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string id = GridView1.DataKeys[e.RowIndex].Values[0].ToString();
        string strConn = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        string strSql = "delete from archives where ar_id=" + id;
        try
        {
            SqlConnection conn = new SqlConnection(strConn);
            if (conn.State.ToString() == "Closed")
                conn.Open();
            SqlCommand comm = new SqlCommand(strSql, conn);
            comm.ExecuteNonQuery();

            comm.Dispose();
            if (conn.State.ToString() == "Open")
                conn.Close();
            GridView1.EditIndex = -1;
            GridViewBind();
        }
        catch (Exception ex)
        {
            Response.Write("数据库错误,错误原因:" + ex.Message);
            Response.End();
        }
    }

 4、GridView中的值自动换行或不换行

GridView1.Attributes.Add("style", "word-break:break-all;word-wrap:break-word");
加到DataBound事件里面去

或放到

protected void Page_Load(object sender, EventArgs e)
    {
        //正常换行
        GridView1.Attributes.Add("style", "word-break:keep-all;word-wrap:normal");
        //下面这行是自动换行
        GridView1.Attributes.Add("style", "word-break:break-all;word-wrap:break-word");
        if (!IsPostBack)
        {
            bind();//调用数据绑定即可
        }
    }

 5、表格线颜色的问题

设置每一列的ITEMSTYLE  
  <asp:TemplateField   ItemStyle-BorderColor ="Black"   ItemStyle-BorderWidth="1"   ItemStyle-BorderStyle="Solid">   
         <ItemTemplate>   
                <asp:Label   ID="lblColumns3"   Width   =   "120"   runat="server"   Text='<%#DataBinder.Eval(Container.DataItem,"ID")   %>'   />   
          </ItemTemplate>  
  </asp:TemplateField>

 

 6、行编辑事件

 

ExpandedBlockStart.gif 代码
 1       ///   <summary>
 2       ///  行编辑事件
 3       ///   </summary>
 4       ///   <param name="sender"></param>
 5       ///   <param name="e"></param>
 6       protected   void  GridView1_RowEditing( object  sender, GridViewEditEventArgs e)
 7      {
 8          GridView1.EditIndex  =  e.NewEditIndex;
 9          GridViewBind();
10      }
11       ///   <summary>
12       ///  行更新事件
13       ///   </summary>
14       ///   <param name="sender"></param>
15       ///   <param name="e"></param>
16       protected   void  GridView1_RowUpdating( object  sender, GridViewUpdateEventArgs e)
17      {
18           string  id  =  GridView1.DataKeys[e.RowIndex].Values[ 0 ].ToString();
19 
20          DataTable dt  =  (DataTable)Session[ " table " ];
21          GridViewRow row  =  GridView1.Rows[e.RowIndex];
22           string  obj1  =  ((TextBox)(row.Cells[ 2 ].Controls[ 0 ])).Text;
23          TextBox tb  =  row.Cells[ 3 ].Controls[ 0 as  TextBox;
24          tb.TextMode  =  TextBoxMode.Password;
25 
26           string  obj2  =  ((TextBox)(row.Cells[ 3 ].Controls[ 0 ])).Text;
27 
28          obj2  =  System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(obj2,  " MD5 " );
29          dt.Rows[row.DataItemIndex][ " Pws " =  obj2;
30           using  (SqlConnection sqlConn  =   new  SqlConnection(SqlHelp.connString))
31          {
32              sqlConn.Open();
33               string  strSql  =   string .Format(
34                   " Update Express_Login  Set Name='{0}', Pws='{1}'  "   +
35                   "  where ID='{2}' " , obj1.ToString(), obj2.ToString(), id);
36               using  (SqlCommand sqlComm  =   new  SqlCommand(strSql, sqlConn))
37              {
38                  SqlDataAdapter da  =   new  SqlDataAdapter();
39 
40                  da.UpdateCommand  =  sqlComm;
41                  da.Update(dt);
42              }
43          }
44          GridView1.EditIndex  =   - 1 ;
45          GridViewBind();        
46      }
47       ///   <summary>
48       ///  绑定函数
49       ///   </summary>
50       private   void  BindData()
51      {
52          GridView1.DataSource  =  Session[ " table " ];
53          GridView1.DataBind();
54      }
55       ///   <summary>
56       ///  更新取消事件
57        ///   </summary>
58       ///   <param name="sender"></param>
59       ///   <param name="e"></param>
60       protected   void  GridView1_RowCancelingEdit( object  sender, GridViewCancelEditEventArgs e)
61      {
62          GridView1.EditIndex  =   - 1 ;
63          GridViewBind();
64      }
65 
66       ///   <summary>
67       ///  行绑定,调整编辑控件项的属性
68       ///   </summary>
69       ///   <param name="sender"></param>
70       ///   <param name="e"></param>
71       protected   void  GridView1_RowDataBound( object  sender, GridViewRowEventArgs e)
72      {
73           if  (e.Row.RowType  ==  DataControlRowType.DataRow)
74          {
75               if  (e.Row.RowState == DataControlRowState.Edit)
76              {
77                  TextBox tbNewName = (TextBox)e.Row.Cells[ 2 ].Controls[ 0 ];
78                  tbNewName.MaxLength  =   16 ;
79                  TextBox tbNewPws  =  (TextBox)e.Row.Cells[ 3 ].Controls[ 0 ];
80                  tbNewPws.TextMode  =  TextBoxMode.Password;
81                  tbNewPws.MaxLength  =   10 ;
82              }
83          }
84      }
85 
86 

 

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/NetCSharp/archive/2009/11/11/1600908.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值