如何将数据从 Texbox 插入到 DataGridView
本文关键字:插入 DataGridView Texbox 数据 | 更新日期: 2024-10-31 14:23:16
我想制作电话簿,需要将文本框中的联系信息添加到数据网格视图。
下面是我的代码
它的问题是每次在第一行填充后 - 它不再新行。
class information
{
public string firstname { get; set; }
public string surname { get; set; }
public int phonenumber { get; set; }
public int cellphone { get; set; }
public string email { get; set; }
}
如何在我的代码中使用此类?
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add("Firstname");
dt.Columns.Add("Lastname");
dt.Columns.Add("PhoneNumber");
dr = dt.NewRow();
dr["Firstname"] = tb1.Text;
dr["Lastname"] = tb2.Text;
dr["PhoneNumber"] = tb3.Text;
dt.Rows.Add(dr);
dataGridView1.DataSource = dt;
我会这样做:
private void PopulateGridView()
{
var myRecords = new List<PhoneBookRecord>();
myRecords.Add(new PhoneBookRecord { FirstName = "blah", LastName = "Blah", PhoneNumber = "1234" });
myRecords.Add(new PhoneBookRecord { FirstName = "New", LastName = "Song", PhoneNumber = "132134" });
temper.DataSource = myRecords;
}
class PhoneBookRecord
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string PhoneNumber { get; set; }
}
由于缺乏提供的信息,我将假设您得到的错误是由于每次都有新的 DataTable 造成的,并在DataGridView 的数据源。您真正想要做的是只创建一个数据表,将其附加到数据源,然后每次都添加到该表中。请参阅下面的代码
public partial class Form1 : Form
{
DataTable dt = new DataTable();
public Form1()
{
InitializeComponent();
// Insert the columns you require
dt.Columns.Add("Firstname");
dt.Columns.Add("Lastname");
dt.Columns.Add("PhoneNumber");
// This is the only time you need to attach to the DataSource
dataGridView1.DataSource = dt;
}
private void button1_Click(object sender, EventArgs e)
{
// Add a new row
var dr = dt.Rows.Add();
// And attach the values to the appropriate column
dr["Firstname"] = tb1.Text;
dr["Lastname"] = tb2.Text;
dr["PhoneNumber"] = tb3.Text;
}
private void DeleteRow()
{
if (dataGridView1.SelectedRows.Count > 0)
{
dt.Rows[dataGridView1.SelectedRows[0].Index].Delete();
return;
}
if (dataGridView1.SelectedCells.Count > 0)
{
dt.Rows[dataGridView1.SelectedCells[0].RowIndex].Delete();
}
}
}
问题是因为
dr = dt.NewRow();
你必须检查数据表是否有数据,如果有,那么你必须分配如下
dr = dt.Rows.Add();
例如:
if(ViewState("temp") == null)
{
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add("Firstname");
dt.Columns.Add("Lastname");
dt.Columns.Add("PhoneNumber");
dr = dt.NewRow();
dr["Firstname"] = tb1.Text;
dr["Lastname"] = tb2.Text;
dr["PhoneNumber"] = tb3.Text;
dt.Rows.Add(dr);
dataGridView1.DataSource = dt;
ViewState("temp")=dt;
}
else
{
DataTable dt = ViewState("temp")
DataRow dr;
dr =dt.Rows.Add();
dr["Firstname"] = tb1.Text;
dr["Lastname"] = tb2.Text;
dr["PhoneNumber"] = tb3.Text;
dt.Rows.Add(dr);
dataGridView1.DataSource = dt;
ViewState("temp")=dt;
}
试试这个:
public partial class Form1 : Form
{
DataTable dt = new DataTable();
DataRow dr;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
dt.Columns.Add("Firstname");
dt.Columns.Add("Lastname");
dt.Columns.Add("PhoneNumber");
}
private void button1_Click(object sender, EventArgs e)
{
dr = dt.NewRow();
dr["Firstname"] = textBox1.Text;
dr["Lastname"] = textBox2.Text;
dr["PhoneNumber"] = textBox3.Text;
dt.Rows.Add(dr);
dataGridView1.DataSource = dt;
}
}