C# .NET Gridview

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2006
Posts: 4
Reputation: Richard Lelle is an unknown quantity at this point 
Solved Threads: 0
Richard Lelle Richard Lelle is offline Offline
Newbie Poster

C# .NET Gridview

 
0
  #1
Feb 7th, 2007
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?
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 79
Reputation: nikkiH is an unknown quantity at this point 
Solved Threads: 4
nikkiH's Avatar
nikkiH nikkiH is offline Offline
Junior Poster in Training

Re: C# .NET Gridview

 
0
  #2
Feb 12th, 2007
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?
Google is your friend. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
Bored? Visit http://www.kaelisspace.com/
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 4
Reputation: Richard Lelle is an unknown quantity at this point 
Solved Threads: 0
Richard Lelle Richard Lelle is offline Offline
Newbie Poster

Re: C# .NET Gridview

 
0
  #3
Feb 12th, 2007
I am using sqldatasource.

Originally Posted by nikkiH View Post
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?
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 79
Reputation: nikkiH is an unknown quantity at this point 
Solved Threads: 4
nikkiH's Avatar
nikkiH nikkiH is offline Offline
Junior Poster in Training

Re: C# .NET Gridview

 
0
  #4
Feb 12th, 2007
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.
Google is your friend. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
Bored? Visit http://www.kaelisspace.com/
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 4
Reputation: Richard Lelle is an unknown quantity at this point 
Solved Threads: 0
Richard Lelle Richard Lelle is offline Offline
Newbie Poster

Re: C# .NET Gridview

 
0
  #5
Feb 12th, 2007
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:
  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.....

Originally Posted by nikkiH View Post
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?
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 79
Reputation: nikkiH is an unknown quantity at this point 
Solved Threads: 4
nikkiH's Avatar
nikkiH nikkiH is offline Offline
Junior Poster in Training

Re: C# .NET Gridview

 
0
  #6
Feb 12th, 2007
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.
  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>

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

Google is your friend. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
Bored? Visit http://www.kaelisspace.com/
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC