locked
How to output data from gridview into textboxes RRS feed

  • Question

  • User1717218719 posted

    I am looking to output the data in my gridview to textboxs. I have multiple rows and columns in my gridview.

    I have code that uese a select row and outputs that row to a textbox but I now would like to know how to do it using one butten which displays all

    any help on this would be great

    what gridview I have looks like this

    1 2 3
    a b c
    d e f

    what I want it to look like when I click a button

    (TextBox1)

    a

    d

    (TextBox2)

    b

    e

    (TextBox3)

    c

    f

    Wednesday, April 17, 2019 10:55 AM

Answers

  • User-1174608757 posted

    Hi E.RU,

    Yes,you have to define columns name.If you want to loop the table and put its value in textbox,you should put the value of column in a control like label or textbox,so that you could get the value and loop the Gridview in code behind.  

    Else,in my code '<%#Eval("column name") %>' just represents the value of column, you should put it in the control.

    Best Regards

    Wei

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 24, 2019 9:13 AM

All replies

  • User-1174608757 posted

    Hi E.RU,

    According to your description, I have made a sample here. I suggest that you could  firstly put the column values of Gridview in  label firstly,then you could use foreach code to loop the text of label and put text into textbox in code behind.Lastly,by default ,textbox is one line only ,if you want set it as multiply lines , you should change its mode. Here is the demo.I hope it could help you.

    aspx:

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:GridView ID="GridView1" runat="server" Height="194px" Width="275px" AutoGenerateColumns="false">
                    <Columns>
                        <asp:TemplateField HeaderText="1">
                            <ItemTemplate>
                                <asp:Label ID="Label1" runat="server" Text='<%#Eval("1") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                    <Columns>
                         <asp:TemplateField HeaderText="2">
                            <ItemTemplate>
                                <asp:Label ID="Label2" runat="server" Text='<%#Eval("2") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                    <Columns>
                         <asp:TemplateField HeaderText="3">
                            <ItemTemplate>
                                <asp:Label ID="Label3" runat="server" Text='<%#Eval("3") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
                
                <asp:TextBox ID="T1" runat="server" TextMode="MultiLine"></asp:TextBox><br />
                <asp:TextBox ID="T2" runat="server" TextMode="MultiLine"></asp:TextBox><br />
                <asp:TextBox ID="T3" runat="server" TextMode="MultiLine"></asp:TextBox><br />
                <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
            </div>
        </form>
    </body>
    </html>
    

    aspx.cs:

    namespace WebApplication1._4._10
    {
        public partial class text : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    DataTable dt = new DataTable();
                    dt.Columns.Add(new DataColumn("1",typeof(string)));
                    dt.Columns.Add(new DataColumn("2", typeof(string)));
                    dt.Columns.Add(new DataColumn("3", typeof(string)));
                    dt.Rows.Add("a","b","c");
                    dt.Rows.Add("d", "e", "f");
                    GridView1.DataSource = dt;
                    GridView1.DataBind();
                   
                }
            }
    
            protected void Button1_Click(object sender, EventArgs e)
            {
               
                foreach(GridViewRow row in GridView1.Rows)
                {
                    Label L1 = (Label)row.FindControl("Label1");
                    T1.Text +=L1.Text+Environment.NewLine;
                    Label L2 = (Label)row.FindControl("Label2");
                    T2.Text += L2.Text + Environment.NewLine;
                    Label L3 = (Label)row.FindControl("Label3");
                    T3.Text += L3.Text + Environment.NewLine;
    
                }
            }
        }
    }

    You could see:

    Best Regards

    Wei 

    Thursday, April 18, 2019 2:36 AM
  • User1717218719 posted

    Thankyou for your reply Wei!

    I should have specified im coding in vb. I wrote some code which is almost does what I want.my code goes row by row but now I want the first column in one text box and next column in the next textbox. 

    The code I have reads in 1st column in Textbox1, the 1st & 2nd in Textbox2, the 1st & 2nd & 3rd in Textbox3 etc...

    The problem I have with it is I dont know how to filter the data for this. Any help is greatly appreciated

    Protected Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    
    
        Dim conConn As OleDbConnection = Nothing
        Dim comComm As OleDbCommand = Nothing
        Dim adpAdap As OleDbDataAdapter = Nothing
        Dim strOleCon As String = Nothing
        Dim DataS As New DataSet
        Dim Datatbl As New DataTable
    
    
        Me.TextBox2.Text = Nothing
        Me.TextBox3.Text = Nothing
        Me.TextBox4.Text = Nothing
        Me.TextBox5.Text = Nothing
        Me.TextBox6.Text = Nothing
        Me.TextBox7.Text = Nothing
        Me.TextBox8.Text = Nothing
        Me.TextBox9.Text = Nothing
    
        Try
    
            strOleCon = "Provider= Microsoft.ACE.OLEDB.12.0;Data Source='G:FilePath'; Persist Security Info=False;"
            conConn = New OleDbConnection(strOleCon)
    
    
            For i As Integer = 0 To Me.GridView1.Rows.Count - 1
    
                '-- Get the row object of the rows. 
                Dim anotherrow As GridViewRow = GridView1.Rows(i)
    
                '-- Build a list of the all row table numbers
                Dim strlist As String = Nothing
    
                For k As Integer = 0 To 7
                    strlist &= "," & anotherrow.Cells(19 + k).Text
    
                    '''' Exclude table numbers that are all "00000000" ??????
                Next
                strlist = strlist.TrimStart(",")
    
                '-- All rows
                comComm = New OleDbCommand
                With comComm
    
                    .Connection = conConn
                    .CommandType = CommandType.Text
                    .CommandText = "SELECT * FROM MSAccessTbl WHERE TblNbr IN ('" & strlist.Replace(",", "','") & "') ORDER BY TblNbr, LneNbr"
    
                End With
    
                '-- Fill a datable with the above
    
                adpAdap = New OleDbDataAdapter(comComm)
                adpAdap.Fill(Datatbl)
    
    
                '-- The datable now contains all rows for up to eight tables, each table number result needs to go in a separate textbox. So filter the datatable.??????
    
                Dim dv As New DataView(Datatbl)
    
    
                '-- Loop through each "row" of the view and build a temp string before applying to the textbox
                Dim strTemp As String = Nothing
    
                For k As Integer = 0 To dv.Count - 1
    
                    strTemp &= dv(k)("TxtDat").ToString.PadRight(50, " ") & vbCrLf
    
                Next
    
                '-- The view of the datatable is now filtered, depending on the index select the correct textbox
                Select Case i
                    Case 0
                        Me.TextBox2.Text = strTemp
                    Case 1
                        Me.TextBox3.Text = strTemp
                                          
                    Case 2
                        Me.TextBox4.Text = strTemp
                                           
                    Case 3
                        Me.TextBox5.Text = strTemp
                                            
                    Case 4
                        Me.TextBox6.Text = strTemp
                                            
                    Case 5
                        Me.TextBox7.Text = strTemp
                                            
                    Case 6
                        Me.TextBox8.Text = strTemp
                                           
                    Case 7
                        Me.TextBox9.Text = strTemp
                    Case Else
                End Select
    
            Next

    Thursday, April 18, 2019 9:01 AM
  • User-1174608757 posted

    Hi E.RU,

    According to your description, I couldn't understand your requirement clearly.For your first post, you  just want to loop the data in a gridview by column and put the column data in textbox  one by one.

    However,based on the codes you provided,you firstly want to get the specific column values by looping rows in Gridview then you  search the data in database and set output in datatable  finally  you set the textbox value of datatable . So,if you want to loop the gridview by column ,I suggest that you could put the label in template so that it will be easy to loop each column value because you only need to loop the label value.

    Here is vb code.

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:GridView ID="GridView1" runat="server" Height="194px" Width="275px" AutoGenerateColumns="false">
                    <Columns>
                        <asp:TemplateField HeaderText="1">
                            <ItemTemplate>
                                <asp:Label ID="Label1" runat="server" Text='<%#Eval("1") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                    <Columns>
                         <asp:TemplateField HeaderText="2">
                            <ItemTemplate>
                                <asp:Label ID="Label2" runat="server" Text='<%#Eval("2") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                    <Columns>
                         <asp:TemplateField HeaderText="3">
                            <ItemTemplate>
                                <asp:Label ID="Label3" runat="server" Text='<%#Eval("3") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
                
                <asp:TextBox ID="T1" runat="server" TextMode="MultiLine"></asp:TextBox><br />
                <asp:TextBox ID="T2" runat="server" TextMode="MultiLine"></asp:TextBox><br />
                <asp:TextBox ID="T3" runat="server" TextMode="MultiLine"></asp:TextBox><br />
                <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
            </div>
        </form>
    </body>
    </html>
    
    Public Class Te
        Inherits System.Web.UI.Page
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Not IsPostBack Then
                Dim dt As DataTable = New DataTable()
                dt.Columns.Add(New DataColumn("1", GetType(String)))
                dt.Columns.Add(New DataColumn("2", GetType(String)))
                dt.Columns.Add(New DataColumn("3", GetType(String)))
                dt.Rows.Add("a", "b", "c")
                dt.Rows.Add("d", "e", "f")
                GridView1.DataSource = dt
                GridView1.DataBind()
            End If
    
        End Sub
        Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
            For Each row As GridViewRow In GridView1.Rows
                Dim L1 As Label = CType(row.FindControl("Label1"), Label)
                T1.Text += L1.Text + Environment.NewLine
                Dim L2 As Label = CType(row.FindControl("Label2"), Label)
                T2.Text += L2.Text + Environment.NewLine
                Dim L3 As Label = CType(row.FindControl("Label3"), Label)
                T3.Text += L3.Text + Environment.NewLine
            Next
        End Sub
    End Class

    Else , if you just want loop datatable, you could try below codes to loop column value in datatable.

    Dim dtTable As DataTable
    For Each dtRow As DataRow In dtTable.Rows For Each dc As DataColumn In dtTable.Columns Dim field1 = dtRow(dc).ToString() Next
    Next

    Best Regards

    Wei

    Friday, April 19, 2019 2:42 AM
  • User1717218719 posted

    Hi Wei,

    Yes I am looking to loop through the table. I am reading in data from MS Acess so I dont have any lables or infact anything to define the data in the columns and rows I have in my datagridview,  as there is just too much data to list them all out. How would I go about it in that case? do I need to define columns by name or something in order for the textbox to know what data goes where, if so how do I do this.

    UPDATE: Ihave added my html code:

    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    
    <head runat="server">
    
        <link rel="stylesheet" type="text/css" href="StyleSheet.css" />
        <title>Hello World</title>
    </head>
    <body>
    
        <form id="form1" runat="server">
            <p class="auto-style2">
                Enter Code:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <asp:TextBox ID="TextBox1" runat="server" Height="34px" MaxLength="2"></asp:TextBox>
                &nbsp;<asp:Button ID="Button1" runat="server" Text="Submit" Width="113px" CssClass="auto-style1" />
                <asp:Button ID="Button2" runat="server" Text="Button" Width="113px" />
            </p>
            <p>
                &nbsp;
            </p>
    
            <asp:GridView CssClass="gridview" ID="GridView1" runat="server">
                <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <AlternatingRowStyle BackColor="#BFE4FF" />
    
                <Columns>
                    <asp:TemplateField HeaderText=" ">
                        <ItemTemplate>
                            <%# Container.DataItemIndex + 1 %>
                        </ItemTemplate>
                    </asp:TemplateField>
    
                </Columns>
    
            </asp:GridView>
    
            <asp:TextBox CssClass="texttitle" ID="TextBox12" runat="server" Height="63px" Width="1561px" ReadOnly="True"></asp:TextBox>
            <asp:TextBox CssClass="text" ID="TextBox11" runat="server" Height="63px" Width="1430px" ReadOnly="True"></asp:TextBox>
    
            <asp:TextBox CssClass="text" ID="TextBox10" runat="server" Width="303px" ReadOnly="True"></asp:TextBox>
    
            <table>
                <tr>
                    <td>
    
                        <asp:TextBox CssClass="textbox" ID="TextBox2" runat="server" TextMode="MultiLine" Height="200px" Columns="50"></asp:TextBox></td>
                    <td>
                        <asp:TextBox CssClass="textbox" ID="TextBox3" runat="server" TextMode="MultiLine" Height="200px" Columns="50"></asp:TextBox></td>
                    <td>
                        <asp:TextBox CssClass="textbox" ID="TextBox4" runat="server" TextMode="MultiLine" Height="200px" Columns="50"></asp:TextBox></td>
                    <td>
                        <asp:TextBox CssClass="textbox" ID="TextBox5" runat="server" TextMode="MultiLine" Height="200px" Columns="50"></asp:TextBox></td>
                </tr>
            </table>
    
            <table>
                <tr>
                    <td>
                        <asp:TextBox CssClass="textbox" ID="TextBox6" runat="server" TextMode="MultiLine" Height="200px" Columns="50"></asp:TextBox></td>
                    <td>
                        <asp:TextBox CssClass="textbox" ID="TextBox7" runat="server" TextMode="MultiLine" Height="200px" Columns="50"></asp:TextBox></td>
                    <td>
                        <asp:TextBox CssClass="textbox" ID="TextBox8" runat="server" TextMode="MultiLine" Height="200px" Columns="50"></asp:TextBox></td>
                    <td>
                        <asp:TextBox CssClass="textbox" ID="TextBox9" runat="server" TextMode="MultiLine" Height="200px" Columns="50"></asp:TextBox></td>
    
                </tr>
            </table>
        </form>
    </body>
    </html>
    

    Tuesday, April 23, 2019 8:07 AM
  • User-1174608757 posted

    Hi E.RU,

    Yes,you have to define columns name.If you want to loop the table and put its value in textbox,you should put the value of column in a control like label or textbox,so that you could get the value and loop the Gridview in code behind.  

    Else,in my code '<%#Eval("column name") %>' just represents the value of column, you should put it in the control.

    Best Regards

    Wei

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 24, 2019 9:13 AM