• 大小: 2KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-05-10
  • 语言: 其他
  • 标签:

资源简介

GridView动态增加行,非常好用。保证出效果

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender EventArgs e)
    {

        if (!Page.IsPostBack)
        {
            DataTable table = new DataTable();
            table.Columns.Add(new DataColumn(“ID“));
            table.Columns.Add(new DataColumn(“Name“));
            table.Columns.Add(new DataColumn(“UsuallyResults“));
            table.Columns.Add(new DataColumn(“ExamResults“));
            DataRow row = table.NewRow(); table.Rows.Add(row);
            GridView1.DataSource = table; GridView1.DataBind();
        }
    }
    protected void lbtnAddRow_Click(object sender EventArgs e)
    {
        DataTable table = GetGridViewData();
        DataRow newRow = table.NewRow();
        newRow[“ID“] = Guid.NewGuid().ToString();
        table.Rows.Add(newRow);
        GridView1.DataSource = table;
        GridView1.DataBind();
    }
    private DataTable GetGridViewData()
    {
        DataTable table = new DataTable();
        table.Columns.Add(new DataColumn(“ID“));
        table.Columns.Add(new DataColumn(“Name“));
        table.Columns.Add(new DataColumn(“UsuallyResults“));
        table.Columns.Add(new DataColumn(“ExamResults“));
        foreach (GridViewRow row in GridView1.Rows)
        {
            DataRow sourseRow = table.NewRow();
            sourseRow[“ID“] = row.Cells[0].Text;
            sourseRow[“Name“] = ((TextBox)row.Cells[3].FindControl(“txtName“)).Text;
            sourseRow[“UsuallyResults“] = ((TextBox)row.Cells[4].FindControl(“txtUsuallyResults“)).Text;
            sourseRow[“ExamResults“] = ((TextBox)row.Cells[5].FindControl(“txtExamResults“)).Text;
            table.Rows.Add(sourseRow);
        }
        return table;
    }
    protected void btnDeleteRow_Click(object sender EventArgs e)
    {
        DataTable table = GetGridViewData();
        foreach (GridViewRow row in GridView1.Rows)
        {
            if (((HtmlInputCheckBox)row.Cells[2].FindControl(“chkRow“)).Checked)
            {
                foreach (DataRow dtRow in table.Rows)
                {
                    if (dtRow[“ID“].ToString() == row.Cells[0].Text)
                    {
                        table.Rows.Remove(dtRow);
                        break;
                    }
                }
            }
        }
        GridView1.DataSource = table;
        GridView1.DataBind();
    }
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       4493  2014-01-10 09:39  GridView动态增加行\Default.aspx

     文件       2632  2014-01-10 09:39  GridView动态增加行\Default.aspx.cs

     目录          0  2014-01-10 09:51  GridView动态增加行

----------- ---------  ---------- -----  ----

                 7125                    3


评论

共有 条评论

相关资源