add new rows in gridview

Discussion in 'C#' started by btzr, Jun 15, 2010.

  1. #1
    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!
     
    btzr, Jun 15, 2010 IP
  2. Craig M

    Craig M Guest

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    try this...

    hxxp://www.4guysfromrolla.com/articles/021203-1.aspx
     
    Craig M, Jun 23, 2010 IP
  3. bitstream

    bitstream Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    insert into .....

    gridview.databind
     
    bitstream, Jul 4, 2010 IP
  4. m2hmonique

    m2hmonique Peon

    Messages:
    41
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Hello,

    Please elaborate little bit more. thanks :)
     
    m2hmonique, Jul 4, 2010 IP
  5. zinist

    zinist Banned

    Messages:
    91
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    43
    #5
    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.....";
    }
    }
    }
     
    zinist, Jun 27, 2015 IP
  6. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #6
    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)
     
    PoPSiCLe, Jun 27, 2015 IP
  7. necko16

    necko16 Greenhorn

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    21
    #8
    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.
     
    necko16, Dec 6, 2017 IP