I'm using Visual Studio and developing an ASP webshop website (in VB). I've attached an sql database and put a gridview on the form which pulls up the product details from the database. I can turn on Edit and Delete in the gridview so it is easy to edit the rows, but how can I add new rows? What would be the easiest solution? thanks!
Hello, You can use RowCommand event to add new row in gridview... sample code protected void gridView_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName.Equals("AddNew")) { TextBox inRollNo = (TextBox)gridView.FooterRow.FindControl("inRollNo"); TextBox Name = (TextBox)gridView.FooterRow.FindControl("inName"); TextBox Address = (TextBox)gridView.FooterRow.FindControl("inAddress"); // FileUpload image = (FileUpload)gridView.FooterRow.FindControl("FileUpload2"); //string str= image.FileName; con.Open(); SqlCommand cmd = new SqlCommand( "insert into Table1(Name,Address) values('" +Name.Text + "','" +Address.Text + "')", con); int result = cmd.ExecuteNonQuery(); con.Close(); if (result == 1) { loadStores(); lblmsg.BackColor = Color.Green; lblmsg.ForeColor = Color.White; lblmsg.Text = inRollNo.Text + " Added successfully...... "; } else { lblmsg.BackColor = Color.Red; lblmsg.ForeColor = Color.White; lblmsg.Text = inRollNo.Text + " Error while adding row....."; } } }
The forum has a CODE-button, for use when pasting code. Please use it? You can also just use the bbcode [ CODE ] code goes here [ /CODE ] (without the spaces)
you can try the following code [ CODE ]protectedvoidButton1_Click(object sender,EventArgs e){DataTable dt =newDataTable(); if(dt.Columns.Count==0){ dt.Columns.Add("PayScale",typeof(string)); dt.Columns.Add("IncrementAmt",typeof(string)); dt.Columns.Add("Period",typeof(string));} DataRowNewRow= dt.NewRow();NewRow[0]=TextBox1.Text;NewRow[1]=TextBox2.Text; dt.Rows.Add(NewRow);GridView1.DataSource= dt;GridViewl.DataBind();}[ /CODE ] here payscale,incrementamt and period are database field name.