Insert a row in gridview

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cmrhema
    Contributor
    • Jan 2007
    • 375

    Insert a row in gridview

    Hi
    I want to insert a row in gridview using C#
    I do not want to connect using sqldatasource and input parameters( usually many of them code in UI part). But i DO NOT want that.
    i am supposed to do it dynamically. I have to put all code in C#. No connection should be made to SQLDATASOURCE

    Thanks
  • cmrhema
    Contributor
    • Jan 2007
    • 375

    #2
    Originally posted by cmrhema
    Hi
    I want to insert a row in gridview using C#
    I do not want to connect using sqldatasource and input parameters( usually many of them code in UI part). But i DO NOT want that.
    i am supposed to do it dynamically. I have to put all code in C#. No connection should be made to SQLDATASOURCE

    Thanks
    any one please reply?

    Comment

    • radcaesar
      Recognized Expert Contributor
      • Sep 2006
      • 759

      #3
      OK, the Make a Connection, Adapter, Command and Dataset.

      Bind that dataset to GridView.

      Inserting row in Gridview ? I Didn't get that.

      In runtime u can add a row using Gridview.Rows.A dd() Method.

      Comment

      • rohitbce
        New Member
        • Aug 2007
        • 30

        #4
        According to C# vchange the syntax
        try this
        Dim drRefData As DataRow
        drRefData = dtDataTableOfRe ferecnes.NewRow
        drRefData(0) = Me.txtName.Text .Trim
        drRefData(1) = Me.txtAddress.T ext.Trim
        drRefData(2) = Me.txtPhoneNumb er.Text.Trim
        drRefData(3) = ""
        drRefData(4) = 1
        dtDataTableOfRe ferecnes.Rows.A dd(drRefData)
        Me.dgVLEReferen cesInfo.DataSou rce = dtDataTableOfRe ferecnes
        Me.dgVLEReferen cesInfo.DataBin d()

        Comment

        • cmrhema
          Contributor
          • Jan 2007
          • 375

          #5
          I thank all the members who replied

          Works now

          producing the code below

          Thank you once again


          Code:
          protected void Insert_Click(object sender, EventArgs e)
          //This is a link button kept on the footer of the grid. I kept on the Edit footer part
          //There are 6 textboxes kept on footer of each field and named accordingly
          {
          
          
          string id1 = (GridView2.FooterRow.FindControl("txtID") as TextBox).Text;
          
          string name1 = (GridView2.FooterRow.FindControl("txtNAME") as TextBox).Text;
          
          string date1 = (GridView2.FooterRow.FindControl("txtDATE") as TextBox).Text;
          
          string qty1 = (GridView2.FooterRow.FindControl("txtQTY") as TextBox).Text;
          
          string price1 = (GridView2.FooterRow.FindControl("txtPRICE") as TextBox).Text;
          
          string amt1 = (GridView2.FooterRow.FindControl("txtAMT") as TextBox).Text;
          
          SqlConnection cnn = new SqlConnection("Server=HEMALATHA\\SQLEXPRESS;Integrated Security=SSPI;database=demo");
          
          cnn.Open();
          
          string sqlCommand = "insert into t1 values(" + id1 + ",'" + name1 + "','" + date1 + "'," + qty1 + "," + price1 + "," + amt1+")";
          
          SqlCommand cmd = new SqlCommand(sqlCommand,cnn);
          
          cmd.ExecuteNonQuery();
          
          GridView2.DataBind();
          
          Display();
          //The above is a method which will display the entire grid
          }
          Now only one more help, I want to place a dtpicker instead of textbox for inserting date values. I tried the calendar control. But that increased the breadth and width of the gridview.

          Any suggestions please.

          Thanks

          Comment

          Working...