Solved

How to Set Gridview Datasource to be a Table which is created dynamically

Posted on 2011-03-22
4
320 Views
Last Modified: 2012-05-11
Helllo Experts,
I am creating a table and enter information into it dynamically. No DATABASE is used. Just plain textbox added dynamically to the table and on a button I am trying to bind this table to a Gridview.

But i am getting error while Binding the table to the Gridview. Can some guide me where I am making the error

here is my markup

Thanks and appreciate it
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DynamicTable.aspx.cs" Inherits="DataTableCsharp2005_DynamicTable" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
    <script type="text/javascript" language="javascript">
        function addRow()
        {
            var tbl = document.getElementById('Prod');
            var lastRow = tbl.rows.length;
            var iteration = lastRow;
            var row = tbl.insertRow(lastRow);
            
            //ItemNo Column
            var cellItemNo = row.insertCell(0);
            var el = document.createElement('input');
            el.type = 'text';
            el.name = 'txtItemNo' + iteration;
            el.id = 'txtItemNo' + iteration;
            el.className = 'textbox';  
            el.runat = 'server';
            cellItemNo.appendChild(el);
            
            
            //Desc Column
            var cellDesc = row.insertCell(1);
            var el = document.createElement('input');
            el.type = 'text';
            el.name = 'txtDesc' + iteration;              
            el.id = 'txtDesc' + iteration;
            el.className = 'textbox';                 
            el.runat = 'server';     
            cellDesc.appendChild(el);
            
            //Size Column
            var cellSize = row.insertCell(2);
            var el = document.createElement('input');
            el.type = 'text';
            el.name = 'txtSize' + iteration;                
            el.id = 'txtSize' + iteration;
            el.className = 'textbox';             
            el.runat = 'server';     
            cellSize.appendChild(el);
            
            //Price Column   
            var cellPrice = row.insertCell(3);
            var el = document.createElement('input');
            el.type = 'text';
            el.name = 'txtPrice' + iteration;                
            el.id = 'txtPrice' + iteration;
            el.className = 'textbox';             
            el.runat = 'server';     
            cellPrice.appendChild(el);
            
            //Status Column
            var cellStatus = row.insertCell(4);
            var el = document.createElement('input');
            el.type = 'text';
            el.name = 'txtStatus' + iteration;                
            el.id = 'txtStatus' + iteration;
            el.className = 'textbox';             
            el.runat = 'server';     
            cellStatus.appendChild(el);
            
            //backorder date Column
            var cellBO = row.insertCell(5);
            var el = document.createElement('input');
            el.type = 'text';
            el.name = 'txtBO' + iteration;                
            el.id = 'txtBO' + iteration;
            el.className = 'textbox';             
            el.runat = 'server';     
            cellBO.appendChild(el);
        }
    </script>

</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table border="1" id="tblProd" runat="server">
            <tr>
                <th>ItemNo</th>
                <th>Description</th>
                <th>Size</th>
                <th>Price</th>
                <th>Status</th>
                <th>Expected Backorder Date</th>
            </tr>
            <tr>
                <td><input type="text" name="txtItemNo" id="txtItemNo" runat="server" /></td>
                <td><input type="text" name="txtDesc" id="txtDesc" runat="server" /></td>
                <td><input type="text" name="txtSize" id="txtSize" runat="server" /></td>
                <td><input type="text" name="txtPrice" id="txtPrice" runat="server" /></td>
                <td><input type="text" name="txtStatus" id="txtStatus" runat="server" /></td>
                 <td><input type="text" name="txtBO" id="txtBO" runat="server" /></td>
            </tr>
        </table>
        <br />
        <p>
            <input type="button" value="Add Line" onclick="addRow()" />
            <asp:Button ID="submit" runat="server" Text="submit" OnClick="submit_Click" />
        </p>

        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
    </div>
    </form>
</body>
</html>



------------- CODE BEHIND --------------------
    protected void submit_Click(object sender, EventArgs e)
    {
        this.GridView1.DataSource = tblProd;
        this.GridView1.DataBind();
    
    }

Open in new window

0
Comment
Question by:niceoneishere
  • 2
  • 2
4 Comments
 
LVL 51

Expert Comment

by:HainKurt
ID: 35193978
this is ok

this.GridView1.DataSource = tblProd;
this.GridView1.DataBind();

as long as tblProd has some data in it... is this the table on aspx code? oops... you should create a datatable, add columns to this and one row with values coming from text boxes on page
then you can bind this into gridview...
0
 
LVL 1

Author Comment

by:niceoneishere
ID: 35194408
Yes tbProd will have the data which will be entered by the user and Yes i am adding table using javascript.

Can you tell me if there is another way to do this sir. I appreciate it
0
 
LVL 51

Accepted Solution

by:
HainKurt earned 500 total points
ID: 35197849
not sure but you can try this

create a class

class MyItem {
  int ItemNo;
  String Description;
  ...
}

then  create an array/collection of MyItems

collection MyItems list of MyItem; // not sure about the syntax

protected void submit_Click(object sender, EventArgs e)
    {
        MyItems=Session["items"];
        MyItem i = new MyItem;
        i.ItemNo = txtItemNo.Text
...
        i.BackorderDate = txtBO.Text
        MyItems.add(i);
        Session["items"]=MyItems;

        this.GridView1.DataSource = MyItems;
        this.GridView1.DataBind();
    }
0
 
LVL 1

Author Closing Comment

by:niceoneishere
ID: 35388502
thanks
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Service Controller 1 23
DataTables + iCheck + pagination Issue 2 23
C# LINQ ForEach() question 6 28
tableview is not updating 1 10
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question