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?

I am using sqldatasource.

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?

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?

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:

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.

<%@ 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>
        &nbsp;
        <asp:Button ID="btnFilter" runat="server" OnClick="btnFilter_Click" Text="Button" /><br />
        &nbsp;
        <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>
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();

    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.