?
Solved

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

Posted on 2011-03-22
4
Medium Priority
?
324 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 57

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 57

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

What is a Denial of Service (DoS)?

A DoS is a malicious attempt to prevent the normal operation of a computer system. You may frequently see the terms 'DDoS' (Distributed Denial of Service) and 'DoS' used interchangeably, but there are some subtle differences.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

771 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