Click here to Skip to main content
15,892,005 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
Posted
Updated 21-Aug-18 10:16am
v6
Comments
[no name] 17-Aug-18 15:07pm    
A single row underneath each "row of columns" versus an extra column buys nothing; only confusion for someone trying to look down a column.
Member 13952925 17-Aug-18 15:48pm    
Yeah. But my requirement is that there's a lot of text in that column and I want to reduce the number of pages of the final results of the table. If I move that text to seperate row, I can spread the text across the entire display and my pages count would reduce.
Vincent Maverick Durano 20-Aug-18 9:25am    
Are you talking about ASP.NET GridView or WinForm's DataGridView?
Member 13952925 20-Aug-18 11:59am    
ASP.Net GridView @Vincent
Vincent Maverick Durano 20-Aug-18 12:24pm    
If your intent is just to display data, then you may use a Repeater control instead to have more flexibility in terms of layout formatting. You could also use DataList since it provides properties like RepeatLayout and RepeatDirection that you can set.

1 solution

Based on our comments discussion, here's a quick demo for your reference:

ASPX:
ASP.NET
<asp:Repeater ID="Repeater1" runat="server">
        <HeaderTemplate>
            <table>
        </HeaderTemplate>
        <ItemTemplate>
                 <tr>
                    <td><%# DataBinder.Eval(Container.DataItem, "Id") %></td>
                    <td><%# DataBinder.Eval(Container.DataItem, "Field1") %></td>
                    <td><%# DataBinder.Eval(Container.DataItem, "Field2") %></td>
                    <td><%# DataBinder.Eval(Container.DataItem, "Field3") %></td>
                 </tr>
                 <tr>
                   <td colspan="4"><%# DataBinder.Eval(Container.DataItem, "Field4") %></td>
                 </tr>
        </ItemTemplate>
        <FooterTemplate>
            </table>
        </FooterTemplate>
</asp:Repeater>

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) {
                BindRepeater();
            }

        }

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

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

        private List<Student> GetSampleData() {
            List<Student> students = new List<Student>();
            students.Add(new Student() { Id = 1, Field1 = "SomeText 1", Field2 = "SomeText 1", Field3 = "SomeText 3", Field4="SomeText with more contents here and there." });
            students.Add(new Student() { Id = 2, Field1 = "SomeText 2", Field2 = "SomeText 2", Field3 = "SomeText 2", Field4 = "SomeText with more contents here and there." });
            students.Add(new Student() { Id = 3, Field1 = "SomeText 3", Field2 = "SomeText 3", Field3 = "SomeText 3", Field4 = "SomeText with more contents here and there." });

            return students;
        }
    }

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


Field4 in the example above should display below each row of the entry.

Hope that helps!
 
Share this answer
 

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