aspnet and C#: How to link a TemplateField to a Dynamically Created Anonymous Type?

1.9k views Asked by At

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

There are 1 answers

3
Bala R On BEST ANSWER

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
    }