简体   繁体   English

将新行数据添加到gridview asp.net c#

[英]Add new row data to gridview asp.net c#

I've made a class with this code : 我用这段代码创建了一个类:

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; }
}

and created this function to load list with some data: 并创建此函数以加载包含一些数据的列表:

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;
}

returning this list to bind with the grid. 返回此列表以与网格绑定。 The code is : 代码是:

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

My questions are : 我的问题是:

  1. I added 4 textboxes and a button to an aspx page with the names: Id,Name,FatherName,Email When I click on the button, I want add the new values of the textboxes to gridview1 row. 我向aspx页面添加了4个文本框和一个按钮,其名称为: Id,Name,FatherName,Email当我点击按钮时,我想将文本框的新值添加到gridview1行。 I want to add a row to the gridview dynamically. 我想动态地向gridview添加一行。

  2. If I define an empty gridview, how can I add my textbox values to gridview rows? 如果我定义一个空的gridview,我如何将我的文本框值添加到gridview行? Is not equal method with question1 ? 问题1的方法不一样吗?

ASPX: 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"/>

Code behind: 代码背后:

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;
}

Updated! 更新!

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 = "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));
        Session["dt"] = lstCustomer;

    }
}

You will need to persist your collection during postback. 您需要在回发期间保留您的收藏。 Depending on how big your collection is you can consider keeping it in viewstate, session, etc. 根据您的收藏量有多大,您可以考虑将其保存在视图状态,会话等中。

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

So when the user fills the textboxes with data and initiates a postback, you can create a new customer object and add it to the existing collection. 因此,当用户使用数据填充文本框并启动回发时,您可以创建新的客户对象并将其添加到现有集合中。 Then rebind the data to the grid. 然后将数据重新绑定到网格。

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);

Note you will need to add the [Serializable] attribute to your Customer class. 请注意,您需要将[Serializable]属性添加到Customer类。

您可以简单地将AutoGenerateInsertButton="True"添加到<asp:GridView>标记。

So, I'd do it like this. 所以,我会这样做。 Firstly I'd extend the scope of lstCustomer so that it persists through postbacks. 首先,我将扩展lstCustomer的范围,以便它通过回发持续存在。

Then in the button event handler, I'd write something like this 然后在按钮事件处理程序中,我会写这样的东西

//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();

This also helps if you want to let the user perform some things on the data (or you want to persist to the database or something). 如果您想让用户对数据执行某些操作(或者您希望持久保存到数据库或其他内容),这也会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM