asp.netdatatabledatatablesaspxgridviewtemplatefield

Not able to add template Field in grid view in visual studio 2022


I am trying to insert template field in grid view. The process is on the Design page click on grid view element > edit columns > add template field > ok. The problem is the template field can neither be seen on the design part of the code nor on the source code.

 <div class="row">
                            <asp:SqlDataSource ID="SqlDataSource1" runat="server" OnSelecting="SqlDataSource1_Selecting1" ConnectionString="<%$ ConnectionStrings:elibraryDBConnectionString %>" SelectCommand="SELECT * FROM [book_master_tbl]"></asp:SqlDataSource>
                            <div class="col">
                                <asp:GridView ID="GridView1" AlternatingRowStyle-CssClass="table table-striped table-bordered" runat="server" OnSelectedIndexChanged="GridView1_SelectedIndexChanged2" AutoGenerateColumns="False" DataKeyNames="book_id" DataSourceID="SqlDataSource1">
                                    <AlternatingRowStyle CssClass="table table-striped table-bordered"></AlternatingRowStyle>
                                    <Columns>
                                        <asp:BoundField DataField="book_id" HeaderText="book_id" ReadOnly="True" SortExpression="book_id"></asp:BoundField>
                                        
                                    </Columns>

                                </asp:GridView>
                            </div>
                        </div>

Solution

  • Ok, something then must be messed up with your setup.

    I assume the designer looks ok?

    I just cut + pasted in your above markup. When I go edit columns, I am able to add a new column without issue (but really, no, really really get rid of the alternating column, since your bootstrap class of table-striped will handle that issue).

    but, a simple cut + paste, and when I choose add-fields, I see this:

    enter image description here

    And if I flip the page view into designer (not split), then I see this:

    enter image description here

    so, it not clear why you can't use edit columns. As noted, you can remove (should remove) the alternating template - but even as above shows, leaving it it, I am able to use the edit columns just fine, and added a new column called Hotelname, it it worked/looks fine.