Solved

How can you work with programmatic Datatables in Gridviews to allow for editing and deleting of columns and rows.

Posted on 2007-04-04
3
295 Views
Last Modified: 2010-04-15
I have a situation where I have programmatically created a temporary Datatable that is persistent in memory while the data is manipulated...rows are added...and values of columns can be edited..as well as rows being deleted from this table via the gridview.

Once the user is finished manipulating the data they click a button and everything writes to the database into the appropriate tables.

Well at least that is the functionality I need..

I have the Datatable and am currently using the Session to store the object temporarily while the user manipulates the data...

I can even bind the table to the gridview so that the data is displayed.

WHat I cant figure out is how to configure the gridview to allow me to edit the data in the rows and delete rows...normally if the table was created with a dataset I could just set an object datasource to the TableAdapter business object in the dataset and then if I bind the gridview to the ObjectDatasource I get all kinds of optionds to allow me to edit delte etc etc. and its easy to convert the fields to templates...

But how can you do this when your datatable is temporary and built programmatically?
I dont really want to have to be reading and writing and deleting from the database for this but I suppose if I hae to I have to...
0
Comment
Question by:Prysson
  • 2
3 Comments
 
LVL 16

Expert Comment

by:AlexNek
ID: 18853707
In such a situation we always used a grid like this one
http://www.infragistics.com/dotnet/netadvantage/winforms/wingrid.aspx#Overview
0
 
LVL 16

Accepted Solution

by:
AlexNek earned 500 total points
ID: 18853771
In addition I can't figure out your troubles.  I created tables/dataset in memory without any problem.
http://msdn2.microsoft.com/en-us/library/ss7fbaez.aspx
0
 

Author Comment

by:Prysson
ID: 18860124
I have foudn a better way to explain this..hopefully you can help me out.

This is my code that create my table programmatically and loads it into memory...

public DataTable MakeTable()
    {
        DataTable dto = new DataTable();
        DataColumn col;

        col = new DataColumn();
        col.DataType = System.Type.GetType("System.Int32");
        col.ColumnName = "ID";
        dto.Columns.Add(col);
       
        col = new DataColumn();
        col.DataType = System.Type.GetType("System.String");
        col.ColumnName = "Products";
        dto.Columns.Add(col);

        col = new DataColumn();
        col.DataType = System.Type.GetType("System.Int32");
        col.ColumnName = "Quantity";
        dto.Columns.Add(col);

        col = new DataColumn();
        col.DataType = System.Type.GetType("System.Int32");
        col.ColumnName = "Price";
        dto.Columns.Add(col);

        return dto;
    }

    private void AddData(string prod, int quantity, int prodid, int price, DataTable dto)
    {
        DataRow row;
        row = dto.NewRow();
        row["Products"] = prod;
        row["Quantity"] = quantity;
        row["ID"] = prodid;
        row["Price"] = price;
        dto.Rows.Add(row);
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {

            DataTable dto = new DataTable();
            dto = MakeTable();
            Session["prodDT"] = dto;

           
       

           
        }
       
       
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string prod = "Frontenac";
        int quantity = 101;
        int prodid = 1;
        int price = 12;
        AddData(prod, quantity, prodid, price, (DataTable)Session["prodDT"]);


    }



Now let's say I have a GridView1 and I  want to create a template field that is a textbox and I want to bind the TEXT property of that to the "Quantity" COlumn of this datatable...HOw would I do that. I am guessing the answer lies in the asp code somewhere...

<asp:GridView ID="GridView1" runat="server">
            <Columns>
                <asp:TemplateField HeaderText="Quantity">
                    <ItemTemplate>
                        <asp:TextBox ID="TextBox2" runat="server" Text="????"></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>  

BUt I am not certain what the code needs to be to bind that item template to the table column...
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

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

10 Experts available now in Live!

Get 1:1 Help Now