sam
sam

Reputation: 29

How to update gridview through design code/ aspx page code

I'm trying to update GridView every time I update, delete or insert any record. Now I have provided sql data source code for GridView in Design Code not form code. Now how can I update it from there ? When I write GridView1.databind() on form code, it says

> Both DataSource and DataSourceID are defined on 'GridView1'. Remove one definition.

Can someone tell me how to DataBind in design view, to update GridView every time I insert/update/delete record ?

Here is GridView1 Code

           <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>

Upvotes: 0

Views: 85

Answers (2)

sam
sam

Reputation: 29

That error was coming because data was being binded on both Design view code as well as web form backend code. I simply added GridView1.DataSourceID = null;

and that did the job.

Upvotes: 0

MaxPayne999
MaxPayne999

Reputation: 184

Set the DataSourceID to null.

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

Upvotes: 2

Related Questions