How to update Gridview

Discussion in 'ASP .Net' started by RAB, Jan 30, 2010.

  1. RAB

    RAB Guest

    I have created a web site using asp.net 2.0. One of my pages has the
    following code:

    <%@ Page Language="VB" MasterPageFile="~/Default.master"
    AutoEventWireup="false" CodeFile="Add_Dept.aspx.vb"
    Inherits="Sched_Admin_Add_Dept" title="Untitled Page" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="Main" Runat="Server">
    <br />


    <asp:GridView ID="GridView1" runat="server" AllowPaging="True"
    AllowSorting="True"
    AutoGenerateColumns="False" DataKeyNames="Dept_Id"
    DataSourceID="SqlDataSource_GetDepartments">
    <Columns>
    <asp:CommandField ShowDeleteButton="True"
    ShowEditButton="True" ShowSelectButton="True" />
    <asp:BoundField DataField="Dept_Id" HeaderText="Dept_Id"
    InsertVisible="False" ReadOnly="True"
    SortExpression="Dept_Id" />
    <asp:BoundField DataField="Dept_Name"
    HeaderText="Dept_Name" SortExpression="Dept_Name" />
    </Columns>
    </asp:GridView>

    <asp:SqlDataSource ID="SqlDataSource_GetDepartments" runat="server"
    ConflictDetection="CompareAllValues"
    ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
    DeleteCommand="DELETE FROM [Departments] WHERE [Dept_Id] =
    @original_Dept_Id AND [Dept_Name] = @original_Dept_Name"
    InsertCommand="INSERT INTO [Departments] ([Dept_Name]) VALUES
    (@Dept_Name)" OldValuesParameterFormatString="original_{0}"
    SelectCommand="SELECT * FROM [Departments] ORDER BY
    [Dept_Name]" UpdateCommand="UPDATE [Departments] SET [Dept_Name] =
    @Dept_Name WHERE [Dept_Id] = @original_Dept_Id AND [Dept_Name] =
    @original_Dept_Name">
    <DeleteParameters>
    <asp:parameter Name="original_Dept_Id" Type="Int32" />
    <asp:parameter Name="original_Dept_Name" Type="String" />
    </DeleteParameters>
    <UpdateParameters>
    <asp:parameter Name="Dept_Name" Type="String" />
    <asp:parameter Name="original_Dept_Id" Type="Int32" />
    <asp:parameter Name="original_Dept_Name" Type="String" />
    </UpdateParameters>
    <InsertParameters>
    <asp:parameter Name="Dept_Name" Type="String" />
    </InsertParameters>
    </asp:SqlDataSource>
    <br />

    <asp:DetailsView ID="DetailsView1" runat="server"
    AutoGenerateRows="False"
    DataKeyNames="Dept_Id"
    DataSourceID="SqlDataSource_AddDepartments" Height="50px"
    Width="125px" CellPadding="4" ForeColor="#333333"
    GridLines="None" DefaultMode="Insert">
    <Fields>
    <asp:BoundField DataField="Dept_Id" HeaderText="Dept_Id"
    InsertVisible="False" ReadOnly="True"
    SortExpression="Dept_Id" Visible="False" />
    <asp:BoundField DataField="Dept_Name"
    HeaderText="Department Name" SortExpression="Dept_Name" />
    <asp:CommandField ShowInsertButton="True" />
    </Fields>
    <FooterStyle BackColor="#507CD1" Font-Bold="True"
    ForeColor="White" />
    <CommandRowStyle BackColor="#D1DDF1" Font-Bold="True" />
    <RowStyle BackColor="#EFF3FB" />
    <FieldHeaderStyle BackColor="#DEE8F5" Font-Bold="True" />
    <PagerStyle BackColor="#2461BF" ForeColor="White"
    HorizontalAlign="Center" />
    <HeaderStyle BackColor="#507CD1" Font-Bold="True"
    ForeColor="White" />
    <EditRowStyle BackColor="#2461BF" />
    <AlternatingRowStyle BackColor="White" />
    </asp:DetailsView>

    <asp:SqlDataSource ID="SqlDataSource_AddDepartments" runat="server"
    ConflictDetection="CompareAllValues"
    ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
    DeleteCommand="DELETE FROM [Departments] WHERE [Dept_Id] =
    @original_Dept_Id AND [Dept_Name] = @original_Dept_Name"
    InsertCommand="INSERT INTO [Departments] ([Dept_Name]) VALUES
    (@Dept_Name)" OldValuesParameterFormatString="original_{0}"
    SelectCommand="SELECT * FROM [Departments]"
    UpdateCommand="UPDATE [Departments] SET [Dept_Name] = @Dept_Name WHERE
    [Dept_Id] = @original_Dept_Id AND [Dept_Name] = @original_Dept_Name">
    <DeleteParameters>
    <asp:parameter Name="original_Dept_Id" Type="Int32" />
    <asp:parameter Name="original_Dept_Name" Type="String" />
    </DeleteParameters>
    <UpdateParameters>
    <asp:parameter Name="Dept_Name" Type="String" />
    <asp:parameter Name="original_Dept_Id" Type="Int32" />
    <asp:parameter Name="original_Dept_Name" Type="String" />
    </UpdateParameters>
    <InsertParameters>
    <asp:parameter Name="Dept_Name" Type="String" />
    </InsertParameters>
    </asp:SqlDataSource>
    <br />
    <br />
    <br />
    &nbsp;<br />
    <br />
    </asp:Content>

    After I insert a new department into the database using the
    Detailsview how can the new department be seen in the Gridview
    without having to refresh the page?

    Thanks in advance,
    RABMissouri2010
    RAB, Jan 30, 2010
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hartmut Schroth
    Replies:
    3
    Views:
    3,481
    Hartmut Schroth
    Dec 1, 2003
  2. joun
    Replies:
    9
    Views:
    2,217
    W.G. Ryan eMVP
    Nov 30, 2004
  3. =?Utf-8?B?bWdvbnphbGVzMw==?=

    GridView Hierarchical View - Gridview in Gridview

    =?Utf-8?B?bWdvbnphbGVzMw==?=, May 9, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    17,934
    =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=
    May 9, 2006
  4. mesut
    Replies:
    0
    Views:
    647
    mesut
    Feb 19, 2007
  5. Evan M.
    Replies:
    1
    Views:
    1,179
    Evan M.
    Apr 18, 2007
Loading...

Share This Page