窗口形式,数据网格视图 [英] window form ,datagrid view
本文介绍了窗口形式,数据网格视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个Form
,它正在计算和显示我正在使用的TexBox
es中的数据,用于进一步计算,现在在保存Button
Click
上,我有这段书面代码:
I have a Form
which is calculating and displaying data in TexBox
es which I''m using for further calculation, now on Save Button
Click
I have this written code:
DataTable dt = new DataTable();
DataRow drow = dt.NewRow();
DataColumn dc = new DataColumn("X", typeof(System.String));
DataColumn dc1 = new DataColumn("Y", typeof(System.String));
DataColumn dc2 = new DataColumn("Z", typeof(System.String));
dt.Columns.Add(dc);
dt.Columns.Add(dc1);
dt.Columns.Add(dc2);
string[] rows = { "" + textBox10.Text, "" + textBox11.Text, "" + textBox12.Text };
dt.Rows.Add(rows);
dataGridView1.DataSource = dt;
一切正常,只是我希望每次将值保存到新行时都保存而不覆盖先前的行,如:
r1 11 111 111
r2 22 11 1111
...
etc
It is working fine except that I want on save each time value gets saved to new row without overwriting the previous like:
r1 11 111 111
r2 22 11 1111
...
etc
推荐答案
为此,您必须在每次保存单击时在同一表上创建新行".
在这里,您每次单击都将创建一个新表,新表将始终只有1条记录.
仅创建一次表,然后在每次单击时向其中添加行. :)
For that you have to "create new row on same table" on each save click.
Here, you are creating new table on every click, new table will always have only 1 record.
Create table only once and then add rows to it on every click. :)
我认为您已经在保存按钮点击中编写了完整的代码,您应该按照以下方式定义代码
1. gollbally定义此变量
DataTable dt;
2.在form_Load事件中
dt = new DataTable();< br/>
DataColumn dc =新的DataColumn("X",typeof(System.String));
DataColumn dc1 =新的DataColumn("Y",typeof(System.String));
DataColumn dc2 =新的DataColumn("Z",typeof(System.String));
dt.Columns.Add(dc);
dt.Columns.Add(dc1);
dt.Columns.Add(dc2);
3.在保存"按钮中单击
DataRow drow = dt.NewRow();
Hi,
I think you had written whole code inside Save button click, you should have to define code on following manner
1. define this variable gollbally
DataTable dt;
2. in form_Load event
dt = new DataTable();<br/>
DataColumn dc = new DataColumn("X", typeof(System.String));
DataColumn dc1 = new DataColumn("Y", typeof(System.String));
DataColumn dc2 = new DataColumn("Z", typeof(System.String));
dt.Columns.Add(dc);
dt.Columns.Add(dc1);
dt.Columns.Add(dc2);
3. in Save button click
DataRow drow = dt.NewRow();
string[] rows = { "" + textBox10.Text, "" + textBox11.Text, "" + textBox12.Text };
dt.Rows.Add(rows);
BindGrd();
4. BindGrid()
公共无效dataGridView1.DataSource = dt;
BindGrd();
4. BindGrid()
public void dataGridView1.DataSource = dt;
这篇关于窗口形式,数据网格视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文