jackyrong 发表于 2013-1-29 07:52:50

asp.net 2.0的gridview基本知识

在老外的一篇blog里(http://fredrik.nsquared2.com/ViewPost.aspx?PostID=429),很好的一篇文章介绍了
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的一些状态
   http://www.cnblogs.com/images/cnblogs_com/jackyrong/gridview.gif


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


2  访问gridview的某一列
   要注意的是,访问时,可以用
e.Row.Cells
去访问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.Text;
 
e.Row.Cells.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.Text == “ANTON”)
            {
                   e.Row.Cells.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.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.FindControl(“Label1”);
     }
也可以这样,用findcontrol

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
     if (e.Row.RowType == DataControlRowType.DataRow)
     {
Label myLabel = (Label)e.Row.FindControl(“Label1”);
     }
}

 

  
页: [1]
查看完整版本: asp.net 2.0的gridview基本知识