DataGrid 呈現數據——綁定與編程混合

DataGrid 呈現數據——綁定與編程混合

通常方式是只是使用綁定。樣式設置好後,每行每列都一樣。

也有可能,行列的樣式不全都一樣。

例如,將前兩列背景色黃色,其他則正常(白色)。

 

        Dim ds As New DataSet

        Dim sr As New System.IO.StringReader(sFieldValue)

        ds.ReadXml(sr)

 

        DataGrid1.DataSource = ds.Tables(0)

        Dim i As Integer

        Dim c1 As BoundColumn

 

        For i = 0 To ds.Tables(0).Columns.Count - 1

            '//ERRORNO

            If ds.Tables(0).Columns(i).ColumnName = "ERROR" Or ds.Tables(0).Columns(i).ColumnName = "NO" Then

            Else '//ERRORNO欄位,動態加載。

                c1 = New BoundColumn

                c1.HeaderText = ds.Tables(0).Columns(i).ColumnName

                c1.DataField = ds.Tables(0).Columns(i).ColumnName

                DataGrid1.Columns.Add(c1)

                c1 = Nothing

            End If

        Next

 

        DataGrid1.DataBind()

 

        ------------ASP.NET------------

                        <asp:datagrid id="DataGrid1" runat="server" BorderColor="Black" BorderWidth="1px" CellPadding="3"

                                AutoGenerateColumns="False">

                                <SelectedItemStyle Font-Size="10pt" Font-Names="Arial" Wrap="False"></SelectedItemStyle>

                                <EditItemStyle Font-Size="10pt" Font-Names="Arial" Wrap="False"></EditItemStyle>

                                <AlternatingItemStyle Font-Size="10pt" Font-Names="Arial" Wrap="False"></AlternatingItemStyle>

                                <ItemStyle Font-Size="10pt" Font-Names="Arial" Wrap="False"></ItemStyle>

                                <HeaderStyle Font-Size="10pt" Font-Names="Arial" BackColor="#AAAADD"></HeaderStyle>

                                <Columns>

                                        <asp:TemplateColumn HeaderText="#">

                                                <HeaderStyle Font-Size="10pt" Font-Names="Arial" Font-Bold="True" Wrap="False" HorizontalAlign="Left"

                                                        Width="40px" CssClass="GridHeader" VerticalAlign="Top" BackColor="#99CCCC"></HeaderStyle>

                                                <ItemStyle Font-Size="10pt" Font-Names="Arial" Wrap="False" HorizontalAlign="Center" CssClass="GridRow"

                                                        VerticalAlign="Middle"></ItemStyle>

                                                <ItemTemplate>

                                                        <asp:Label id="Label1" Width="100%" Runat="server" Text='&nbsp;<%# DataBinder.Eval(Container, "DataItem.NO") %>'>

                                                        </asp:Label>

                                                </ItemTemplate>

                                        </asp:TemplateColumn>

                                        <asp:TemplateColumn HeaderText="Alert Message">

                                                <HeaderStyle Font-Size="10pt" Font-Names="Arial" Font-Bold="True" Wrap="False" HorizontalAlign="Left"

                                                        ForeColor="Black" Width="400px" CssClass="GridHeader" VerticalAlign="Top" BackColor="#99CCCC"></HeaderStyle>

                                                <ItemStyle Font-Size="10pt" Font-Names="Arial" Wrap="False" HorizontalAlign="Left" ForeColor="Black"

                                                        CssClass="GridRow" VerticalAlign="Top" BackColor="Yellow"></ItemStyle>

                                                <ItemTemplate>

                                                        <asp:Label id=Label2 Text='&nbsp;<%# DataBinder.Eval(Container, "DataItem.ERROR") %>' Runat="server" Width="100%">

                                                        </asp:Label>

                                                </ItemTemplate>

                                        </asp:TemplateColumn>

                                </Columns>

                                <PagerStyle Font-Size="10pt" Font-Names="Arial"></PagerStyle>

                        </asp:datagrid>

 

學習資料
http://topic.csdn.net/t/20030603/20/1872011.html

posted @ 2010-08-13 15:50  yellowwood  阅读(137)  评论(0编辑  收藏  举报
Never Give UP