Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET
how to display a data from text box to gridview without databounding with the database
 
i have add the following code in source part in asp.net application
 
<form id="form1" runat="server">
    <div>
 
        <table class="style1">
            <tr>
                <td>
                    Name
                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
&nbsp; Address&nbsp;
                    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
&nbsp; Number
                    <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
                    <asp:Button ID="Button1" runat="server" onclick="Button1_Click1" Text="Add" />
                </td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td>
                    <asp:GridView ID="GridView1" runat="server">
                    </asp:GridView>
                </td>
                <td>
                    &nbsp;</td>
            </tr>
        </table>
        <br />
        <br />
        <br />
        <br />
        <br />
 
    </div>
    </form>
 
now when i enter the details in following textboxes then when i press add button
 
the information will be displayed in gridview what i enter in textbox
 
Note:- textboxes values are not inserted into database
 

So how it can be displayed
 

Give u r solution
Posted 16-Nov-11 18:57pm
Comments
AmolPandurangBorkar at 17-Nov-11 2:00am
   
Hove you got your solution ??
Please mark your solution bellow.
 
~Amol
Reshma89 at 17-Nov-11 2:17am
   
no
AmolPandurangBorkar at 17-Nov-11 2:52am
   
What is stopping you?
Reshma89 at 17-Nov-11 3:02am
   
ya iam create datatable button event
 
but i unable to create a Datacolumn in button event
 
so plz send me the exact code what have write in button click
AmolPandurangBorkar at 17-Nov-11 4:01am
   
I don't like people who want spoon feeding... :(
see my new solution for you.. :)
AmolPandurangBorkar at 17-Nov-11 4:02am
   
Check Solution4
AmolPandurangBorkar at 17-Nov-11 6:21am
   
Any updates Reshma ???
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Following can be one approach:
As soon as user clicks on Add button, follow following stapes
1. Crate a data table and create all the columns those are there in the grid
2. Now if grid is not blank then insert all the grid data in the data table by adding new rows (this way if grid has 3 rows then, your data table will have 3 rows)
3. New insert the textbox data in the data table (now data table has got 4th row)
4. Now set this data table as data source for data grid view.
Hope this will help you.
 
~Amol
  Permalink  
Comments
Reshma89 at 17-Nov-11 1:11am
   
thanks for u response
 
i have write the following code in pageload event
 
if (!IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.Add("Name");
dt.Columns.Add("Address");
dt.Columns.Add("Number");
ViewState["dt"] = dt;
}
 
and what code i have to write in button click event please mention u r code on button click event
AmolPandurangBorkar at 17-Nov-11 1:16am
   
No need to create data table on Page_Load… Create it on Button click and follow the steps written above.
Reshma89 at 17-Nov-11 2:14am
   
ok tell me the exact code what i have written in button click event
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Button_Click()
{
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add("Name");
dt.Columns.Add("Address");
dt.Columns.Add("Number");
//First fill all the date present in the grid
for (int intCnt = 0; intCnt < grd.Rows.Count; intCnt ++)
{
if (grd.Rows[intCnt].RowType == DataControlRowType.DataRow)
{
dr = dt.NewRow();
dr["Name"] = grd.Rows[intCnt].Cells[0].Value;
dr["Address"] = grd.Rows[intCnt].Cells[1].Value;
dr["Number"] = grd.Rows[intCnt].Cells[2].Value;
dt.Rows.Add(dr);
}
}
dr = dt.NewRow();
dr["Name"] = txt1.Text;
dr["Address"] = txt2.Text;
dr["Number"] = txt3.Text;
dt.Rows.Add(dr);
grd.DataSource = dt;
grd.DataBind();
}
 
Please make corrections if I made any syntactic mistake because I wrote this code directly in the text.
 
I hope now your problem will be solved
 

~Amol
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

create virtual table and insert data into that then after bind virtual table with Grid.
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 474
1 Maciej Los 375
2 OriginalGriff 263
3 CPallini 214
4 Peter Leow 200
0 OriginalGriff 6,152
1 Sergey Alexandrovich Kryukov 5,312
2 Maciej Los 3,279
3 Peter Leow 3,184
4 DamithSL 2,490


Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 17 Nov 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100