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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
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.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

791 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