Editable Gridview to edit underlying DataTable in ASP.NET (C#)

Posted on 2011-10-08
Last Modified: 2013-12-16
In my ASP.NET code in C# (VS 2008) I have created a gridView and here is the .aspx code:

        <asp:GridView ID="grid1" runat="server">

In aspx.cs code I have created a DataTable via program code and I could bind to the GridView by this code:

            grid1.DataSource = dsOut;

dsOut above has variable number of columns and unknown column names (names of the columns are determined in the program code).
The content shows in the GridView correctly. How can I make the GridView  editable so that after the Submit button is pressed --

 protected void btn1_Click(object sender, EventArgs e)
        DataTable dt = (DataTable)grid1.DataSource;

The above works. I can get the data in "dt" but all I need it seems to make the GridView editable..

Question by:toooki
    LVL 12

    Accepted Solution


    Author Comment

    Thank you.
    I have used editable gridview before but there I used the bound source (bound to a database table) -- there Edit/Update was fairly easy (as they way mentioned in the above links...).

    But the GridView is bound to a dataSet.  

    I cannot provide the column names:

            <asp:GridView ID="grid1" runat="server">

    Is there any alternate design that will let me update the dataSet (NOT bound directly to a database table) via a web form..?
    LVL 12

    Expert Comment

    Is your columns same every time or they will b changing every time you bind the grid with DataSet.

    Author Comment

    The number of columns will be the same. But the column names are not like Id/Name/Address..
    The column names are like Feb022011, Feb092011, Feb162011, Mar032011, etc.
    But yes the column names will be the same...
    (I get the column names by somewhat transposing a table from the database -- so the column names are like this)
    LVL 12

    Assisted Solution

    That means if you will be filling your dataset for month of apr, may or august the column names can change i suppose for the gridview to show but the display will be like May022011 etc.

    Is this so?

    Author Comment

    Thank you.
    Yes that is right..I obtain the values say May022011 from a date field from the database table and convert to char string like May022011 .

    I could fill the dataset without issues.

    The dataset looks like:
    Id          02Feb2011 09Feb2011 06Apr2011 20Jun2011
    abc1     10               15                20              5
    abc2      15               7                  9               70
    abc2       8                2                  0               11


    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
    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…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

    732 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

    22 Experts available now in Live!

    Get 1:1 Help Now