I have a datagridview in which I display informations from my database.
I want my user to add information in the last row and then he will click on the button add that will add a new row into the database.
The function is below. When I debug it, it goes through all my rows, get all the informations, but it doesn't add my new row to the database.
private void addNewAgent()
{
int row = 0;
if (ViewState["CurrentTable"] !=null)
{
DataTable mydt = (DataTable)ViewState["CurrentTable"];
DataRow myrow = null;
if (mydt.Rows.Count>0)
{
for (int i = 1; i <= mydt.Rows.Count; i++)
{
TextBox fn = GridView1.Rows[row].Cells[1].FindControl("txt_FirstName") as TextBox;
TextBox ln = GridView1.Rows[row].Cells[2].FindControl("txt_LastName") as TextBox;
TextBox email = GridView1.Rows[row].Cells[3].FindControl("txt_Email") as TextBox;
myrow = mydt.NewRow();
myrow["UserID"] = i + 1;
mydt.Rows[i - 1]["FirstName"] = fn.Text;
mydt.Rows[i - 1]["LastName"] = ln.Text;
mydt.Rows[i - 1]["email"] = email.Text;
row++;
}
mydt.Rows.Add(myrow);
ViewState["CurrentTable"] = mydt;
GridView1.DataSource = mydt;
GridView1.DataBind();
}
else
{
Response.Write("view state is null");
}
}
}