protected void Page_Load(object sender, EventArgs e)
{
table_black.Visible = false;
}
protected void Button1_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
DataColumn bianhao1 = new DataColumn("bianhao", Type.GetType("System.String"));
DataColumn mingcheng1 = new DataColumn("mingcheng", Type.GetType("System.String"));
DataColumn yanse1 = new DataColumn("yanse", Type.GetType("System.String"));
DataColumn chima1 = new DataColumn("chima", Type.GetType("System.String"));
DataColumn shuliang1 = new DataColumn("shuliang", Type.GetType("System.String"));
dt.Columns.Add(bianhao1);
dt.Columns.Add(mingcheng1);
dt.Columns.Add(yanse1);
dt.Columns.Add(chima1);
dt.Columns.Add(shuliang1); DataRow _newRow = dt.NewRow();
_newRow["bianhao"] = bianhao.Text.ToString();
_newRow["mingcheng"] = bianhao.Text.ToString();
_newRow["yanse"] = bianhao.Text.ToString();
_newRow["chima"] = bianhao.Text.ToString();
_newRow["shuliang"] = bianhao.Text.ToString();
dt.Rows.Add(_newRow);
GridView1.DataSource = dt;
GridView1.DataBind();
table_black.Visible = Visible;
}
{
table_black.Visible = false;
}
protected void Button1_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
DataColumn bianhao1 = new DataColumn("bianhao", Type.GetType("System.String"));
DataColumn mingcheng1 = new DataColumn("mingcheng", Type.GetType("System.String"));
DataColumn yanse1 = new DataColumn("yanse", Type.GetType("System.String"));
DataColumn chima1 = new DataColumn("chima", Type.GetType("System.String"));
DataColumn shuliang1 = new DataColumn("shuliang", Type.GetType("System.String"));
dt.Columns.Add(bianhao1);
dt.Columns.Add(mingcheng1);
dt.Columns.Add(yanse1);
dt.Columns.Add(chima1);
dt.Columns.Add(shuliang1); DataRow _newRow = dt.NewRow();
_newRow["bianhao"] = bianhao.Text.ToString();
_newRow["mingcheng"] = bianhao.Text.ToString();
_newRow["yanse"] = bianhao.Text.ToString();
_newRow["chima"] = bianhao.Text.ToString();
_newRow["shuliang"] = bianhao.Text.ToString();
dt.Rows.Add(_newRow);
GridView1.DataSource = dt;
GridView1.DataBind();
table_black.Visible = Visible;
}
创建一个新DataTable ,行数是0 ,然后dt.Rows.Add(_newRow);
添加了一行,所以总是添加一行。把DataTable dt 声明为全局变量就可以了
{
table_black.Visible = false;
}
protected void Button1_Click(object sender, EventArgs e)
{
DataColumn bianhao1 = new DataColumn("bianhao", Type.GetType("System.String"));
DataColumn mingcheng1 = new DataColumn("mingcheng", Type.GetType("System.String"));
DataColumn yanse1 = new DataColumn("yanse", Type.GetType("System.String"));
DataColumn chima1 = new DataColumn("chima", Type.GetType("System.String"));
DataColumn shuliang1 = new DataColumn("shuliang", Type.GetType("System.String"));
dt.Columns.Add(bianhao1);
dt.Columns.Add(mingcheng1);
dt.Columns.Add(yanse1);
dt.Columns.Add(chima1);
dt.Columns.Add(shuliang1); DataRow _newRow = dt.NewRow();
_newRow["bianhao"] = bianhao.Text.ToString();
_newRow["mingcheng"] = bianhao.Text.ToString();
_newRow["yanse"] = bianhao.Text.ToString();
_newRow["chima"] = bianhao.Text.ToString();
_newRow["shuliang"] = bianhao.Text.ToString();
dt.Rows.Add(_newRow);
GridView1.DataSource = dt;
GridView1.DataBind();
table_black.Visible = Visible;
}
if(dt==null) dt = new DateTable();在button里这么声明就可以了
{
DataTable dt = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
table_black.Visible = false;
DataColumn bianhao1 = new DataColumn("bianhao", Type.GetType("System.String"));
DataColumn mingcheng1 = new DataColumn("mingcheng", Type.GetType("System.String"));
DataColumn yanse1 = new DataColumn("yanse", Type.GetType("System.String"));
DataColumn chima1 = new DataColumn("chima", Type.GetType("System.String"));
DataColumn shuliang1 = new DataColumn("shuliang", Type.GetType("System.String"));
dt.Columns.Add(bianhao1);
dt.Columns.Add(mingcheng1);
dt.Columns.Add(yanse1);
dt.Columns.Add(chima1);
dt.Columns.Add(shuliang1);
}
protected void Button1_Click(object sender, EventArgs e)
{
DataRow _newRow = dt.NewRow();
_newRow["bianhao"] = bianhao.Text.ToString();
_newRow["mingcheng"] = bianhao.Text.ToString();
_newRow["yanse"] = bianhao.Text.ToString();
_newRow["chima"] = bianhao.Text.ToString();
_newRow["shuliang"] = bianhao.Text.ToString();
dt.Rows.Add(_newRow);
GridView1.DataSource = dt;
GridView1.DataBind();
table_black.Visible = Visible;
}
}
你用10楼的方法就好了
if(!IsPostBck)
{
//定义列
}
{
protected void Page_Load(object sender, EventArgs e)
{
table_black.Visible = false;
}
protected void Button1_Click(object sender, EventArgs e)
{
DataTable dt = (DataTable)GridView1.DataSource;
if (dt == null)
{
dt = new DataTable();
DataColumn bianhao1 = new DataColumn("bianhao", Type.GetType("System.String"));
DataColumn mingcheng1 = new DataColumn("mingcheng", Type.GetType("System.String"));
DataColumn yanse1 = new DataColumn("yanse", Type.GetType("System.String"));
DataColumn chima1 = new DataColumn("chima", Type.GetType("System.String"));
DataColumn shuliang1 = new DataColumn("shuliang", Type.GetType("System.String"));
dt.Columns.Add(bianhao1);
dt.Columns.Add(mingcheng1);
dt.Columns.Add(yanse1);
dt.Columns.Add(chima1);
dt.Columns.Add(shuliang1);
}
DataRow _newRow = dt.NewRow();
_newRow["bianhao"] = bianhao.Text.ToString();
_newRow["mingcheng"] = bianhao.Text.ToString();
_newRow["yanse"] = bianhao.Text.ToString();
_newRow["chima"] = bianhao.Text.ToString();
_newRow["shuliang"] = bianhao.Text.ToString();
dt.Rows.Add(_newRow);
GridView1.DataSource = dt;
GridView1.DataBind();
table_black.Visible = Visible;
}
}
每次都走DataTable dt = new DataTable();
你放到缓存里吧
{
if (!IsPostBack)
{
DataColumn bianhao1 = new DataColumn("bianhao", Type.GetType("System.String"));
DataColumn mingcheng1 = new DataColumn("mingcheng", Type.GetType("System.String"));
DataColumn yanse1 = new DataColumn("yanse", Type.GetType("System.String"));
DataColumn chima1 = new DataColumn("chima", Type.GetType("System.String"));
DataColumn shuliang1 = new DataColumn("shuliang", Type.GetType("System.String"));
db.dt.Columns.Add(bianhao1);
db.dt.Columns.Add(mingcheng1);
db.dt.Columns.Add(yanse1);
db.dt.Columns.Add(chima1);
db.dt.Columns.Add(shuliang1);
}
}
protected void Button1_Click(object sender, EventArgs e)
{
DataRow _newRow = db.dt.NewRow();
_newRow["bianhao"] = 2.ToString();
_newRow["mingcheng"] = 3.ToString();
_newRow["yanse"] = 6.ToString();
_newRow["chima"] = 7.ToString();
_newRow["shuliang"] = 8.ToString();
db.dt.Rows.Add(_newRow);
GridView1.DataSource = db.dt;
GridView1.DataBind();
}
这是代码,就是你原来的代码,稍加改动
你在项目里新加一个类,类里面写一句代码
public class db
{
public static DataTable dt = new DataTable();
}
就可以实现了应该还有更好的方法
public Form2()
{
InitializeComponent();
} private DataTable dt = new DataTable();
private void Form2_Paint(object sender, PaintEventArgs e)
{
dt.Columns.Add(new DataColumn("列1"));
dt.Columns.Add(new DataColumn("列2")); } private void button1_Click(object sender, EventArgs e)
{
DataRow dr = dt.NewRow();
dt.Rows.Add(dr); dt.Rows[dt.Rows.Count - 1]["列1"] = "111";
dt.Rows[dt.Rows.Count - 1]["列2"] = "111"; dataGridView1.DataSource = dt; }
}
private DataTable dt = new DataTable();
private void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
dt.Columns.Add(new DataColumn("列1"));
dt.Columns.Add(new DataColumn("列2"));
}
}
private void button1_Click(object sender, EventArgs e)
{
DataRow dr = dt.NewRow();
dt.Rows.Add(dr); dt.Rows[dt.Rows.Count - 1]["列1"] = "111";
dt.Rows[dt.Rows.Count - 1]["列2"] = "111"; dataGridView1.DataSource = dt; }/*上面搞错了............我是在winform中测试的 妈的一直Server is too busy 不要我发了*/
protected void Button1_Click(object sender, EventArgs e)
{
DataTable dt = null;
if(Session["db"] == null)
{
dt = new DataTable();
DataColumn bianhao1 = new DataColumn("bianhao", Type.GetType("System.String"));
DataColumn mingcheng1 = new DataColumn("mingcheng", Type.GetType("System.String"));
DataColumn yanse1 = new DataColumn("yanse", Type.GetType("System.String"));
DataColumn chima1 = new DataColumn("chima", Type.GetType("System.String"));
DataColumn shuliang1 = new DataColumn("shuliang", Type.GetType("System.String"));
dt.Columns.Add(bianhao1);
dt.Columns.Add(mingcheng1);
dt.Columns.Add(yanse1);
dt.Columns.Add(chima1);
dt.Columns.Add(shuliang1); }
else
{
dt = (DataTable)Session["db"];
}
DataRow _newRow = dt.NewRow();
_newRow["bianhao"] = bianhao.Text.ToString();
_newRow["mingcheng"] = bianhao.Text.ToString();
_newRow["yanse"] = bianhao.Text.ToString();
_newRow["chima"] = bianhao.Text.ToString();
_newRow["shuliang"] = bianhao.Text.ToString();
dt.Rows.Add(_newRow);
GridView1.DataSource = dt;
GridView1.DataBind();
table_black.Visible = Visible;
}
private void comboBox1_SelectionChangeCommitted(object sender, EventArgs e)
{ NewMethod(Convert.ToString(this.comboBox1.SelectedValue));
this.dataGridView1.DataSource = dt;
} private void NewMethod(string enter)
{
if (dt.Columns.Count == 0)
{
dt.Columns.Add("Id", typeof(System.Int32));
dt.Columns.Add("入库单号", typeof(System.String));
}
DataRow dr = dt.NewRow();
if (textBox2.Text.Trim() == "")
{
dr[0] = 1;
dr[1] = Convert.ToInt64(enter);
textBox2.Text = "1";
dt.Rows.Add(dr);
}
else
{
dr[0] = Convert.ToInt32(textBox2.Text) + 1;
dr[1] = Convert.ToInt64(enter);
textBox2.Text = Convert.ToString(Convert.ToInt32(textBox2.Text) + 1);
dt.Rows.Add(dr);
}
}
得再加个 static 才行哦。public static DataTable dt = new DataTable()
DataTable dt = new DataTable();
DataColumn bianhao1 = new DataColumn("bianhao", Type.GetType("System.String"));
DataColumn mingcheng1 = new DataColumn("mingcheng", Type.GetType("System.String"));
DataColumn yanse1 = new DataColumn("yanse", Type.GetType("System.String"));
DataColumn chima1 = new DataColumn("chima", Type.GetType("System.String"));
DataColumn shuliang1 = new DataColumn("shuliang", Type.GetType("System.String"));
dt.Columns.Add(bianhao1);
dt.Columns.Add(mingcheng1);
dt.Columns.Add(yanse1);
dt.Columns.Add(chima1);
dt.Columns.Add(shuliang1);
//把里面的DataTable dt = new DataTable();放到外面就行了,不然你每次点击按钮的话它都重新创建为0了,怎么可能一直加上去
{
if (!IsPostBack)
{
table_black.Visible = false;
}
}
将!ispostback加上试试
{
dataTable dt=null;
protected void Page_Load(object sender, EventArgs e)
{
table_black.Visible = false;
if(viewstate["table"]!=null)
{
dt=(DataTable)viewstate["table"];
} }
protected void Button1_Click(object sender, EventArgs e)
{
if (dt == null)
{
dt = new DataTable();
DataColumn bianhao1 = new DataColumn("bianhao", Type.GetType("System.String"));
DataColumn mingcheng1 = new DataColumn("mingcheng", Type.GetType("System.String"));
DataColumn yanse1 = new DataColumn("yanse", Type.GetType("System.String"));
DataColumn chima1 = new DataColumn("chima", Type.GetType("System.String"));
DataColumn shuliang1 = new DataColumn("shuliang", Type.GetType("System.String"));
dt.Columns.Add(bianhao1);
dt.Columns.Add(mingcheng1);
dt.Columns.Add(yanse1);
dt.Columns.Add(chima1);
dt.Columns.Add(shuliang1);
}
DataRow _newRow = dt.NewRow();
_newRow["bianhao"] = bianhao.Text.ToString();
_newRow["mingcheng"] = bianhao.Text.ToString();
_newRow["yanse"] = bianhao.Text.ToString();
_newRow["chima"] = bianhao.Text.ToString();
_newRow["shuliang"] = bianhao.Text.ToString();
dt.Rows.Add(_newRow);
GridView1.DataSource = dt;
GridView1.DataBind();
viewstate["table"]=dt; table_black.Visible = Visible;
}
}
再回来就是第一次点击了、
static DataTable dt=new DataTble();
TableAdapter对象有一个clearbeforefill的属性,默认值是true.
设置为false后即可追加填充了。