我用这段代码做了一个类:
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; }
}
并创建了这个函数来加载一些数据的列表:
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 = "cena@gmail.com";
lstCustomer.Add(new Customer(customer));
customer.ID = 2;
customer.Name = "Mokesh";
customer.FatherName = "Rajnikant";
customer.Email = "mokesh@gmail.com";
lstCustomer.Add(new Customer(customer));
customer.ID = 3;
customer.Name = "Bilal Ahmad";
customer.FatherName = "Kashif";
customer.Email = "bilal@gmail.com";
lstCustomer.Add(new Customer(customer));
customer.ID = 4;
customer.Name = "Chin Shang";
customer.FatherName = "Shang Woe";
customer.Email = "chinshang@gmail.com";
lstCustomer.Add(new Customer(customer));
return lstCustomer;
}
返回此列表以与网格绑定。代码是:
List<Customer> lstCustomer = new List<Customer>();
lstCustomer = Generate_Data();
GridView1.DataSource = lstCustomer;
GridView1.DataBind();
我的问题是:
我将 4 个文本框和一个按钮添加到带有名称的 aspx 页面:
Id,Name,FatherName,Email
当我单击按钮时,我想将文本框的新值添加到 gridview1 行。我想动态地向 gridview 添加一行。如果我定义了一个空的 gridview,我如何将我的文本框值添加到 gridview 行?不是与 question1 相等的方法吗?