By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,677 Members | 1,060 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,677 IT Pros & Developers. It's quick & easy.

Update Datagridview with current data

bplacker
100+
P: 121
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.update(dataset, "tablename"),
and setting the source of the grid to the dataset, but no updates are shown.
Nov 20 '06 #1
Share this Question
Share on Google+
8 Replies


sheenattt
P: 20
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="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>
Nov 21 '06 #2

bplacker
100+
P: 121
Alright, I have set the Primary Key column for the grid, but what is the command to update it?
Nov 21 '06 #3

sheenattt
P: 20
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")

Expand|Select|Wrap|Line Numbers
  1. <asp:GridView ID="GridView1" runat="server" AllowPaging="True" DataSourceID="SqlDataSource1" BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Horizontal">
  2. <Columns>
  3. <asp:CommandField ShowEditButton="True" />
  4. </Columns>
  5. <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
  6. <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
  7. <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
  8. <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
  9. <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
  10. <AlternatingRowStyle BackColor="#F7F7F7" />
  11. </asp:GridView>
  12. <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 %>"> 
  13. <SelectParameters>
  14. </SelectParameters>
  15. <UpdateParameters>
  16. <ASP:PARAMETER Type="Int32" Name="sno" /> </UpdateParameters>
  17.  
  18. </asp:SqlDataSource>
  19.  
Nov 23 '06 #4

bplacker
100+
P: 121
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")

Expand|Select|Wrap|Line Numbers
  1. <asp:GridView ID="GridView1" runat="server" AllowPaging="True" DataSourceID="SqlDataSource1" BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Horizontal">
  2. <Columns>
  3. <asp:CommandField ShowEditButton="True" />
  4. </Columns>
  5. <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
  6. <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
  7. <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
  8. <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
  9. <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
  10. <AlternatingRowStyle BackColor="#F7F7F7" />
  11. </asp:GridView>
  12. <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 %>"> 
  13. <SelectParameters>
  14. </SelectParameters>
  15. <UpdateParameters>
  16. <ASP:PARAMETER Type="Int32" Name="sno" /> </UpdateParameters>
  17.  
  18. </asp:SqlDataSource>
  19.  

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!!!
Nov 27 '06 #5

Frinavale
Expert Mod 5K+
P: 9,731
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!
Nov 27 '06 #6

milonov
P: 32
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
Nov 28 '06 #7

bplacker
100+
P: 121
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?!
Nov 28 '06 #8

milonov
P: 32
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, "notification services". Perhaps it may help also.

Best Regards,
Michael Milonov


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?!
Nov 29 '06 #9

Post your reply

Sign in to post your reply or Sign up for a free account.