PriceCheaperton
PriceCheaperton

Reputation: 5349

GridView RowIndex is 0

I have a GridView on my page and I click on Edit, it displays my editable text box but when I edit the value and press Update it errors:

The error suggests that GridView2.DataKeys is null.

I adding some extra debugging by:

int test = e.RowIndex

and this gives me a value of 0

My code is below:

Can you suggest why im getting:

An exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll but was not handled in user code

Additional information: Index was out of range. Must be non-negative and less than the size of the collection.

protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e)

{
    int DataKeyValue = Convert.ToInt32(GridView2.DataKeys[e.RowIndex].Value.ToString());
    GridViewRow row = (GridViewRow)GridView2.Rows[e.RowIndex];
    Label lblID = (Label)row.FindControl("lblID");

    TextBox GVtxtNextStep = (TextBox)row.Cells[0].Controls[0];

    GridView2.EditIndex = -1;

    cobj.SupportRef1 = txtSupportRef.Text;
    cobj.NextStep1 = txtNextStep.Text;


    bobj.MicroTicketUpdate(cobj);

    GridView2.DataBind();

}

Upvotes: 0

Views: 2536

Answers (2)

Denys Wessels
Denys Wessels

Reputation: 17039

You need to set the DataKeyNames property on your GridView control to the relevant ID property e.g. <asp:GridView DataKeyNames="PersonID"...

EDIT

Below is a complete example which will return a valid primary key value in the DataKeyValue variable.You can copy this example, as is, into a new web page in your web application and test it to see how it works and how the data get's bound and I hope by looking at this example you can figure out the changes you should make to your code to get it working:

.ASPX:

<asp:GridView 
    DataKeyNames="ID" 
    ID="GridView1" 
    runat="server" 
    AutoGenerateColumns="false" 
    AutoGenerateEditButton="True" 
    OnRowEditing="GridView1_RowEditing" 
    OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit">
    <Columns>
        <asp:BoundField DataField="Name" HeaderText="Name" />
        <asp:BoundField DataField="CreditRequest" HeaderText="Credit Request" />
    </Columns>
</asp:GridView>

Code behind:

 public class PersonT
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string CreditRequest { get; set; }
    }

    public partial class aaaa_GridViewRowUpdating : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                this.BindData();
            }
        }

        private void BindData()
        {
            var p1 = new PersonT() { ID = 10, Name = "Person 1", CreditRequest = "Credit Request 1" };
            var p2 = new PersonT() { ID = 20, Name = "Person 2", CreditRequest = "Credit Request 2" };

            var list = new List<PersonT> { p1, p2 };
            GridView1.DataSource = list;
            GridView1.DataBind();
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            BindData();
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            System.Diagnostics.Debugger.Break();
            int index = e.RowIndex;
            int DataKeyValue = Convert.ToInt32(GridView1.DataKeys[index].Value.ToString());
        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            BindData();
        }
    }

Upvotes: 2

Shon
Shon

Reputation: 486

if yo click outside of the rows but on the data view the row index which is returned is -1. You have to check if this occurs and break out of the code if it does eg.

if (e.RowIndex<0&&e.RowIndex>=gridiew.Rows.Count)//have to account for the blan row
return;

Upvotes: 0

Related Questions