使用foreach语句将文本框控件的数据添加到datagridview [英] Adding data of textbox control to datagridview by using foreach statement
本文介绍了使用foreach语句将文本框控件的数据添加到datagridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
亲爱的先生,在我的Windows窗体中,我使用文本框将数据添加到datagridview。数据将填入第一行,同时数据将填入第二行。我正在尝试为它编写一些代码,但没有成功。我忘记了序列。我的代码是
Dear Sir, In my windows form I used textboxes to add data to datagridview. Data will be filled in first row, in the same time again data will be filled in the second row. I am trying to write some codes for it but not succeeded. I have forgot the sequence. my codes are
public partial class DVdata : Form
{
public DVdata()
{
InitializeComponent();
}
DataTable dt = new DataTable();
DataRow dr;
private void button1_Click(object sender, EventArgs e)
{
dt.Columns.Add("Column1", typeof(int));
dt.Columns.Add("Column2", typeof(string));
dt.Columns.Add("Column3", typeof(string));
dt.Columns["Column1"].AutoIncrement = true;
dt.Columns["Column1"].AutoIncrementSeed = 1;
dt.Clear();
dataGridView1.DataSource = dt;
foreach (DataRow row in dt.Rows)
{
dt.Rows.Add(null, row["Column2"].ToString(), row["Column2"].ToString());
}
dt.Rows.Add(null, textBox1.Text, textBox2.Text);
dataGridView1.DataSource = dt;
}
public void DataBind()
{
DataTable dt = new DataTable();
dt.Columns.Add("Column1", typeof(int));
dt.Columns.Add("Column2", typeof(string));
dt.Columns.Add("Column3", typeof(string));
dt.Clear();
dataGridView1.DataSource = dt;
}
}
}
我忘记了下一步了什么?
I forgot what next?
推荐答案
使用直接绑定,请参阅 http:// msdn。 microsoft.com/en-us/library/fbk67b6z(v=vs.90).aspx [ ^ ]。允许用户直接在datagridview中输入数据也可能更简单。
Use direct binding, see http://msdn.microsoft.com/en-us/library/fbk67b6z(v=vs.90).aspx[^]. It may also be simpler to allow the user to enter data direct into the datagridview.
这篇关于使用foreach语句将文本框控件的数据添加到datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文