943,633 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 82160
  • C# RSS
Feb 7th, 2007
0

C# .NET Gridview

Expand Post »
I am still pretty new to C# and .NET. I am using .NET 2005, and am attempting to use a Gridview. This GV displays data from two tables, one dependant on the other. I cannot, however, get the GV to Edit/Delete a specific column in any row. I was able to make a GV that displays data from only one table, and I got it to Edit/Delete data. Is there something special that must be done to get 'connected tables' data to be modifiable? Or are the GV's just designed for data modification for one table, but can display data for more than one table?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Richard Lelle is offline Offline
4 posts
since May 2006
Feb 12th, 2007
0

Re: C# .NET Gridview

Are you using sqldatasource as you need to be doing for GridView to work properly, or are you trying this the "old" way with sqldataadapters, etc?
Reputation Points: 13
Solved Threads: 4
Junior Poster in Training
nikkiH is offline Offline
79 posts
since Dec 2006
Feb 12th, 2007
0

Re: C# .NET Gridview

I am using sqldatasource.

Click to Expand / Collapse  Quote originally posted by nikkiH ...
Are you using sqldatasource as you need to be doing for GridView to work properly, or are you trying this the "old" way with sqldataadapters, etc?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Richard Lelle is offline Offline
4 posts
since May 2006
Feb 12th, 2007
0

Re: C# .NET Gridview

Okay, can you elaborate on this?
"I cannot, however, get the GV to Edit/Delete a specific column in any row"

What do you mean by "cannot"? Do you get an error? Does it just not update anything? does the edit button not push it into edit mode? Or ...?
And what do you mean by specific column?
Last edited by nikkiH; Feb 12th, 2007 at 4:01 pm.
Reputation Points: 13
Solved Threads: 4
Junior Poster in Training
nikkiH is offline Offline
79 posts
since Dec 2006
Feb 12th, 2007
0

Re: C# .NET Gridview

My current error is: 'Index was out of range. Must be non-negative and less than the size of the collection.' It blows up on the line of C# code:
C# Syntax (Toggle Plain Text)
  1. GridViewRow
 row = GridView.SelectedRow; 


I know that I need to supply an index number. What would I do with the index number if/when I get it?

As far as 'specific column' goes.....actually, there is only one column that I am trying to update.....

Click to Expand / Collapse  Quote originally posted by nikkiH ...
Okay, can you elaborate on this?
"I cannot, however, get the GV to Edit/Delete a specific column in any row"

What do you mean by "cannot"? Do you get an error? Does it just not update anything? does the edit button not push it into edit mode? Or ...?
And what do you mean by specific column?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Richard Lelle is offline Offline
4 posts
since May 2006
Feb 12th, 2007
0

Re: C# .NET Gridview

row = GridView.SelectedRow;

SelectedRow?
Where are you trying to put that? Unless you make someone first select a row before being able to edit it, it isn't the right thing to use.

In general, GridView is very much out of the box as far as editing and whatnot.

Here's a small example using Northwind.
C# Syntax (Toggle Plain Text)
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="_Default2" %>
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4.  
  5. <html xmlns="http://www.w3.org/1999/xhtml" >
  6. <head>
  7. <title>Untitled Page</title>
  8.  
  9. </head>
  10. <body>
  11. <form runat="server" id="Form1">
  12. <div>
  13. <asp:SqlDataSource ID="sda_customers" runat="server" ConnectionString='<%$ ConnectionStrings:NORTHWIND %> '
  14. SelectCommand="SELECT [CustomerID], [CompanyName], [ContactName], [ContactTitle], [Address], [City], [Region], [PostalCode], [Country], [Phone], [Fax] FROM [Northwind].[dbo].[Customers]"
  15. UpdateCommand="UPDATE [Northwind].[dbo].[Customers] SET [CompanyName]=@CompanyName, [ContactName]=@ContactName, [ContactTitle]=@ContactTitle, [Address]=@Address,[City]=@City, [Region]=@Region,[PostalCode]=@PostalCode,[Country]=@Country, [Phone]=@Phone, [Fax]=@Fax WHERE [CustomerID]=@CustomerID"
  16. ProviderName="System.Data.SqlClient"
  17. >
  18. </asp:SqlDataSource>
  19.  
  20. <asp:Label ID="Label1" runat="server"></asp:Label><br />
  21. <br />
  22. Filter:
  23. <asp:TextBox ID="txtFilterExpression" runat="server"></asp:TextBox>
  24. &nbsp;
  25. <asp:Button ID="btnFilter" runat="server" OnClick="btnFilter_Click" Text="Button" /><br />
  26. &nbsp;
  27. <asp:GridView ID="gvCustomers" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="true"
  28. DataSourceID="sda_customers" DataKeyNames="CustomerID" OnRowCommand="gvCustomers_RowCommand" OnRowUpdated="gvCustomers_RowUpdated" AutoGenerateEditButton="True" OnRowUpdating="gvCustomers_RowUpdating">
  29. </asp:GridView>
  30. </div>
  31.  
  32. </form>
  33. </body>
  34. </html>

C# Syntax (Toggle Plain Text)
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Web;
  5. using System.Web.Security;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. using System.Web.UI.WebControls.WebParts;
  9. using System.Web.UI.HtmlControls;
  10.  
  11. public partial class _Default2 : System.Web.UI.Page
  12. {
  13.  
  14. protected void Page_Load(object sender, EventArgs e)
  15. {
  16. if (IsPostBack)
  17. {
  18. sda_customers.FilterExpression = txtFilterExpression.Text;
  19. if (ViewState["updating"] == null ||
  20. ViewState["updating"].ToString() == "false")
  21. gvCustomers.DataBind();
  22.  
  23. string key = (string)ViewState["editing"];
  24. int selectedindex = 0;
  25. bool found = false;
  26. if (key != null)
  27. {
  28. foreach (DataKey dataKey in gvCustomers.DataKeys)
  29. {
  30. if (dataKey.Value.ToString() == key)
  31. {
  32. gvCustomers.EditIndex = selectedindex;
  33. found = true;
  34. break;
  35. }
  36. else selectedindex += 1;
  37. }
  38. if (!found) gvCustomers.EditIndex = -1;
  39. }
  40. }
  41.  
  42. }
  43. protected void btnFilter_Click(object sender, EventArgs e)
  44. {
  45.  
  46. }
  47.  
  48. protected void gvCustomers_RowCommand(object sender, GridViewCommandEventArgs e)
  49. {
  50. if (e.CommandName == "Edit") ViewState["editing"] = gvCustomers.DataKeys[Convert.ToInt32(e.CommandArgument)].Value;
  51. if (e.CommandName == "Cancel") ViewState["editing"] = null;
  52.  
  53. if (e.CommandName == "Update") ViewState["updating"] = "true";
  54. else ViewState["updating"] = "false";
  55.  
  56. Label1.Text = "key: " + (string)ViewState["editing"];
  57. Label1.Text += " is updating: " + (string)ViewState["updating"];
  58.  
  59. }
  60. protected void gvCustomers_RowUpdated(object sender, GridViewUpdatedEventArgs e)
  61. {
  62. ViewState["editing"] = null;
  63. ViewState["updating"] = "false";
  64. Label1.Text = "key: " + (string)ViewState["editing"];
  65. Label1.Text += " is updating: " + (string)ViewState["updating"];
  66. }
  67.  
  68. protected void gvCustomers_RowUpdating(object sender, GridViewUpdateEventArgs e)
  69. {
  70. string s = e.Keys[0].ToString();
  71. s = e.NewValues[0].ToString();
  72.  
  73. }
  74. }

Reputation Points: 13
Solved Threads: 4
Junior Poster in Training
nikkiH is offline Offline
79 posts
since Dec 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Directx or GDI+
Next Thread in C# Forum Timeline: InvalidOperationException was unhandled by user code.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC