0

我有一个简单的gridview,2列。第一列是一个数值。第二列基于从下拉列表中选择的值。我有下拉列表工作,但是当我去更新表格时,我收到一个错误: 索引超出范围。必须是非负数且小于集合的大小。参数名称:索引

违规行是 55

Line 53:         {
Line 54:             string BU = (gvSummary.Rows[e.RowIndex].FindControl("dlBU") as DropDownList).SelectedItem.Value;
Line 55:             string AnnoNum = gvSummary.DataKeys[e.RowIndex].Value.ToString();

这是网络表单背后的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Security;
using System.Configuration;

namespace SHCAnnotation
{
    public partial class WebForm2 : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.BindData();
            }
        }

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

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

        protected void RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow && gvSummary.EditIndex == e.Row.RowIndex)
            {
                DropDownList dlBU = (DropDownList)e.Row.FindControl("dlBU");
                string query = "select distinct Unit from vw_KmartBU";
                SqlCommand cmd = new SqlCommand(query);
                dlBU.DataSource = GetData(cmd);
                dlBU.DataTextField = "Unit";
                dlBU.DataValueField = "Unit";
                dlBU.DataBind();
                //dlBU.Items.FindByValue((e.Row.FindControl("lblBU") as Label).Text).Selected = true;
            }
        }

        protected void UpdateSummary(object sender, GridViewUpdateEventArgs e)
        {
            string BU = (gvSummary.Rows[e.RowIndex].FindControl("dlBU") as DropDownList).SelectedItem.Value;
            string AnnoNum = gvSummary.DataKeys[e.RowIndex].Value.ToString();
            string strConnString = ConfigurationManager.ConnectionStrings["SRM_MetricConnectionString"].ConnectionString;
            using (SqlConnection con = new SqlConnection(strConnString))
            {
                string query = "update vw_GridviewSource set [Business Unit] = @BU where [Annotation Number] = @AnnoNum";
                using (SqlCommand cmd = new SqlCommand(query))
                {
                    cmd.Connection = con;
                    cmd.Parameters.AddWithValue("@BU", BU);
                    cmd.Parameters.AddWithValue("@AnnoNum", AnnoNum); 


                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                    Response.Redirect(Request.Url.AbsoluteUri);
                }
            }
        }




        private void BindData()
        {
            string query = "select [Annotation Number], [Business Unit] as Unit from vw_GridviewSource";
            SqlCommand cmd = new SqlCommand(query);
            gvSummary.DataSource = GetData(cmd);
            gvSummary.DataBind();
        }

        private DataTable GetData(SqlCommand cmd)
        {
            string strConnString = ConfigurationManager.ConnectionStrings["SRM_MetricConnectionString"].ConnectionString;
            using (SqlConnection con = new SqlConnection(strConnString))
            {
                using (SqlDataAdapter sda = new SqlDataAdapter())
                {
                    cmd.Connection = con;
                    sda.SelectCommand = cmd;
                    using (DataTable dt = new DataTable())
                    {
                        sda.Fill(dt);
                        return dt;
                    }
                }
            }
        }

    }
}

我需要做的是在第一列“注释编号”中获取值,并在我的更新的 where 子句中使用它。所以我需要更新 vw_GridviewSource set [Business Unit] = 'Accessories' where [Annotation Number] = '123456'

当我选择该行进行编辑时,附件将是下拉列表中的选择,并且 123456 将在文本框中。

4

1 回答 1

0

你的 gridview 不见了DataKeyNames。它可能看起来像:

<asp:GridView ID="gvSummary" 
    runat="server" 
    AutoGenerateColumns="False"
    DataKeyNames="AnnoNum, MyOtherKey1, MyOtherKey2"
    ... ... ...

DataKeyNames 属性是一个字符串数组。您可以在MSDN中找到更多信息

于 2013-08-29T17:53:27.710 回答