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
296 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

776 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