如何通过设计代码/aspx页面代码更新gridview

How to update gridview through design code/ aspx page code

每次更新、删除或插入任何记录时,我都会尝试更新 GridView。现在我在设计代码中为 GridView 提供了 sql 数据源代码,而不是表单代码。现在我怎样才能从那里更新它?当我在表单代码上写 GridView1.databind() 时,它说

> DataSource 和 DataSourceID 都在 'GridView1' 上定义。删除一个定义。

谁能告诉我如何在设计视图中进行 DataBind,以便在每次 insert/update/delete 记录时更新 GridView?

这是GridView1的代码

           <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AutoGenerateSelectButton="True" BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" DataSourceID="SqlDataSource1" ForeColor="Black" GridLines="Vertical" Width="284px">
                    <AlternatingRowStyle BackColor="#CCCCCC" />
                    <Columns>
                        <asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
                        <asp:BoundField DataField="surname" HeaderText="surname" SortExpression="surname" />
                        <asp:BoundField DataField="amount" HeaderText="amount" SortExpression="amount" />
                    </Columns>
                    <FooterStyle BackColor="#CCCCCC" />
                    <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
                    <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
                    <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
                    <SortedAscendingCellStyle BackColor="#F1F1F1" />
                    <SortedAscendingHeaderStyle BackColor="#808080" />
                    <SortedDescendingCellStyle BackColor="#CAC9C9" />
                    <SortedDescendingHeaderStyle BackColor="#383838" />
                </asp:GridView>
                <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:myDbConnectionString %>" SelectCommand="SELECT [name], [surname], [amount] FROM [Table1]"></asp:SqlDataSource>

将 DataSourceID 设置为空。

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

出现该错误是因为数据绑定在设计视图代码和 Web 表单后端代码上。 我只是添加了 GridView1.DataSourceID = null;

这就完成了工作。