关于GridView的开发应用

最近的项目中有许多数据页面.之前只提供浏览,而现在需要添加进行修改的功能
之前是用的Repeater控件,如果在此基础上添加修改功能稍显麻烦
于是换成了GridView控件.现 将其中使用比较特殊的功能列出来共享:

[1]合并列头-添加动态行
需要在数据绑定时进行-RowDataBound
protected void gvShowDatas_RowDataBound(object sender, GridViewRowEventArgs e)
    
{
        
if (e.Row.RowType == DataControlRowType.Header)
        
{
            e.Row.Cells.Clear();
            e.Row.Cells.Add(
new TableCell());
            e.Row.Cells[
0].Text = "周 期";

            e.Row.Cells.Add(
new TableCell());
            e.Row.Cells[
1].Text = "黄金    涨跌";
            e.Row.Cells[
1].ColumnSpan = 2;

            e.Row.Cells.Add(
new TableCell());
            e.Row.Cells[
2].Text = "白银    涨跌";
            e.Row.Cells[
2].ColumnSpan = 2;

            e.Row.Cells.Add(
new TableCell());
            e.Row.Cells[
3].Text = "铂    涨跌";
            e.Row.Cells[
3].ColumnSpan = 2;

            e.Row.Cells.Add(
new TableCell());
            e.Row.Cells[
4].Text = "钯    涨跌";
            e.Row.Cells[
4].ColumnSpan = 2;

            e.Row.Cells.Add(
new TableCell());   //e.Row.Cells[5].Style.Add("text-align", "center");
            e.Row.Cells[5].Text = "修 改";
            e.Row.CssClass 
= "tb_title2";
        }

        
if (e.Row.RowType == DataControlRowType.Footer)
        
{
            e.Row.Cells[
0].Text = dataTime;
            e.Row.Cells[
0].Attributes.Add("text-align""center");
            e.Row.Cells[
0].ColumnSpan = 10;
            
for (int j = 1; j <= 9; j++)
            
{
                e.Row.Cells[j].Visible 
= false;
            }

        }

    }


代码中先进行列头的合并,并设置每个CELL的ColumnSpan属性.

在尾行处理时需要设置ShowFooter的属性为TRUE
通过手动的方式隐藏其它的单元格?[此处没有想明白-请高手指点...]

[2]由于在GridView控件的绑定隐藏列时,发送到客户端的数据中无隐藏域,特别是需要隐藏数据ID时[数据主键]无法获取.
还好GridView提供了DataKeyNames和DataKeys属性


//数据绑定
gvShowDatas.DataSource = dt;
gvShowDatas.DataBind();
gvShowDatas.DataKeyNames 
= new string[] "ID"};
//获取主键值
string editID = gvShowDatas.DataKeys[gvShowDatas.EditIndex]["ID"].ToString();

通过此种方式保存处理数据的主键

[3]
 <asp:BoundField DataField="Time" HeaderText="周期" ReadOnly="true">
           <ControlStyle Width="50px" />
          <ItemStyle CssClass="tb_title2" />
</asp:BoundField>

ReadOnly属于用于该行处理编辑状态时此列只读.
ControlStyle的 Width值将设置该列处理编辑状态时TextBox的宽度

[4]在绑定数据时,有些数据需要格式化.还好GridView提供了格式化属性
<asp:BoundField DataField="WChange" ReadOnly="True" DataFormatString="{0:F}" HtmlEncode="false" HeaderText="周涨跌幅(%)">
       <ItemStyle CssClass="tb_title2" />
</asp:BoundField>

此处只列出这种格式化方式,其它的根据需求来.可查MSDN

[5]GridView中的模板列使的数据绑定与处理更加灵活.
而且可以用Eval方法进行数据绑定

<asp:TemplateField HeaderText="修改" ShowHeader="False">
                                        
<ItemTemplate>
                                            
<asp:HyperLink NavigateUrl='<%#"OperaterManage.aspx?salesID="+Eval("ID")%>' ID="salesID"
                                                Text="修改" runat="server" Target="_self">
</asp:HyperLink>
                                        
</ItemTemplate>
                                        
<HeaderStyle CssClass="tb_title2" />
                                    
</asp:TemplateField>

[6]添加删除时的提示

protected void salesInfos_RowDataBound(object sender, GridViewRowEventArgs e)
    
{
        
if (e.Row.RowType == DataControlRowType.DataRow)
        
{
            
if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            
{
                ((LinkButton)e.Row.Cells[
5].Controls[0]).Attributes.Add("onclick""javascript:return confirm('请确认执行删除:\"" + e.Row.Cells[0].Text + "\"!')");
            }

        }

    }






posted @ 2008-06-04 15:05  大力哥的技术  阅读(473)  评论(0)    收藏  举报
版权
作者:Bober Song

出处:http://bober.cnblogs.com

Care健康:http://www.aicareyou.com

推荐空间:华夏名网

本文首发博客园,版权归作者跟博客园共有。

转载必须保留本段声明,并在页面显著位置给出本文链接,否则保留追究法律责任的权利。