GridView RowIndex为0

时间:2016-05-28 11:48:53

标签: c# asp.net .net gridview

我的页面上有一个GridView,我点击了Edit,它显示了我的可编辑文本框,但是当我编辑该值并按Update时出现错误:

该错误表明GridView2.DataKeysnull

我通过以下方式添加了一些额外的调试:

int test = e.RowIndex

这给了我0

的值

我的代码如下:

你能说明为什么我会:

  

发生了'System.ArgumentOutOfRangeException'类型的异常   mscorlib.dll但未在用户代码中处理

     

其他信息:指数超出范围。必须是非负面的   并且小于集合的大小。

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();

}

2 个答案:

答案 0 :(得分:2)

您需要将GridView控件上的DataKeyNames属性设置为相关的ID属性,例如<asp:GridView DataKeyNames="PersonID" ...

修改

下面是一个完整的示例,它将在DataKeyValue变量中返回有效的主键值。您可以将此示例按原样复制到Web应用程序的新网页中并对其进行测试以查看其如何工作以及如何限制数据,我希望通过查看这个示例,您可以找出应该对代码进行的更改以使其正常工作:

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

代码背后:

 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();
        }
    }

答案 1 :(得分:0)

如果你在行之外点击但是在数据视图上,返回的行索引是-1。您必须检查是否发生这种情况,如果出现这种情况,则必须打破代码。

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