Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2011-03-22
4
Medium Priority
?
326 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 60

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 60

Accepted Solution

by:
HainKurt earned 2000 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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

618 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