初学Asp.net关于GridView的一些学习记录

第一次接触Asp.net,是因为暑假接的一个项目做起的.

现在这个项目已经发布了.

从这个项目中,我学到的具体编程知识(关于GridView)有具体说来,有以下几点:

1.

在后台代码程序里绑定数据源:

 

  < asp:GridView  ID ="GridView1"  runat ="server"  AllowPaging ="True"  AutoGenerateColumns ="False"
            CellPadding
="4"  DataKeyNames ="InformID"  ForeColor ="#31639C"  GridLines ="None"
            HorizontalAlign
="Left"  OnPageIndexChanging ="GridView1_PageIndexChanging"
            PageSize
="7"  Style ="left: 37px; position: relative; top: -25px" >
            
< FooterStyle  BackColor ="#31639C"  Font-Bold ="True"  ForeColor ="White"  BorderColor ="#31639C"   />
            
< Columns >
                
< asp:TemplateField  HeaderText ="消息" >
                    
< ItemTemplate >
                        
< asp:Image  ID ="Image1"  runat ="server"  ImageUrl ="~/Inform/9.gif"  Style ="position: relative"   />
                    
</ ItemTemplate >
                    
< ItemStyle  Width ="80px"   />
                    
< HeaderStyle  HorizontalAlign ="Left"   />
                
</ asp:TemplateField >
                
< asp:BoundField  DataField ="IsNew"  HeaderText ="状态" >
                    
< ItemStyle  Font-Size ="Small"  ForeColor ="Navy"  Width ="100px"   />
                    
< HeaderStyle  HorizontalAlign ="Left"   />
                
</ asp:BoundField >
                
< asp:HyperLinkField  DataNavigateUrlFields ="InformID"  DataNavigateUrlFormatString ="InformDetail.aspx?ID={0}"
                    DataTextField
="InformTitle"  HeaderText ="消息主题"  Target ="_blank" >
                    
< ItemStyle  Font-Size ="Small"  Width ="580px"   />
                    
< HeaderStyle  HorizontalAlign ="Left"   />
                
</ asp:HyperLinkField >
                
< asp:BoundField  DataField ="InformDate"  HeaderText ="发布日期"   >
                    
< ItemStyle  ForeColor ="Gray"   />
                
</ asp:BoundField >
            
</ Columns >
            
< EditRowStyle  BackColor ="#31639C"   />
            
< SelectedRowStyle  BackColor ="#C5BBAF"  Font-Bold ="True"  ForeColor ="#333333"   />
            
< PagerStyle  BackColor ="#31639C"  ForeColor ="White"  HorizontalAlign ="Center"   />
            
< HeaderStyle  BackColor ="#31639C"  Font-Bold ="True"  ForeColor ="White"  Wrap ="False"   />
            
< AlternatingRowStyle  BackColor ="White"   />
            
< PagerSettings  PageButtonCount ="30"   />
        
</ asp:GridView >

后台代码中像正常一样绑定数据一样:

 


    
private   void  createBind()
    

     SqlConnection conn;
     SqlCommand cmd;
     SqlDataAdapter da;

   
//  string strConn = "server=2E97168B31034F3;database=hr; Trusted_Connection=yes";
 
     
string strConn = System.Configuration.ConfigurationManager.AppSettings["hrConnection"].ToString();
        conn 
= new SqlConnection(strConn);

      cmd
=new SqlCommand("DisplayInform34",conn);
      cmd.CommandType
=CommandType.StoredProcedure;
         
      cmd.Parameters.Add(
"@Employee12",SqlDbType.Int,4);
      cmd.Parameters[
"@Employee"].Value=liangjingbo;
    
     da 
= new SqlDataAdapter(cmd);
    
     DataSet ds 
= new DataSet() ;

     da.Fill(ds);
     GridView1.DataSource 
= ds;
     GridView1.DataBind();
     conn.Close();

    
    }

 

2.在这种情况下,实现分页,可以这样在代码:

 

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

3.从数据库中取出数据经过处理再绑定到数据库

 

< asp:GridView  ID ="GridView1"  runat ="server"  BackColor ="White"  BorderColor ="#7CA3CF"
            BorderStyle
="Double"  BorderWidth ="3px"  CellPadding ="4"  GridLines ="Horizontal"
            Height
="211px"  Style ="left: 81px; position: relative; top: 2px"  Width ="533px"   >
            
< FooterStyle  BackColor ="White"  ForeColor ="#333333"   />
            
< Columns >
                
< asp:TemplateField  HeaderText ="评价统计" >
                    
< HeaderStyle  HorizontalAlign ="Center"   />
                    
< ItemTemplate >
                        
< asp:Image  ID ="Image1"  runat ="server"  ImageUrl ="~/Praise/4.gif"  Style ="position: relative"   />
                    
</ ItemTemplate >
                
</ asp:TemplateField >
                
< asp:HyperLinkField  DataNavigateUrlFields ="院系列表"  HeaderText ="具体查看"  Text ="查看"  DataNavigateUrlFormatString ="PCollegeDetail.aspx?name={0}"   />
            
</ Columns >
            
< RowStyle  BackColor ="White"  ForeColor ="#333333"  HorizontalAlign ="Center"   />
            
< SelectedRowStyle  BackColor ="#339966"  Font-Bold ="True"  ForeColor ="White"   />
            
< PagerStyle  BackColor ="#336666"  ForeColor ="White"  HorizontalAlign ="Center"   />
            
< HeaderStyle  BackColor ="#7CA3CF"  Font-Bold ="True"  ForeColor ="White"  HorizontalAlign ="Center"   />
        
</ asp:GridView >

 

后台代码如下:

 

         SqlCommand com1  =   new  SqlCommand( " PFiguregood " , conn);
                com1.CommandType 
=  CommandType.StoredProcedure;
            
// 放到Arraylist name 中,以便与在下面使用

            DataTable mess 
=   new  DataTable();
            mess.Columns.Add(
new  DataColumn( " 列表 " typeof ( string )));
            mess.Columns.Add(
new  DataColumn( " 次数1 " typeof ( int )));
            mess.Columns.Add(
" 次数2 " typeof ( int ));

            
for  ( int  j  =   0 ; j  <  name.Count; j ++ )
            
{
                DataRow dr 
= mess.NewRow();
                dr[
0= name[j].ToString()+" ";
                dr[
1= Convert.ToInt32(goodCount[j]);
                dr[
2= Convert.ToInt32(badCount[j]);
                mess.Rows.Add(dr);
            }

            DataSet dataset 
=   new  DataSet();
                    
            dataset.Tables.Add(mess);

            dataset.Tables[
0 ].DefaultView.Sort  =   "  表扬次数 DESC  , 批评次数   " ;
                     
this .GridView1.DataSource  =  dataset.Tables[ 0 ].DefaultView;

                               
this .GridView1.DataBind();
         

 4.GridView中的循环

 

 

  foreach  (GridViewRow rr  in   this .GridView1.Rows)
                
{
                    
if ((rr.Cells[0].Text.ToString()) == "soff")
                        ((CheckBox)rr.FindControl(
"one")).Checked = true;
                }

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值