Solved

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

Posted on 2011-03-22
4
318 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now