Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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

XML
<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
Comments
Amol_27101982, India 17-Nov-11 2:00am    
Hove you got your solution ??
Please mark your solution bellow.

~Amol
Reshma89 17-Nov-11 2:17am    
no
Amol_27101982, India 17-Nov-11 2:52am    
What is stopping you?
Reshma89 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
Amol_27101982, India 17-Nov-11 4:01am    
I don't like people who want spoon feeding... :(
see my new solution for you.. :)

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
 
Share this answer
 
create virtual table and insert data into that then after bind virtual table with Grid.
 
Share this answer
 
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
 
Share this answer
 
Comments
Reshma89 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
Amol_27101982, India 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 17-Nov-11 2:14am    
ok tell me the exact code what i have written in button click event

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