gridview RowUpdating索引

时间:2012-08-23 16:59:51

标签: c# asp.net gridview

将项目添加到gridview时,我希望用户能够在将文件(从gridview条目)写入csv格式之前选择和编辑/删除项目。当我点击“编辑”并更新信息时,我收到一个错误。位置0处没有行(或者您选择编辑的任何行)。这是我的代码。

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        SetFocus(parttxtbox);
        table = new DataTable();
        MakeDataTable();
    }
    else
        table = (DataTable)ViewState["table"];

    ViewState["table"] = table;
}

protected void MakeDataTable()
{
    table.Columns.Add("Part", typeof(string));
    table.Columns.Add("Quantity", typeof(Int32));
    table.Columns.Add("Ship-To", typeof(string));
    table.Columns.Add("Requested Date", typeof(string));
    table.Columns.Add("Shipping Method", typeof(string));

    //Persist the table in the Session object.
    Session["table"] = table;

    //Bind data to the GridView control.
    BindData();
}

protected void addbtn_Click(object sender, EventArgs e)
{
    part = parttxtbox.Text.ToUpper();
    shipto = shiptotxtbox.Text;
    reqdate = reqdatecal.SelectedDate.ToShortDateString();
    shipmthd = shipddl.SelectedItem.ToString();
    CreateTable();
 }

public void CreateTable()
{
    DataRow row = table.NewRow();
    row["Part"] = part;
    row["Quantity"] = qty;
    row["Ship-To"] = shipto;
    row["Requested Date"] = reqdate;
    row["Shipping Method"] = shipmthd;
    table.Rows.Add(row);

    griditems.DataSource = table.DefaultView;
    griditems.DataBind();
}

private void BindData()
{
    griditems.DataSource = table;
    griditems.DataBind();
}

protected void griditems_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    //Retrieve the table from the session object.
    DataTable dt = (DataTable)Session["table"];

    //Update the values.           
    GridViewRow row = griditems.Rows[e.RowIndex];
    dt.Rows[row.DataItemIndex]["Part"] = ((TextBox)(row.Cells[1].Controls[0])).Text;
    dt.Rows[row.DataItemIndex]["Quantity"] = ((TextBox)(row.Cells[2].Controls[0])).Text;
    dt.Rows[row.DataItemIndex]["Ship-To"] = ((TextBox)(row.Cells[3].Controls[0])).Text;
    dt.Rows[row.DataItemIndex]["Requested Date"] = ((TextBox)(row.Cells[4].Controls[0])).Text;
    dt.Rows[row.DataItemIndex]["Shipping Method"] = ((DropDownList)(row.Cells[5].Controls[0])).SelectedItem.ToString();


    //Reset the edit index.
    griditems.EditIndex = -1;
    //Bind data to the GridView control.
    BindData();

    ////Somewhat works, doesn't update the part field with the text entered
    //TextBox partedit = (TextBox)griditems.Rows[e.RowIndex].FindControl("Part");
    //DataRow row = table.NewRow();
    //row["Part"] = partedit;
    //row["Quantity"] = qty;
    //row["Ship-To"] = shipto;
    //row["Requested Date"] = reqdate;
    //row["Shipping Method"] = shipmthd;
    //table.Rows.Add(row);
}

注释掉的部分“Somewhat works”会在您点击更新时更新的字段中写入空白。

1 个答案:

答案 0 :(得分:1)

您在会话there is no row in it中添加数据表的时间,这就是您收到错误的原因"There is no row at position 0."

您正在将表从视图状态分配给表,它将是会话。

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        SetFocus(parttxtbox);
        table = new DataTable();
        MakeDataTable();
    }
    else
    if(Session["table"] != null)
        table = (DataTable)ViewState["table"];
}

protected void MakeDataTable()
{
    table.Columns.Add("Part", typeof(string));
    table.Columns.Add("Quantity", typeof(Int32));
    table.Columns.Add("Ship-To", typeof(string));
    table.Columns.Add("Requested Date", typeof(string));
    table.Columns.Add("Shipping Method", typeof(string));

    //Persist the table in the Session object.
    Session["table"] = table; //This adds table without any record in it. 

    //Bind data to the GridView control.
    BindData();
}

在数据表中添加记录后,将数据表添加到会话中。

protected void addbtn_Click(object sender, EventArgs e)
{
    part = parttxtbox.Text.ToUpper();
    shipto = shiptotxtbox.Text;
    reqdate = reqdatecal.SelectedDate.ToShortDateString();
    shipmthd = shipddl.SelectedItem.ToString();
    CreateTable();
    Session["table"] = table;
 }

更新后在griditems_RowUpdating中更新Session [“table”]。

protected void griditems_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    //Retrieve the table from the session object.
    DataTable dt = (DataTable)Session["table"];

    //Update the values.           
    GridViewRow row = griditems.Rows[e.RowIndex];
    dt.Rows[row.DataItemIndex]["Part"] = ((TextBox)(row.Cells[1].Controls[0])).Text;
    dt.Rows[row.DataItemIndex]["Quantity"] = ((TextBox)(row.Cells[2].Controls[0])).Text;
    dt.Rows[row.DataItemIndex]["Ship-To"] = ((TextBox)(row.Cells[3].Controls[0])).Text;
    dt.Rows[row.DataItemIndex]["Requested Date"] = ((TextBox)(row.Cells[4].Controls[0])).Text;
    dt.Rows[row.DataItemIndex]["Shipping Method"] = ((DropDownList)(row.Cells[5].Controls[0])).SelectedItem.ToString();


    //Reset the edit index.
    griditems.EditIndex = -1;
    //Bind data to the GridView control.
    BindData();

    Session["table"] = dt; 
}

注意:您尝试获取更新记录的方式不合适,您应该阅读有关如何访问行更新数据的更多信息。