如何动态地添加在GridView的一排? [英] how to add a row dynamically in gridview?

查看:94
本文介绍了如何动态地添加在GridView的一排?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够在GridView通过以下code动态添加行,但我需要做同样的被捕获,并在应用程序每次显示时我打开与记录我加入$ P中的应用$ pviously在GridView。
有任何想法吗??

编码:

 ![此处输入的形象描述] [1]保护无效的Page_Load(对象发件人,EventArgs的发送)
    {
        如果(!Page.IsPostBack)
        {
            SetInitialRow();
        }
    }    私人无效SetInitialRow()
    {
        DataTable的DT =新的DataTable();
        医生的DataRow = NULL;
       dt.Columns.Add(新的DataColumn(ROWNUMBER,typeof运算(字符串)));
        dt.Columns.Add(新的DataColumn(列1,typeof运算(字符串)));
        dt.Columns.Add(新的DataColumn(列2中的typeof(字符串)));
        dt.Columns.Add(新的DataColumn(栏3中的typeof(字符串)));
        博士= dt.NewRow();
       博士[ROWNUMBER] = 1;
        博士[列1] =的String.Empty;
        博士[列2] =的String.Empty;
        博士[栏3] =的String.Empty;
        dt.Rows.Add(DR);
        博士= dt.NewRow();        //存储在ViewState中的数据表
    //的ViewState [CurrentTable] = DT;
        应用[CurrentTable] = DT;
        Gridview1.DataSource = DT;
        Gridview1.DataBind();
    }
    私人无效AddNewRowToGrid()
    {
        INT的rowIndex = 0;       //如果(的ViewState [CurrentTable]!= NULL)
        如果(应用[CurrentTable]!= NULL)
        {
            //数据表dtCurrentTable =(数据表)的ViewState [CurrentTable];
            数据表dtCurrentTable1 =(数据表)应用[CurrentTable];
            DataRow的drCurrentRow = NULL;
            如果(dtCurrentTable1.Rows.Count大于0)
            {
                的for(int i = 1; I< = dtCurrentTable1.Rows.Count;我++)
                {
                    //提取文本框值
                    文本框BOX1 =(文本框)Gridview1.Rows [rowIndex位置] .Cells [1] .FindControl(TextBox1中);
                    文本框BOX2 =(文本框)Gridview1.Rows [rowIndex位置] .Cells [2] .FindControl(TextBox2中);
                    文本框BOX3 =(文本框)Gridview1.Rows [rowIndex位置] .Cells [3] .FindControl(TextBox3);                    drCurrentRow = dtCurrentTable1.NewRow();
                   drCurrentRow [ROWNUMBER] = I + 1;                    /*dtCurrentTable.Rows[i - 1] [列1] = box1.Text;
                    dtCurrentTable.Rows [我 - 1] [列2] = box2.Text;
                    dtCurrentTable.Rows [我 - 1] [栏3] = box3.Text; * /
                    dtCurrentTable1.Rows [我 - 1] [列1] = box1.Text;
                    dtCurrentTable1.Rows [我 - 1] [列2] = box2.Text;
                    dtCurrentTable1.Rows [我 - 1] [栏3] = box3.Text;                    rowIndex位置++;
                }
                dtCurrentTable1.Rows.Add(drCurrentRow);
             //的ViewState [CurrentTable] = dtCurrentTable;
                应用[CurrentTable] = dtCurrentTable1;
                Gridview1.DataSource = dtCurrentTable1;
                Gridview1.DataBind();
            }
        }
        其他
        {
            的Response.Write(ViewState中为空);
        }        上回发//设置previous数据
        设置previousData();
    }
    私人无效套装previousData()
    {
        INT的rowIndex = 0;
        //如果(的ViewState [CurrentTable]!= NULL)
        如果(应用[CurrentTable]!= NULL)
        {
            // DataTable的DT =(数据表)的ViewState [CurrentTable];
            DataTable的DT1 =(数据表)应用[CurrentTable];
            如果(dt1.Rows.Count大于0)
            {
                的for(int i = 0; I< dt1.Rows.Count;我++)
                {
                    文本框BOX1 =(文本框)Gridview1.Rows [rowIndex位置] .Cells [1] .FindControl(TextBox1中);
                    文本框BOX2 =(文本框)Gridview1.Rows [rowIndex位置] .Cells [2] .FindControl(TextBox2中);
                    文本框BOX3 =(文本框)Gridview1.Rows [rowIndex位置] .Cells [3] .FindControl(TextBox3);                    /*box1.Text = dt.Rows [I] [列1]的ToString()。
                    box2.Text = dt.Rows [I] [列2]的ToString()。
                    。box3.Text = dt.Rows [I] [栏3]的ToString(); * /
                    box1.Text = dt1.Rows [I] [列1]的ToString()。
                    box2.Text = dt1.Rows [I] [列2]的ToString()。
                    box3.Text = dt1.Rows [I] [栏3]的ToString()。
                    rowIndex位置++;
                }
            }
        }
    }    保护无效ButtonAdd_Click(对象发件人,EventArgs的发送)
    {
        AddNewRowToGrid();
    }
}


解决方案

由于您的问题和你谈话的应用程序打开时左右,你需要做的是有一个方法来保存数据网格视图某处,即数据库,文本文件等。

这是因为,当应用程序被关闭的应用程序变量丢失,所以要重新打开在最后一点上,你需要阅读您在数据库中保存的数据或者一个文本文件,并重新创建DataTable对象从。

I am able to add a row dynamically in a GridView through the following code but I need to do the same to be captured and displayed in Application every time when I open the application with the records I have added previously in the GridView. Any Ideas??

Coding:

    ![enter image description here][1] protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            SetInitialRow();
        }
    }

    private void SetInitialRow()
    {
        DataTable dt = new DataTable();
        DataRow dr = null;
       dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
        dt.Columns.Add(new DataColumn("Column1", typeof(string)));
        dt.Columns.Add(new DataColumn("Column2", typeof(string)));
        dt.Columns.Add(new DataColumn("Column3", typeof(string)));
        dr = dt.NewRow();
       dr["RowNumber"] = 1;
        dr["Column1"] = string.Empty;
        dr["Column2"] = string.Empty;
        dr["Column3"] = string.Empty;
        dt.Rows.Add(dr);
        dr = dt.NewRow();

        //Store the DataTable in ViewState
    // ViewState["CurrentTable"] = dt;
        Application["CurrentTable"]=dt;
        Gridview1.DataSource = dt;
        Gridview1.DataBind();
    }
    private void AddNewRowToGrid()
    {
        int rowIndex = 0;

       //if (ViewState["CurrentTable"] != null)
        if (Application["CurrentTable"] != null)
        {
            //DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
            DataTable dtCurrentTable1 = (DataTable)Application["CurrentTable"];
            DataRow drCurrentRow = null;
            if (dtCurrentTable1.Rows.Count > 0)
            {
                for (int i = 1; i <= dtCurrentTable1.Rows.Count; i++)
                {
                    //extract the TextBox values
                    TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("TextBox1");
                    TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
                    TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox3");

                    drCurrentRow = dtCurrentTable1.NewRow();
                   drCurrentRow["RowNumber"] = i + 1;

                    /*dtCurrentTable.Rows[i - 1]["Column1"] = box1.Text;
                    dtCurrentTable.Rows[i - 1]["Column2"] = box2.Text;
                    dtCurrentTable.Rows[i - 1]["Column3"] = box3.Text;*/
                    dtCurrentTable1.Rows[i - 1]["Column1"] = box1.Text;
                    dtCurrentTable1.Rows[i - 1]["Column2"] = box2.Text;
                    dtCurrentTable1.Rows[i - 1]["Column3"] = box3.Text;

                    rowIndex++;
                }
                dtCurrentTable1.Rows.Add(drCurrentRow);
             //ViewState["CurrentTable"] = dtCurrentTable;
                Application["CurrentTable"] = dtCurrentTable1;
                Gridview1.DataSource = dtCurrentTable1;
                Gridview1.DataBind();
            }
        }
        else
        {
            Response.Write("ViewState is null");
        }

        //Set Previous Data on Postbacks
        SetPreviousData();
    }
    private void SetPreviousData()
    {
        int rowIndex = 0;
        //if (ViewState["CurrentTable"] != null)
        if (Application["CurrentTable"] != null)
        {
            //DataTable dt = (DataTable)ViewState["CurrentTable"];
            DataTable dt1 = (DataTable)Application["CurrentTable"];
            if (dt1.Rows.Count > 0)
            {
                for (int i = 0; i < dt1.Rows.Count; i++)
                {
                    TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("TextBox1");
                    TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
                    TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox3");

                    /*box1.Text = dt.Rows[i]["Column1"].ToString();
                    box2.Text = dt.Rows[i]["Column2"].ToString();
                    box3.Text = dt.Rows[i]["Column3"].ToString();*/
                    box1.Text = dt1.Rows[i]["Column1"].ToString();
                    box2.Text = dt1.Rows[i]["Column2"].ToString();
                    box3.Text = dt1.Rows[i]["Column3"].ToString();
                    rowIndex++;
                }
            }
        }
    }

    protected void ButtonAdd_Click(object sender, EventArgs e)
    {
        AddNewRowToGrid();
    }
}

解决方案

Since in your question you are talking about when the application is opened, what you will need to do is have a method to save the data in the grid view somewhere, i.e. Database, Text File etc.

This is because the Application variable is lost when the application is closed, so to re-open at the last point you will need to read the data you saved either in a Database or a Text file and re-create the DataTable object from that.

这篇关于如何动态地添加在GridView的一排?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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