Click here to Skip to main content
15,892,643 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a textbox in my GridView. I want to bind that textbox with required Data. How do I do that? Below are my aspx & aspx.vb files.

aspx:

ASP.NET
<asp:GridView ID="grdItems" runat="server"  Width="100%" AllowPaging="True" CellPadding="4" ForeColor="#333333" GridLines="Horizontal" AutoGenerateColumns="False">
     <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" Font-Size="X-Small" />
     <RowStyle BackColor="#EFF3FB" />
     <EditRowStyle BackColor="#2461BF" />
     <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
     <pagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" Font-Size="Small" />
     <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
     <AlternatingRowStyle BackColor="White" />
       <Columns>

      <asp:BoundField DataField="actionItemId" HeaderText="Item Id"  >
      <ItemStyle Font-Size="Small" VerticalAlign="Top" />
      <HeaderStyle Font-Bold="True" Font-Size="Small" HorizontalAlign="Left" Width="65px" />
      <FooterStyle Font-Size="X-Small" />
      </asp:BoundField>
                
      <asp:TemplateField HeaderText="Description" >
      <ItemStyle Font-Size="Small" VerticalAlign="Top" />
      <HeaderStyle Font-Size="Small" HorizontalAlign="Left" Width="265px"/>
      <ItemTemplate>
       
      </ItemTemplate>
      </asp:TemplateField>
     
      <asp:TemplateField HeaderText="Actions Taken">
      <ItemTemplate>
      <tr>
      <td colspan="1">
      <asp:TextBox runat="server" ID="actionsTB" TextMode="MultiLine"> </asp:TextBox>
      </td>
      </tr>
      </ItemTemplate>
      <ItemStyle Font-Size="Small" VerticalAlign="Top" />
      <HeaderStyle Font-Bold="True" Font-Size="Small" HorizontalAlign="Left" />
      </asp:TemplateField>
            
       </Columns>
         <pagerSettings Mode="NumericFirstLast" />
         </asp:GridView>


aspx.vb:(Bind Method for that column)

VB
Private Sub GetActionsTaken(ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs, ByVal curActionItemId As Int32)
       Dim flexdata As DataSet = Nothing
       flexdata = CType(Session("flexdata"), DataSet)
       Dim myRows() As DataRow
       Dim sbData As New System.Text.StringBuilder
       Dim dbhelper As New DbHelper

       myRows = flexdata.Tables(table.PastActivities).Select("actionitemid=" & curActionItemId)
       For Each myRow As DataRow In myRows
       sbData.Append("" & dbhelper.ConvertDrString(myRow.Item(colActivity.occurredOn)) & " - " & "" & dbhelper.ConvertDrString(myRow.Item(colActivity.personFullName)) & "<br>")
       sbData.Append(dbhelper.ConvertDrString(myRow.Item(colActivity.activity)) & "<br><br>")
       Next
       e.Row.Cells(gridCol.ActionsTaken).Text = sbData.ToString
       dbhelper = Nothing
     End Sub


Previously, the data was passed directly to the Column's text as shown above in the aspx.vb file. But now I have a textbox in the same column and I want to bind the same data with that textbox. Any help would be greatly appreciated. Thanks!

What I have tried:

New to ASP.Net. Trying to solve it.
Posted
Updated 21-Aug-18 11:36am

1 solution

Here's a quick example. This is in C# as I don't do VB.NET unfortunately. But this should be straight forward for you to follow and convert. I've updated the code and added a VB.NET equivalent.

ASPX (removed the styles for simplicity):

ASP.NET
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" 
            onrowdatabound="GridView1_RowDataBound">
        <Columns>
            <asp:BoundField DataField="Id" HeaderText="Id" />
            <asp:BoundField DataField="Field1" HeaderText="Field 1" />
            <asp:TemplateField>
                <ItemTemplate>
                    <tr>
                        <td colspan="3">
                            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                        </td>
                    </tr>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
     </asp:GridView>


Code Behind:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebFormDemo
{
    public partial class Repeater : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e) {
            if (!IsPostBack) {
                BindGridView();
            }

        }

        private void BindGridView() {
            var data = GetSampleData();

            GridView1.DataSource = data;
            GridView1.DataBind();
        }


        private List<Student> GetSampleData() {
            List<Student> students = new List<Student>();
            students.Add(new Student() { Id = 1, Field1 = "SomeText 1", Field2 = "SomeText 1" });
            students.Add(new Student() { Id = 2, Field1 = "SomeText 2", Field2 = "SomeText 2" });
            students.Add(new Student() { Id = 3, Field1 = "SomeText 3", Field2 = "SomeText 3" });

            return students;
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) {
            if (e.Row.RowType == DataControlRowType.DataRow) {
                //access first column
                int id = Convert.ToInt32(e.Row.Cells[0].Text);

                //access the TexBox
                TextBox tb = (TextBox)e.Row.FindControl("TextBox1");
                tb.Text = "Assign whatever value here";
            }
        }
    }

    public class Student
    {
        public int Id { get; set; }
        public string Field1 { get; set; }
        public string Field2 { get; set; }
    }
}


The key there is the RowDataBound event. That event is fired when a grid is bounded with data, that's why it's a perfect place to access your TextBox and assign a value to it.

Here's the VB.NET equivalent using a convert tool:

VB
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls

Namespace WebFormDemo
    Public Partial Class Repeater
        Inherits System.Web.UI.Page

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
            If Not IsPostBack Then
                BindGridView()
            End If
        End Sub

        Private Sub BindGridView()
            Dim data = GetSampleData()
            GridView1.DataSource = data
            GridView1.DataBind()
        End Sub

        Private Function GetSampleData() As List(Of Student)
            Dim students As List(Of Student) = New List(Of Student)()
            students.Add(New Student() With {
                .Id = 1,
                .Field1 = "SomeText 1",
                .Field2 = "SomeText 1"
            })
            students.Add(New Student() With {
                .Id = 2,
                .Field1 = "SomeText 2",
                .Field2 = "SomeText 2"
            })
            students.Add(New Student() With {
                .Id = 3,
                .Field1 = "SomeText 3",
                .Field2 = "SomeText 3"
            })
            Return students
        End Function

        Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
            If e.Row.RowType = DataControlRowType.DataRow Then
                Dim id As Integer = Convert.ToInt32(e.Row.Cells(0).Text)
                Dim tb As TextBox = CType(e.Row.FindControl("TextBox1"), TextBox)
                tb.Text = "Assign whatever value here"
            End If
        End Sub
    End Class

    Public Class Student
        Public Property Id As Integer
        Public Property Field1 As String
        Public Property Field2 As String
    End Class
End Namespace
 
Share this answer
 
v2
Comments
Member 13952925 22-Aug-18 10:19am    
It's working Vincent. Thank you so much for you help. I really appreciate it. :)
Vincent Maverick Durano 22-Aug-18 10:22am    
You bet! glad to be of help.
Member 13952925 22-Aug-18 11:08am    
And Vincent, I'm actually sending data from a String Builder into the TextBox. I'm making few sentences bold, italic etc while appending to the String Builder, and I don't see any bold or italic text when this text is sent to the textbox. Do you know why?
Vincent Maverick Durano 22-Aug-18 11:11am    
TextBox only displays data in plain Text. You need to use Literal control instead to retain the format.
Member 13952925 22-Aug-18 11:22am    
Changed it to Literal. It worked Vincent. Thank you so much. So glad to come across a knowledgable guy like you. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900