Update Datagridview with current data

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bplacker
    New Member
    • Sep 2006
    • 121

    Update Datagridview with current data

    I have a datagridview which is filled with data from a table in a database. In the grid, the user clicks a row to edit that specific record. An edit screen is brought up, which has a text box for each field in the table, and the user can edit the data accordingly.

    My question is... once the user is done editing, how can I reload/update the data in the datagridview without completely reloading the dataadapter and re-setting the source of the grid?

    I tried

    dataAdapter.upd ate(dataset, "tablename" ),
    and setting the source of the grid to the dataset, but no updates are shown.
  • sheenattt
    New Member
    • Nov 2006
    • 20

    #2
    You have to specify the key field thru which update could be made,like Ive specified here "sno"(here it is a parameter,it could be a control also)

    <asp:GridView ID="GridView1" runat="server" AllowPaging="Tr ue" DataSourceID="S qlDataSource1" BackColor="Whit e" BorderColor="#E 7E7FF" BorderStyle="No ne" BorderWidth="1p x" CellPadding="3" GridLines="Hori zontal">
    <Columns>
    <asp:CommandFie ld ShowEditButton= "True" />
    </Columns>
    <FooterStyle BackColor="#B5C 7DE" ForeColor="#4A3 C8C" />
    <RowStyle BackColor="#E7E 7FF" ForeColor="#4A3 C8C" />
    <SelectedRowSty le BackColor="#738 A9C" Font-Bold="True" ForeColor="#F7F 7F7" />
    <PagerStyle BackColor="#E7E 7FF" ForeColor="#4A3 C8C" HorizontalAlign ="Right" />
    <HeaderStyle BackColor="#4A3 C8C" Font-Bold="True" ForeColor="#F7F 7F7" />
    <AlternatingRow Style BackColor="#F7F 7F7" />
    </asp:GridView>
    <asp:SqlDataSou rce ID="SqlDataSour ce1" runat="server" ConnectionStrin g="<%$ ConnectionStrin gs:ConnectionSt ring2 %>"UpdateComman d="UPDATE [tablename] SET [location] = @location where (([sno]=@sno))" SelectCommand=" SELECT [sno], [location] FROM [tablename]" ConnectionStrin g="<%$ ConnectionStrin gs:ConnectionSt ring2 %>">
    <SelectParamete rs>
    </SelectParameter s>
    <UpdateParamete rs>
    <ASP:PARAMETE R Type="Int32" Name="sno" /> </UpdateParameter s>

    </asp:SqlDataSour ce>

    Comment

    • bplacker
      New Member
      • Sep 2006
      • 121

      #3
      Alright, I have set the Primary Key column for the grid, but what is the command to update it?

      Comment

      • sheenattt
        New Member
        • Nov 2006
        • 20

        #4
        the update command is thr see carefully..all u have to do is to set ur primary key or whatever condition in the where field(like ive done with "sno")

        Code:
        <asp:GridView ID="GridView1" runat="server" AllowPaging="True" DataSourceID="SqlDataSource1" BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Horizontal">
        <Columns>
        <asp:CommandField ShowEditButton="True" />
        </Columns>
        <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
        <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
        <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
        <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
        <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
        <AlternatingRowStyle BackColor="#F7F7F7" />
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString2 %>"UpdateCommand="UPDATE [tablename] SET [location] = @location where (([sno]=@sno))" SelectCommand="SELECT [sno], [location] FROM [tablename]" ConnectionString="<%$ ConnectionStrings:ConnectionString2 %>"> 
        <SelectParameters>
        </SelectParameters>
        <UpdateParameters>
        <ASP:PARAMETER Type="Int32" Name="sno" /> </UpdateParameters>
        
        </asp:SqlDataSource>

        Comment

        • bplacker
          New Member
          • Sep 2006
          • 121

          #5
          Originally posted by sheenattt
          the update command is thr see carefully..all u have to do is to set ur primary key or whatever condition in the where field(like ive done with "sno")

          Code:
          <asp:GridView ID="GridView1" runat="server" AllowPaging="True" DataSourceID="SqlDataSource1" BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Horizontal">
          <Columns>
          <asp:CommandField ShowEditButton="True" />
          </Columns>
          <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
          <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
          <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
          <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
          <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
          <AlternatingRowStyle BackColor="#F7F7F7" />
          </asp:GridView>
          <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString2 %>"UpdateCommand="UPDATE [tablename] SET [location] = @location where (([sno]=@sno))" SelectCommand="SELECT [sno], [location] FROM [tablename]" ConnectionString="<%$ ConnectionStrings:ConnectionString2 %>"> 
          <SelectParameters>
          </SelectParameters>
          <UpdateParameters>
          <ASP:PARAMETER Type="Int32" Name="sno" /> </UpdateParameters>
          
          </asp:SqlDataSource>

          no no no, I don't want to use an update command. This is a very primitive way of doing this, and requires much more code!
          Plus, I don't have a problem updating the database, what I need is to be able to update my datagridview without reloading the whole form, because this is very slow. Certainly someone out there has tried to do this before!!!

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            Originally posted by bplacker
            no no no, I don't want to use an update command. This is a very primitive way of doing this, and requires much more code!
            Plus, I don't have a problem updating the database, what I need is to be able to update my datagridview without reloading the whole form, because this is very slow. Certainly someone out there has tried to do this before!!!
            I've asked the same question a long time ago and never found a way to make this work. Everything I have tried hasn't worked...it seems as if the only way to update the dataGridView is to recreate the dataView (or whatever source you're using for the dataGrid)

            My problem isn't that it takes too long to recreate the page, it's that I don't want to recreate my dataView becuase the user could have sorted it...recreating it would destroy whatever sort has been done on the view.

            All I want to do, as I'm assuming all you want to do, is just change that one row in the dataView without recreating it. It doesn't seem like an unreasonable thing to do but everything I've tried thus far hasn't worked

            If you figure this out please send me a PM

            Thanks a lot!

            Comment

            • milonov
              New Member
              • Oct 2006
              • 32

              #7
              Hello, guys.
              Yep it's a problem. Some times ago I also looked for the solution, and have made the one but a bit specific: it’s suitable for .net desktop applications and only for Oracle database (of course it is temporary restriction).
              Anyway it exists http://www.snotratech.com/snocnet.html :)

              Best Regards,
              Michael Milonov

              Comment

              • bplacker
                New Member
                • Sep 2006
                • 121

                #8
                Well thats fine and dandy, but I'm using microsoft sql server. I agree!! it seems like a very simple thing to do, and something that the developers would have seen useful, but I guess not!! I will keep digging, but for now I guess there is no answer?!

                Comment

                • milonov
                  New Member
                  • Oct 2006
                  • 32

                  #9
                  Anyway, concerning server-generated values this article may help:
                  http://www.devx.com/codemag/Article/20138/0/page/4

                  and if you use SQL server I heard about a new feature of SQL Server 2005, "notificati on services". Perhaps it may help also.

                  Best Regards,
                  Michael Milonov


                  Originally posted by bplacker
                  Well thats fine and dandy, but I'm using microsoft sql server. I agree!! it seems like a very simple thing to do, and something that the developers would have seen useful, but I guess not!! I will keep digging, but for now I guess there is no answer?!

                  Comment

                  Working...