Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

912 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

20 Experts available now in Live!

Get 1:1 Help Now