I want to make a phone book and need to add contact information from text boxes to data grid view.
Below is my code
Problem with it is that each time after first row fills - it doesn't new rows anymore.
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; }
}
how can use this class in my code?
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;
I would do it something like this:
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; }
}
The issue is because of
dr = dt.NewRow();
You have to check whether datatable has data or not, if it had then you have to assign as follows
dr = dt.Rows.Add();
For eg:
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;
}
try THIS:
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;
}
}
Because of the lack of information provided, I am going to assume that the error you are getting is due to a new DataTable every time and replaced it in the DataGridView's datasource. What you really want to do is create only one DataTable, attach it to the DataSource and add to that table every time. See the code below
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();
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.