Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. Adding textbox column to gridview programmatically (vb.net)

Adding textbox column to gridview programmatically (vb.net)

Scheduled Pinned Locked Moved Web Development
csharp
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mominafiz
    wrote on last edited by
    #1

    hi. i want to add textbox column in gridview without using template in my aspx page...i m adding Sr.No column and few more columns to the datatable and then loading the gridview...but along with this i want to add textbox column in the end of the gridview column. Adding textbox to template tag in aspx page result in addition of textbox column in the first column ..this is want to the last column. Here is the code till loading gridview

        Dim dt As New DataTable()
        Dim dcol1 As New DataColumn(IDgen, GetType(System.Int32))
        dcol1.AutoIncrement = True
        dcol1.AutoIncrementSeed = 1
        dcol1.AutoIncrementStep = 1
        dcol1.Unique = True
        dt.Columns.Add(dcol1)
    
        Dim cmd As New SqlCommand
        cmd = New SqlCommand("SELECT \* FROM TestName", MySearchCon)
        cmd = New SqlCommand(Str.ToString, MySearchCon)
        cmd.Connection.Open()
        Dim rdr As SqlDataReader
        rdr = cmd.ExecuteReader()
        dt.Load(rdr)
    
        GrdDynamic.DataSource = dt
        GrdDynamic.DataBind()
        cmd.Connection.Close()
    

    A

    J 1 Reply Last reply
    0
    • M mominafiz

      hi. i want to add textbox column in gridview without using template in my aspx page...i m adding Sr.No column and few more columns to the datatable and then loading the gridview...but along with this i want to add textbox column in the end of the gridview column. Adding textbox to template tag in aspx page result in addition of textbox column in the first column ..this is want to the last column. Here is the code till loading gridview

          Dim dt As New DataTable()
          Dim dcol1 As New DataColumn(IDgen, GetType(System.Int32))
          dcol1.AutoIncrement = True
          dcol1.AutoIncrementSeed = 1
          dcol1.AutoIncrementStep = 1
          dcol1.Unique = True
          dt.Columns.Add(dcol1)
      
          Dim cmd As New SqlCommand
          cmd = New SqlCommand("SELECT \* FROM TestName", MySearchCon)
          cmd = New SqlCommand(Str.ToString, MySearchCon)
          cmd.Connection.Open()
          Dim rdr As SqlDataReader
          rdr = cmd.ExecuteReader()
          dt.Load(rdr)
      
          GrdDynamic.DataSource = dt
          GrdDynamic.DataBind()
          cmd.Connection.Close()
      

      A

      J Offline
      J Offline
      JamieRushton_
      wrote on last edited by
      #2

      Use the GridViews DataBound event. C#

      protected void Page_Init(object sender, EventArgs e)
      {
      GridView1.DataBound += new EventHandler(GridView1_DataBound);
      }

      void GridView1_DataBound(object sender, EventArgs e)
      {
      GridView gridview = (GridView)sender;

      try
      {
          foreach (GridViewRow row in gridview.Rows)
          {
              TextBox textbox = new TextBox();
              textbox.Text = "Something";
      
              TableCell tableCell = new TableCell();
              tableCell.Controls.Add(textbox);
      
              row.Cells.Add(tableCell);
          }
      }
      catch (Exception ex)
      {
      
      }
      

      }

      VB.NET

      Private Sub GridView1_DataBound(sender As Object, e As EventArgs)
      Dim gridview As GridView = CType(sender, GridView)
      Try
      For Each row As GridViewRow In gridview.Rows
      Dim textbox As New TextBox()
      textbox.Text = "Something"

          Dim tableCell As New TableCell()
          tableCell.Controls.Add(textbox)
      
          row.Cells.Add(tableCell)
      Next
      Catch ex As Exception
      End Try
      

      End Sub

      M 1 Reply Last reply
      0
      • J JamieRushton_

        Use the GridViews DataBound event. C#

        protected void Page_Init(object sender, EventArgs e)
        {
        GridView1.DataBound += new EventHandler(GridView1_DataBound);
        }

        void GridView1_DataBound(object sender, EventArgs e)
        {
        GridView gridview = (GridView)sender;

        try
        {
            foreach (GridViewRow row in gridview.Rows)
            {
                TextBox textbox = new TextBox();
                textbox.Text = "Something";
        
                TableCell tableCell = new TableCell();
                tableCell.Controls.Add(textbox);
        
                row.Cells.Add(tableCell);
            }
        }
        catch (Exception ex)
        {
        
        }
        

        }

        VB.NET

        Private Sub GridView1_DataBound(sender As Object, e As EventArgs)
        Dim gridview As GridView = CType(sender, GridView)
        Try
        For Each row As GridViewRow In gridview.Rows
        Dim textbox As New TextBox()
        textbox.Text = "Something"

            Dim tableCell As New TableCell()
            tableCell.Controls.Add(textbox)
        
            row.Cells.Add(tableCell)
        Next
        Catch ex As Exception
        End Try
        

        End Sub

        M Offline
        M Offline
        mominafiz
        wrote on last edited by
        #3

        thanks Jamie!

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups