DataGrid的模版列

      这两天琢磨了一下DataGrid的模版列,发现功能挺强,不过还有挺多没摸透的。用代码来说话吧。
      页面代码是这么写的:

 1                        <asp:DataGrid id="dgTasks" runat="server" AllowPaging="True" BorderStyle="Solid" PageSize="8"
 2                            ToolTip="点击链接处理该记录" BorderColor="#3168CD" AutoGenerateColumns="False" Width="1074">
 3                            <AlternatingItemStyle BackColor="#EEEEEE"></AlternatingItemStyle>
 4                            <HeaderStyle HorizontalAlign="Center" Font-Bold="True" Font-Size="18px"></HeaderStyle>
 5                            <Columns>
 6                                <asp:TemplateColumn HeaderText="处理">
 7                                    <ItemTemplate>
 8                                        <asp:HyperLink runat="server" Text="处理" NavigateUrl='<%# "javascript:DoTask(" + DataBinder.Eval(Container.DataItem, "PI_id") + ");" %>'>
 9                                        </asp:HyperLink>
10                                    </ItemTemplate>
11                                </asp:TemplateColumn>
12                                <asp:TemplateColumn HeaderText="ID">
13                                    <ItemTemplate>
14                                        <%base.GetLabelColumn((long)DataBinder.Eval(Container.DataItem, "PI_id"),0%>
15                                    </ItemTemplate>
16                                </asp:TemplateColumn>
17                                <asp:TemplateColumn HeaderText="描述">
18                                    <ItemTemplate>
19                                        <%base.GetTAColumn((long)DataBinder.Eval(Container.DataItem, "PI_id"),2%>
20                                    </ItemTemplate>
21                                </asp:TemplateColumn>
22                                <asp:TemplateColumn HeaderText="级别">
23                                    <ItemTemplate>
24                                        <%base.GetValueColumn((long)DataBinder.Eval(Container.DataItem, "PI_id"),3,4%>
25                                    </ItemTemplate>
26                                </asp:TemplateColumn>
27                                <asp:TemplateColumn HeaderText="时间">
28                                    <ItemTemplate>
29                                        <%base.GetDTColumn((long)DataBinder.Eval(Container.DataItem, "PI_id"),5%>
30                                    </ItemTemplate>
31                                </asp:TemplateColumn>
32                            </Columns>
33                            <PagerStyle Visible="False" HorizontalAlign="Right" Mode="NumericPages"></PagerStyle>
34                        </asp:DataGrid>
      还需要在后代码中写对应的函数:
 1        protected string GetTAColumn(long id,int colcount)
 2        {//获取text area模版列的item
 3            string item = "<textarea rows=\"3\" cols=\"16\" readonly=true title=";
 4            item += dsDataSource.Tables[0].Rows[(int)id][colcount].ToString() + ">";
 5            item += Server.HtmlEncode(dsDataSource.Tables[0].Rows[(int)id][colcount].ToString());
 6            item += "</textarea>";
 7            iTableWidth += 100;
 8            return item;
 9        }

10        protected string GetDTColumn(long id,int colcount)
11        {//获取日期时间模版列的item
12            string item = "<SPAN>";
13            if(dsDataSource.Tables[0].Rows[(int)id][colcount]!=DBNull.Value)
14            {
15                DateTime dt = (DateTime)dsDataSource.Tables[0].Rows[(int)id][colcount];
16                item += dt.ToShortDateString() + "<br/>" + dt.ToLongTimeString();
17            }

18            else
19                item += "&nbsp&nbsp&nbsp&nbsp";
20            item += "</SPAN>";
21            iTableWidth += 40;
22            return item;
23        }

24        protected string GetLabelColumn(long id,int colcount)
25        {//获取文本模版列的item
26            string item = "<SPAN>";
27            item += dsDataSource.Tables[0].Rows[(int)id][colcount].ToString();
28            item += "</SPAN>";
29            iTableWidth += 50;
30            return item;
31        }

32        protected string GetValueColumn(long id,int colcount,int coltypeid)
33        {//获取静态数据的对应文本模版列的item
34            string item = "<SPAN>";
35            DataRow[] foundRows = dsStaticData.Tables[0].Select("SD_typeid="+coltypeid.ToString());
36            for(int i=0;i<foundRows.Length;i++)
37            {
38                if(dsDataSource.Tables[0].Rows[(int)id][colcount].ToString()==foundRows[i]["SD_typevalue"].ToString())
39                    item += foundRows[i]["SD_typetext"].ToString();
40            }

41            item += "</SPAN>";
42            iTableWidth += 50;
43            return item;
44        }
      通过自定义的这些函数可以返回不同html控件。如GetDTColumn函数,就可以在日期和时间之间加个换行。
      当然,在页面文件中调用自定义函数可以传入字段名,这样可以更直观的控制列。如果同时在后代码中修改SQL调用方式,变成灵活的读取配置或数据库的方式,那就可以不用每次都编译工程,只在页面文件中就可以随意添加、删除列了。
posted @ 2007-06-15 12:30  badwood  阅读(517)  评论(0编辑  收藏  举报
Badwood's Blog