如何避免更换 [英] how to avoid replacement

查看:85
本文介绍了如何避免更换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码如下附上



code is below attached

if (Session["Data"] == null)

       {

           C = 1;

                dt = new DataTable();
               DataRow dr = dt.NewRow();
               dt.Columns.Add(new System.Data.DataColumn("EMPNAME", typeof(String)));
               dt.Columns.Add(new System.Data.DataColumn("EMPID", typeof(String)));
               dt.Columns.Add(new System.Data.DataColumn("DEPARTMENT", typeof(String)));
               dr[0] = Txtaddname.Text;
               dr[1] = Txtaddempid.Text;
               dr[2] = Drpadddept.SelectedItem.Text;
               dt.Rows.Add(dr);
               GridView1.DataSource = dt;
               GridView1.DataBind();
               Session["Data"] = dt;


           }
           else
           {
               C = C + 1;

              dt = new DataTable();
               DataRow dr = dt.NewRow();
               dt.Columns.Add(new System.Data.DataColumn("EMPNAME", typeof(String)));
               dt.Columns.Add(new System.Data.DataColumn("EMPID", typeof(String)));
               dt.Columns.Add(new System.Data.DataColumn("DEPARTMENT", typeof(String)));
               dr[0] = Txtaddname.Text;
               dr[1] = Txtaddempid.Text;
               dr[2] = Drpadddept.SelectedItem.Text;
               dt.Rows.Add(dr);
               GridView1.DataSource = dt;
               GridView1.DataBind();
               Session["Data"] = dt;

推荐答案

// create table outside of the loop, at Session start if you need it that long or at page load if not.
DataTable dt = new DataTable
dt.Columns.Add(new System.Data.DataColumn("EMPNAME", typeof(String)));
dt.Columns.Add(new System.Data.DataColumn("EMPID", typeof(String)));
dt.Columns.Add(new System.Data.DataColumn("DEPARTMENT", typeof(String)));

Session["Data"] = dt; // assign empty table to session
int C = 0; // also not needed in the code

// this code then goes at the point where you're adding data to the gridview (some button handler?
DataTable tmp = CType(Session["Data"], DataTable);// you know it exists (although another check won't hurt
DataRow dr =  tmp.NewRow; 
dr["EMPNAME"] = Txtaddname.Text;
dr["EMPID"] = Txtaddempid.Text;
dr["DEPARTMENT"] = Drpadddept.SelectedItem.Text; // use column names instead of indexes, that way if / when  you add new columns you don't have to change anything

gvEmployees.DataSource = tmp; // it is good practice to give all your controls meaningful names, not leave them at default, otherwise you'll be asking yourself what is in TextBox11 or what does Button44 do in couple of months :)
gvEmployees.DataBind();
Session["Data"] = tmp; // I don't think this is needed anymore





除此之外,你可以使用DataTable.Merge方法将你的单行表(你的方式)添加到gridview数据源。



Alternative to this, you could use DataTable.Merge method to add your single row table (the way you're doing it) to gridview datasource.


这篇关于如何避免更换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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