新行的数据添加到GridView控件asp.net C# [英] Add new row data to gridview asp.net c#

查看:94
本文介绍了新行的数据添加到GridView控件asp.net C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个类与此code:

 公共类客户
{
    公众客户(){}
    公众客户(客户卡斯特)
    {
        ID = cust.ID;
        NAME = cust.Name;
        FatherName = cust.FatherName;
        电子邮件= cust.Email;
    }
    公众诠释ID {搞定;组; }
    公共字符串名称{;组; }
    公共字符串FatherName {搞定;组; }
    公共字符串电子邮件{获得;组; }
}

和创造了这个函数来加载一些数据列表:

 公开名单<客户> Generate_Data()
{
    清单<客户> lstCustomer =新的List<客户>();
    客户客户=新客户();    customer.ID = 1;
    customer.Name =约翰Cena
    customer.FatherName =约翰;
    customer.Email =cena@gmail.com;
    lstCustomer.Add(新客户(客户));    customer.ID = 2;
    customer.Name =Mokesh;
    customer.FatherName =Rajnikant;
    customer.Email =mokesh@gmail.com;
    lstCustomer.Add(新客户(客户));    customer.ID = 3;
    customer.Name =比拉尔·艾哈迈德;
    customer.FatherName =Kashif;
    customer.Email =bilal@gmail.com;
    lstCustomer.Add(新客户(客户));    customer.ID = 4;
    customer.Name =钦尚书;
    customer.FatherName =尚祸;
    customer.Email =chinshang@gmail.com;
    lstCustomer.Add(新客户(客户));    返回lstCustomer;
}

返回此列表与电网结合。在code是:

 列表<客户> lstCustomer =新的List<客户>();
lstCustomer = Generate_Data();
GridView1.DataSource = lstCustomer;
GridView1.DataBind();

我的问题是:


  1. 我加4文本框和一个按钮的名称aspx页: ID,名称,FatherName,电子邮件
    当我按一下按钮,我想补充文本框的新值gridview1一行。我想添加一行来动态GridView的。


  2. 如果我定义一个空的GridView,我怎么能加我的文本框值gridview的行?不等于法问题1?



解决方案

ASPX:

 < ASP:文本框ID =TextBox1的=服务器>< / ASP:文本框>< BR />
< ASP:文本框ID =TextBox2中=服务器>< / ASP:文本框>< BR />
< ASP:文本框ID =TextBox3=服务器>< / ASP:文本框>< BR />
< ASP:文本框ID =TextBox4=服务器>< / ASP:文本框>< BR />
< ASP:按钮的ID =Button1的=服务器的onclick =的button1_Click文本=按钮/>
< ASP:GridView控件ID =GridView1=服务器/>

背后code:

 公共类客户
{
    公众客户(){}
    公众客户(客户卡斯特)
    {
        ID = cust.ID;
        NAME = cust.Name;
        FatherName = cust.FatherName;
        电子邮件= cust.Email;
    }
    公众诠释ID {搞定;组; }
    公共字符串名称{;组; }
    公共字符串FatherName {搞定;组; }
    公共字符串电子邮件{获得;组; }
}
保护无效的button1_Click(对象发件人,EventArgs的发送)
{
    清单<客户> lstCustomer =新的List<客户>();
    如果(会话[DT]!= NULL)
    {
      lstCustomer =(列表<客户>)会议[DT];
    }
    客户客户=新客户();
    customer.ID = int.Parse(TextBox1.Text);
    customer.Name = TextBox2.Text;
    customer.FatherName = TextBox2.Text;
    customer.Email = TextBox2.Text;
    lstCustomer.Add(新客户(客户));
    GridView1.DataSource = lstCustomer;
    GridView1.DataBind();
    会话[DT] = lstCustomer;
}

已更新!

 保护无效的Page_Load(对象发件人,EventArgs的发送)
{
    如果(!的IsPostBack)
    {
        清单<客户> lstCustomer =新的List<客户>();
        客户客户=新客户();        customer.ID = 1;
        customer.Name =约翰Cena
        customer.FatherName =约翰;
        customer.Email =cena@gmail.com;
        lstCustomer.Add(新客户(客户));        customer.ID = 2;
        customer.Name =Mokesh;
        customer.FatherName =Rajnikant;
        customer.Email =mokesh@gmail.com;
        lstCustomer.Add(新客户(客户));        customer.ID = 3;
        customer.Name =比拉尔·艾哈迈德;
        customer.FatherName =Kashif;
        customer.Email =bilal@gmail.com;
        lstCustomer.Add(新客户(客户));        customer.ID = 4;
        customer.Name =钦尚书;
        customer.FatherName =尚祸;
        customer.Email =chinshang@gmail.com;
        lstCustomer.Add(新客户(客户));
        会话[DT] = lstCustomer;    }
}

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

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

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;

    }
}

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

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