| | |
C# .NET Gridview
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: May 2006
Posts: 4
Reputation:
Solved Threads: 0
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?
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/
Bored? Visit http://www.kaelisspace.com/
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?
"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/
Bored? Visit http://www.kaelisspace.com/
•
•
Join Date: May 2006
Posts: 4
Reputation:
Solved Threads: 0
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:
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.....
C# Syntax (Toggle Plain Text)
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.....
•
•
•
•
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?
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.
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)
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="_Default2" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head> <title>Untitled Page</title> </head> <body> <form runat="server" id="Form1"> <div> <asp:SqlDataSource ID="sda_customers" runat="server" ConnectionString='<%$ ConnectionStrings:NORTHWIND %> ' SelectCommand="SELECT [CustomerID], [CompanyName], [ContactName], [ContactTitle], [Address], [City], [Region], [PostalCode], [Country], [Phone], [Fax] FROM [Northwind].[dbo].[Customers]" 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" ProviderName="System.Data.SqlClient" > </asp:SqlDataSource> <asp:Label ID="Label1" runat="server"></asp:Label><br /> <br /> Filter: <asp:TextBox ID="txtFilterExpression" runat="server"></asp:TextBox> <asp:Button ID="btnFilter" runat="server" OnClick="btnFilter_Click" Text="Button" /><br /> <asp:GridView ID="gvCustomers" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="true" DataSourceID="sda_customers" DataKeyNames="CustomerID" OnRowCommand="gvCustomers_RowCommand" OnRowUpdated="gvCustomers_RowUpdated" AutoGenerateEditButton="True" OnRowUpdating="gvCustomers_RowUpdating"> </asp:GridView> </div> </form> </body> </html>
C# Syntax (Toggle Plain Text)
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class _Default2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (IsPostBack) { sda_customers.FilterExpression = txtFilterExpression.Text; if (ViewState["updating"] == null || ViewState["updating"].ToString() == "false") gvCustomers.DataBind(); string key = (string)ViewState["editing"]; int selectedindex = 0; bool found = false; if (key != null) { foreach (DataKey dataKey in gvCustomers.DataKeys) { if (dataKey.Value.ToString() == key) { gvCustomers.EditIndex = selectedindex; found = true; break; } else selectedindex += 1; } if (!found) gvCustomers.EditIndex = -1; } } } protected void btnFilter_Click(object sender, EventArgs e) { } protected void gvCustomers_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "Edit") ViewState["editing"] = gvCustomers.DataKeys[Convert.ToInt32(e.CommandArgument)].Value; if (e.CommandName == "Cancel") ViewState["editing"] = null; if (e.CommandName == "Update") ViewState["updating"] = "true"; else ViewState["updating"] = "false"; Label1.Text = "key: " + (string)ViewState["editing"]; Label1.Text += " is updating: " + (string)ViewState["updating"]; } protected void gvCustomers_RowUpdated(object sender, GridViewUpdatedEventArgs e) { ViewState["editing"] = null; ViewState["updating"] = "false"; Label1.Text = "key: " + (string)ViewState["editing"]; Label1.Text += " is updating: " + (string)ViewState["updating"]; } protected void gvCustomers_RowUpdating(object sender, GridViewUpdateEventArgs e) { string s = e.Keys[0].ToString(); s = e.NewValues[0].ToString(); } }
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/
Bored? Visit http://www.kaelisspace.com/
![]() |
Similar Threads
- Adding color to GridView (ASP.NET)
- ASP.NET GridView order of operation (C#)
- POpulate DataGrid (VB.NET)
Other Threads in the C# Forum
- Previous Thread: Array in struct
- Next Thread: InvalidOperationException was unhandled by user code.
| Thread Tools | Search this Thread |
.net access ado.net algorithm array barchart bitmap box broadcast buttons c# check checkbox client color combobox control conversion csharp custom cyclethruopenforms data database datagrid datagridview dataset datetime degrees development draganddrop drawing encryption enum event excel file files finalyearproject form format forms function gdi+ getoutlookcontactusinfcsvfile globalization httpwebrequest image index input install java label list listbox listener mandelbrot math mono mouseclick mysql operator path photoshop picturebox pixelinversion post programming radians regex remote remoting richtextbox save serialization server sleep socket sql sql-server statistics stream string table tcp text textbox thread time timer update upload usercontrol validate validation visualstudio webbrowser windows winforms wpf xml





