Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4253386
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T04:56:21+00:00 2026-05-21T04:56:21+00:00

I have a Linq query that returns an anonymous type, which I then use

  • 0

I have a Linq query that returns an anonymous type, which I then use as the data source for a GridView. Only 1 of those columns need to be editable. I just want to be able to click on an edit button and have ALL the rows of that 1 column become textboxes (ideally). I will settle for having a CommandField for each row. The problem is, I don’t know how to do that without creating a custom class or manually dealing with all the fields. This column must also update a value in the database in a specific way (so I need to write custom code for that query).

At the moment, I’m easily able to make the whole thing display in the Grid View. I even created a custom DataTable to use as the Data Source and that works okay. But I’m still at a loss as to how to add the editing capabilities. The tutorials I have found are either for a DataSource control or involve making custom classes that extend GridView and what not. I’ve added a CommandField in the aspx page, which works okay but the ReadOnly columns in the DataTable are still editable.

I’d rather have a simple solution…for example, making a TemplateField in the .aspx page that I can give an Item and EditItem Template and somehow bind to the fields returned by my Linq query.

I’ve included some pseudocode below which creates 2 GridViews. One just has all the information displayed, but not editable. The other uses the DataTable which shows ColA, the ColB from the aspx page with empty rows and the ColB from the code behind page, populated.

Binding Query to GridView:

var qry = from t in database
          ...
          select new { colA, colB };

GridView2.DataSource = qry; // not shown in pseudocode
GridView2.DataBind();

Making DataTable

    const string ColA = "ColA";
    const string ColB = "ColB";

    DataTable dt = new DataTable();
    DataColumn dc = new DataColumn(ColA, typeof(System.String));
    dt.Columns.Add(dc);

    dc = new DataColumn(ColB, typeof(System.String));
    dt.Columns.Add(dc);

    foreach (var item in qry) {
        DataRow dr = dt.NewRow();
        dr[ColA] = item.ColA;
        dt.Rows.Add(dr);
    }

    foreach (DataColumn col in dt.Columns) {
        col.ReadOnly = false;
        BoundField bf = new BoundField();
        bf.DataField = col.ColumnName;
        bf.HeaderText = col.ColumnName;
        GridView1.Columns.Add(bf);
    }

    GridView1.DataSource = dt;
    GridView1.DataBind();

Making GridView Control in aspx Page

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" >
  <Columns>
     <asp:TemplateField HeaderText="ColB">
       <EditItemTemplate>
          <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
       </EditItemTemplate>
       <ItemTemplate>
          <asp:Label ID="Label1" runat="server" ></asp:Label>
       </ItemTemplate>
     </asp:TemplateField>
     <asp:CommandField ButtonType="Button" ShowDeleteButton="True" 
        ShowEditButton="True" ShowInsertButton="True" UpdateText="Save">
     </asp:CommandField>
   </Columns>
</asp:GridView>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T04:56:21+00:00Added an answer on May 21, 2026 at 4:56 am

    To avoid the error, you’ll have to handle all of the row events; something like this

        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowCancelingEdit="GridView1_RowCancelingEdit"
            OnRowCommand="GridView1_RowCommand" OnRowEditing="GridView1_RowEditing" OnRowUpdated="GridView1_RowUpdated">
            <Columns>
                <asp:BoundField DataField="ColA" ReadOnly="true" />
                <asp:TemplateField HeaderText="ColB">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("ColB") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("ColB") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:CommandField ButtonType="Button" ShowDeleteButton="True" ShowEditButton="True"
                    ShowInsertButton="True" UpdateText="Save"></asp:CommandField>
            </Columns>
        </asp:GridView>
    

    and

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
        }
    
        protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e)
        {
            // handle row updated
        }
    
        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
        }
    
        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            // handle row command
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Linq query that returns an object of the type IQueryable<ClassX> .
I have a LINQ query that returns data and I want to filter based
I have a LINQ query that returns some object like this... var query =
I have an anonymous linq query that I bind to a datagrid, when I
I have a LINQ query that returns all absences for an employee. The first
I have a LINQ to SQL query that returns a grouped collection of Sponsor
I have a linq query that returns a list of employees and a job
I have the following LINQ query that returns two objects from my database. These
I have a linq query which returns results ordered by first letter. Is there
I have a LINQ query that pulls ID values with integers: var data =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.