Página anterior Voltar ao início do trabalhoPágina seguinte 

GridView (Parte I) (página 2)

Mauricio Junior

 

 

GridView

Id = gdBancoServidor

 

 

HTML do GridView

<asp:GridView ID="gdBancoServidor" runat="server" CellPadding="4" AutoGenerateColumns="False" Width="70%" AllowPaging="True"

                                                            GridLines="None"

                                                            OnRowEditing="gdBancoServidor_RowEditing"

                                                            OnRowCancelingEdit="gdBancoServidor_RowCancelingEdit"

                                                            OnRowUpdating="gdBancoServidor_RowUpdating"

                                                            DataKeyNames="IdCooperativa"

                                                            OnRowDeleting="gdBancoServidor_RowDeleting">

                                                            <AlternatingRowStyle BackColor="#cdcdcd" />

                                                            <HeaderStyle BackColor="#CCCCCC" HorizontalAlign="Left" />

                                                            <PagerStyle HorizontalAlign="Left" />

                                                            <Columns>

                                                                <asp:TemplateField ShowHeader="false">

                                                                    <ItemTemplate>

                                                                        <asp:Label Visible="false" ID="lblIdCooperativa" runat="server">

                                                                            <%# DataBinder.Eval(Container.DataItem, "IdCooperativa")%>

                                                                        </asp:Label>

                                                                    </ItemTemplate>

                                                                     <EditItemTemplate>

                                                                         <asp:TextBox Visible="false" ID="txtIdCooperativa" runat="server"

                                                                        Text='<%# DataBinder.Eval(Container.DataItem, "IdCooperativa") %>'>

                                                                        </asp:TextBox>

                                                                    </EditItemTemplate>

                                                                </asp:TemplateField>

                                                                <asp:TemplateField HeaderText="NR COOP">

                                                                    <ItemTemplate>

                                                                        <asp:Label ID="lblNumCooperativa" runat="server">

                                                                            <%# DataBinder.Eval(Container.DataItem, "NumCooperativa")%>

                                                                        </asp:Label>

                                                                    </ItemTemplate>

                                                                     <EditItemTemplate>

                                                                        <asp:TextBox ID="txtNumCooperativa" runat="server"

                                                                            Text='<%# DataBinder.Eval(Container.DataItem, "NumCooperativa") %>'>

                                                                        </asp:TextBox>

                                                                    </EditItemTemplate>

                                                                </asp:TemplateField>

                                                                <asp:TemplateField HeaderText="BANCO DE DADOS">

                                                                    <ItemTemplate>

                                                                        <asp:Label ID="lblInitialCatalog" runat="server">

                                                                            <%# DataBinder.Eval(Container.DataItem, "InitialCatalog") %>

                                                                        </asp:Label>

                                                                    </ItemTemplate>

                                                                    <EditItemTemplate>

                                                                        <asp:TextBox ID="txtInitialCatalog" runat="server"

                                                                            Text='<%# DataBinder.Eval(Container.DataItem, "InitialCatalog") %>'>

                                                                        </asp:TextBox>

                                                                    </EditItemTemplate>

                                                                </asp:TemplateField>

                                                               <asp:TemplateField ShowHeader="true" HeaderText="SERVIDOR">

                                                                    <ItemTemplate>

                                                                        <asp:Label ID="lblDataSource" runat="server">

                                                                            <%# DataBinder.Eval(Container.DataItem, "DATASOURCE") %>

                                                                        </asp:Label>   

                                                                    </ItemTemplate>

                                                                    <EditItemTemplate>

                                                                        <asp:TextBox ID="txtDataSource" runat="server"

                                                                        Text='<%# DataBinder.Eval(Container.DataItem, "DATASOURCE") %>'>

                                                                        </asp:TextBox>

                                                                    </EditItemTemplate>

                                                                </asp:TemplateField>                                                          

                                                               <asp:TemplateField ShowHeader="true" HeaderText="AMBIENTE">

                                                                    <ItemTemplate>

                                                                        <asp:Label ID="lblIdAmbiente" runat="server">

                                                                            <%# DataBinder.Eval(Container.DataItem, "IdAmbiente") %>

                                                                        </asp:Label>   

                                                                    </ItemTemplate>

                                                                    <EditItemTemplate>

                                                                        <asp:TextBox ID="txtIdAmbiente" runat="server"

                                                                        Text='<%# DataBinder.Eval(Container.DataItem, "IdAmbiente") %>'>

                                                                        </asp:TextBox>

                                                                    </EditItemTemplate>

                                                                </asp:TemplateField>

                                                               

                                                                <asp:CommandField ButtonType="Button" CancelText="C" DeleteText="D" EditText="E" HeaderText="A&#199;&#195;O"

                                                                        InsertVisible="False" ShowDeleteButton="true" ShowEditButton="True" UpdateText="A" />

                                                            </Columns>

                                                            <PagerSettings Mode="NumericFirstLast" Position="TopAndBottom" FirstPageText="Primeira" LastPageText="&#218;ltima" />

                                                        </asp:GridView>

Referência: 1.2 GridView

A referência 1.2 GridView é bem particular porque o mesmo mostra apenas os campos do meu bando de dados. Serve como exemplo onde você possa seguir e desenvolver o seu.

3. Código Behing

No próximo passo é preencher o grid utilizando o código C#.NET.

No método Load coloquei o seguinte código: (Referência 1.3 GridView)

 

        if (!Page.IsPostBack)

        {

            PreencheGridView();

        }

Referência: 1.3 GridView

Logo depois criei o método da referência 1.3 GridView.

 

/// <summary>

    /// Método que preenche o grid view

    /// </summary>

    private void PreencheGridView()

    {

       //instanciando método dentro da página.

       //camada de cima.

       ConfigurarConexao config = new ConfigurarConexao();

 

        this.gdBancoServidor.DataSource = config.SelecionarConfiguracaoOffLine();

        this.gdBancoServidor.DataBind();

    }

Referência: 1.4 GridView

Explicação:

É simples, fácil e prático. Chamei o método SelecionarConfiguracaoOffLine() que é responsável para trazer os dados como DataSet. Depois de pegar os dados e vincular ao DataSource do GridView, só chamar o DataBind(). Não é diferente do componente DataGrid do VS 2003.

O método SelecionarConfiguracaoOffLine() faz o seguinte: Conecta no banco de dados, faz o select na tabela de dados e retorna os campos que irei preencher no GridView como DataSet, apenas isso.

Executei o código e mesmo foi preenchido automaticamente. (Referência 1.5 GridView)

Referência: 1.5 GridView

Bom, fico por aqui e espero ter ajudado nessa primeira parte do artigo. Na segunda parte mostrarei como editar e deletar dados gridview.

Mauricio Junior

Comunidade: www.aspneti.com

Site: www.ascompras.com



 Página anterior Voltar ao início do trabalhoPágina seguinte 



As opiniões expressas em todos os documentos publicados aqui neste site são de responsabilidade exclusiva dos autores e não de Monografias.com. O objetivo de Monografias.com é disponibilizar o conhecimento para toda a sua comunidade. É de responsabilidade de cada leitor o eventual uso que venha a fazer desta informação. Em qualquer caso é obrigatória a citação bibliográfica completa, incluindo o autor e o site Monografias.com.