none
Como adicionar itens no GridView? RRS feed

  • Pergunta

  • Eu queria adicionar itens no meu gridview no evento de um click do botão...

    Tentei isso:

    GridView1.Rows.Item(1).Cells(1).Text = "teste"

    Só que deu isso:

    Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index

    Alguém sabe como adiciono itens no meu dagridView dinâmicamente?



    quarta-feira, 21 de março de 2012 18:59

Respostas

  • Cara tenta assim!

    <%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
        CodeFile="ManipulandoGridView.aspx.cs" Inherits="ExemplosMSDN_ManipulandoGridView" %>
    
    <asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="Server">
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="Server">
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                 <asp:GridView ID="gvAddItem" runat="server" AutoGenerateColumns="False">
                    <Columns>
                        <asp:CommandField ShowEditButton="True" ShowSelectButton="True" />
                        <asp:TemplateField HeaderText="Nome">
                            <EditItemTemplate>
                                <asp:TextBox ID="txtNome" runat="server" Text='<%# Eval("Nome") %>' />
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="lblNome" runat="server" Text='<%# Eval("Nome") %>' />
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="E-mail">
                            <EditItemTemplate>
                                <asp:TextBox ID="txtEmail" runat="server" Text='<%# Eval("Email") %>' />
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="lblEmail" runat="server" Text='<%# Eval("Email") %>' />
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
                <br />
                <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
             </ContentTemplate>
        </asp:UpdatePanel>
    </asp:Content>
    

    cod.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    
    public partial class ExemplosMSDN_ManipulandoGridView : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
    
    
        protected void Button1_Click(object sender, EventArgs e)
        {
            DataTable tb = new DataTable();
    
            if (Session["tbl"] != null)
                tb = Session["tbl"] as DataTable;
            else
            {
                tb.Columns.Add("Nome", typeof(string));
                tb.Columns.Add("Email", typeof(string));
            }
    
            DataRow row;
            row = tb.NewRow();
            row["Nome"] = "nome";
            row["Email"] = "Email";
            tb.Rows.Add(row);
    
            gvAddItem.DataSource = tb;
            gvAddItem.DataBind();
    
            Session["tbl"] = tb;
        }
    }
    
    att.

    quarta-feira, 21 de março de 2012 21:22