2013-09-08 12 views
7

Zrobiłem klasę z tym kodem:Dodaj nowe dane wiersz do gridview asp.net C#

public class Customer 
{ 
    public Customer() { } 
    public Customer(Customer cust) 
    { 
     ID = cust.ID; 
     Name = cust.Name; 
     FatherName = cust.FatherName; 
     Email = cust.Email; 
    } 
    public int ID { get; set; } 
    public string Name { get; set; } 
    public string FatherName { get; set; } 
    public string Email { get; set; } 
} 

i stworzył tę funkcję do listy załadować z jakiegoś danych:

public List<Customer> Generate_Data() 
{ 
    List<Customer> lstCustomer = new List<Customer>(); 
    Customer customer = new Customer(); 

    customer.ID = 1; 
    customer.Name = "John Cena"; 
    customer.FatherName = "John"; 
    customer.Email = "[email protected]"; 
    lstCustomer.Add(new Customer(customer)); 

    customer.ID = 2; 
    customer.Name = "Mokesh"; 
    customer.FatherName = "Rajnikant"; 
    customer.Email = "[email protected]"; 
    lstCustomer.Add(new Customer(customer)); 

    customer.ID = 3; 
    customer.Name = "Bilal Ahmad"; 
    customer.FatherName = "Kashif"; 
    customer.Email = "[email protected]"; 
    lstCustomer.Add(new Customer(customer)); 

    customer.ID = 4; 
    customer.Name = "Chin Shang"; 
    customer.FatherName = "Shang Woe"; 
    customer.Email = "[email protected]"; 
    lstCustomer.Add(new Customer(customer)); 

    return lstCustomer; 
} 

zwrócą listę, aby powiązać z siatką. Kod jest:

List<Customer> lstCustomer = new List<Customer>(); 
lstCustomer = Generate_Data(); 
GridView1.DataSource = lstCustomer; 
GridView1.DataBind(); 

moje pytania są następujące:

  1. dodałem 4 otaczaniem i przycisk do strony aspx z nazwami: Id,Name,FatherName,Email Po kliknięciu na przycisk, chcę dodać nowe wartości pól tekstowych do widoku gridview1. Chcę dynamicznie dodawać wiersz do widoku siatki.

  2. Jeśli definiuję pusty widok siatki, w jaki sposób mogę dodać wartości pola tekstowego do wierszy gridview? Czy nie jest równa metoda z question1?

Odpowiedz

5

ASPX:

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br /> 
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br /> 
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox><br /> 
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox><br /> 
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" /> 
<asp:GridView ID="GridView1" runat="server"/> 

Kod za:

public class Customer 
{ 
    public Customer() { } 
    public Customer(Customer cust) 
    { 
     ID = cust.ID; 
     Name = cust.Name; 
     FatherName = cust.FatherName; 
     Email = cust.Email; 
    } 
    public int ID { get; set; } 
    public string Name { get; set; } 
    public string FatherName { get; set; } 
    public string Email { get; set; } 
} 
protected void Button1_Click(object sender, EventArgs e) 
{ 
    List<Customer> lstCustomer = new List<Customer>(); 
    if (Session["dt"] != null) 
    { 
     lstCustomer = (List<Customer>)Session["dt"]; 
    } 
    Customer customer = new Customer(); 
    customer.ID = int.Parse(TextBox1.Text); 
    customer.Name = TextBox2.Text; 
    customer.FatherName = TextBox2.Text; 
    customer.Email = TextBox2.Text; 
    lstCustomer.Add(new Customer(customer)); 
    GridView1.DataSource = lstCustomer; 
    GridView1.DataBind(); 
    Session["dt"] = lstCustomer; 
} 

Aktualizacja!

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!IsPostBack) 
    { 
     List<Customer> lstCustomer = new List<Customer>(); 
     Customer customer = new Customer(); 

     customer.ID = 1; 
     customer.Name = "John Cena"; 
     customer.FatherName = "John"; 
     customer.Email = "[email protected]"; 
     lstCustomer.Add(new Customer(customer)); 

     customer.ID = 2; 
     customer.Name = "Mokesh"; 
     customer.FatherName = "Rajnikant"; 
     customer.Email = "[email protected]"; 
     lstCustomer.Add(new Customer(customer)); 

     customer.ID = 3; 
     customer.Name = "Bilal Ahmad"; 
     customer.FatherName = "Kashif"; 
     customer.Email = "[email protected]"; 
     lstCustomer.Add(new Customer(customer)); 

     customer.ID = 4; 
     customer.Name = "Chin Shang"; 
     customer.FatherName = "Shang Woe"; 
     customer.Email = "[email protected]"; 
     lstCustomer.Add(new Customer(customer)); 
     Session["dt"] = lstCustomer; 

    } 
} 
+0

Po kliknięciu przycisku dodaj pierwsza lista wyczyszczona i dodane nowe dane wiersza. Jak mogę zapisać pierwszą listę? –

+0

@a_ahmadi moja odpowiedź zaktualizowana. dodaj pierwszą listę do pageLoad. –

+0

Bardzo dobrze @ samiey-mehdi, Dzięki. –

0

Można po prostu dodać AutoGenerateInsertButton="True" do znacznika <asp:GridView>.

+0

Twoja odpowiedź nie rozwiązała mojego pytania! –

0

Tak, zrobiłbym to tak. Po pierwsze, rozszerzyłbym zakres lstCustomer tak, aby działał dalej przez zwrotne informacje.

Następnie w obsłudze zdarzenia przycisk, chciałbym napisać coś takiego

//Here we create the object 
Customer customer = new Customer(); 
customer.foo = txtFoo.Text; 
customer.bar = txtBar.Text; 
//.... 

//Then we add it to the list 
lstCustomer.Add(customer); 

//and databind on it again 
GridView1.DataSource = lstCustomer; 
GridView1.DataBind(); 

Pomaga to również, jeśli chcesz, aby użytkownik mógł wykonywać pewne rzeczy na danych (lub chcesz utrzymują się do bazy lub coś).

+0

Twoja odpowiedź nie rozwiązała mojego pytania! –

2

Trzeba będzie utrzymywać swoją kolekcję podczas odświeżenie strony. W zależności od tego, jak duży jest swojej kolekcji można rozważyć utrzymanie go w stan wyświetlania, sesji itp

List<Customer> lstCustomer = new List<Customer>(); 
lstCustomer = Generate_Data(); 
ViewState["Customers"] = lstCustomer; 
GridView1.DataSource = lstCustomer; 
GridView1.DataBind(); 

Więc gdy użytkownik wypełnia pola tekstowe z danymi i inicjuje odświeżenie strony, można utworzyć nowy obiekt klienta i dodać go do istniejącej kolekcji. Następnie ponownie powiązaj dane z siecią.

Customer customer = new Customer(){ID=int.Parse(TextBox1.Text), Name = TextBox2.Text, 
    FatherName = TextBox2.Text, Email = TextBox2.Text } 
var lstCustomer = ViewState["Customers"] as List<Customers>; 
    lstCustomer.Add(customer); 

Uwaga trzeba będzie dodać atrybut [Serializable] do klasy Customer.

+0

Po kliknięciu przycisku dodaj pierwsza lista wyczyszczona i dodane nowe dane wiersza. Jak mogę zapisać pierwszą listę? –

+1

ViewState ["Klienci"] = lstCustomer; zapisze kolekcję w okienku podglądu. następnie przy dodawaniu nowych danych najpierw pobierasz kolekcję z Viewstate, a następnie dodajesz do niej nowego klienta (drugi blok kodu) – NoviceProgrammer

Powiązane problemy