Adding textbox column to gridview programmatically (vb.net)
-
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
-
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
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) { }
}
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
-
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) { }
}
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