asp.net 2.0的gridview基本知识

1 当要访问gridview的当前行时,可以使用的事件为 OnRowDataBound,
 protected virtual void OnRowDataBound(GridViewRowEventArgs e);
  在这个事件中,往往要关注的是rowtype和row state两个属性

其中,先来看下rowtype,
rowtype是a DataControlRowType 的集合,包括了
  
DataRow
EmptyDataRow
Footer,
Header
Pager
Seperator

   比如下面的代码检查了当前是否处于gridview的header位置
 
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
     if (e.Row.RowType == DataControlRowType.Header)
     {
     }
 }

如果要获得当前的数据行是处于什么样的状态,比如是编辑行,插入行,删除行,交替行都可以获得,则可以通过
rowstate属性获得
  下面的图可以清晰表现gridview的一些状态
  


      可以看到,比如当编辑某行的时候,rowstate的状态是编辑,当选择当选择了某行时,状态是selected,此外的也可以在图上清晰的看到


2  访问gridview的某一列
   要注意的是,访问时,可以用
e.Row.Cells[1]
去访问gridview中的第2列,(第1列的默认是0下标)
    如果是用了绑定列的话,比如
 
<asp:GridView ID="GridView1" …>
    <Columns>
        <asp:BoundField DataField="CustomerID" HeaderText="CustomerID" .../>
        <asp:BoundField DataField="CompanyName" HeaderText="CustomerID" .../>
        ...     
    </Columns>
</asp:GridView>
那么访问某一列时,可以这样
   
String customerId = e.Row.Cells[0].Text;
 
e.Row.Cells[0].Text = “New value for the first column”;

如果要改变某行的背景CSS,可以这样
    
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
     if (e.Row.RowType == DataControlRowType.DataRow)
     {
            if (e.Row.Cells[1].Text == “ANTON”)
            {
                   e.Row.Cells[1].Style.Add(“background-color”,”red”);
             }
     }
}
    再比如,如果已经将一个对象的集合绑定到一个gridview了,而且要访问其中的每一行,可以这样做
Customer customer = (Customer)e.Row.DataItem;
   比如下面的代码检查每一行,如果发现ID为ANTON的话,则变颜色
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
     if (e.Row.RowType == DataControlRowType.DataRow)
     {
            Customer customer = (Customer)e.Row.DataItem;
 
            if (customer.ID == “ANTON”)
            {
                   e.Row.Cells[1].Style.Add(“background-color”,”red”);
             }
     }
}
如果是用模版列的话,而要访问gridview中的某个控件,可以用findcontrol
   
<asp:GridView ID="GridView1" ...>
 <Columns>
    <asp:BoundField DataField="CustomerID" ... />
     <asp:TemplateField HeaderText="CompanyName" ...>
       <ItemTemplate>
         <asp:Label ID="Label1" runat="server" Text='<%# Bind("CompanyName") %>'/>
       </ItemTemplate>
     </asp:TemplateField>
  </Columns>
</asp:GridView>
  要获得label1标签,可以这样,当然这前提是你准确知道要找的是第几列
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
     if (e.Row.RowType == DataControlRowType.DataRow)
     {
   Label myLabel = (Label)e.Row.Cells[1].FindControl(“Label1”);
     }
也可以这样,用findcontrol
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
     if (e.Row.RowType == DataControlRowType.DataRow)
     {
Label myLabel = (Label)e.Row.FindControl(“Label1”);
     }
}

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值