将新行数据添加到 gridview asp.net c# [英] Add new row data to gridview asp.net c#

查看:24
本文介绍了将新行数据添加到 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();

我的问题是:

  1. 我在 aspx 页面中添加了 4 个文本框和一个按钮,名称为:Id,Name,FatherName,Email当我单击按钮时,我想将文本框的新值添加到 gridview1 行.我想动态地向 gridview 添加一行.

  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. I want to add a row to the gridview dynamically.

如果我定义了一个空的 gridview,我如何将我的文本框值添加到 gridview 行?是不是和 question1 一样的方法?

If I define an empty gridview, how can I add my textbox values to gridview rows? Is not equal method with question1 ?

推荐答案

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

背后的代码:

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

已更新!

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;

    }
}

这篇关于将新行数据添加到 gridview asp.net c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆