获取GridView中插入的最后一行的索引

时间:2012-01-20 02:51:28

标签: asp.net

考虑到用户可能在网格中有自定义排序(我不能使用最后一行),如何获取插入GridView中的最后一行的行索引。

    protected void ButtonAdd_Click(object sender, EventArgs e)
    {
        SqlDataSourceCompleteWidget.Insert();
        GridViewCompleteWidget.DataBind();
        GridViewCompleteWidget.EditIndex = ??????;
    }

我希望在插入发生后立即将行置于编辑模式。

更新

    protected void ButtonAdd_Click(object sender, EventArgs e)
    {
        //SqlDataSourceCompleteWidget.InsertParameters.Add("EFFECTIVE_DATE", Calendar1.SelectedDate.ToString("yyyy-MM-dd"));
        SqlDataSourceCompleteWidget.InsertParameters[0].DefaultValue = Calendar1.SelectedDate.ToString("yyyy-MM-dd");
        SqlDataSourceCompleteWidget.Insert();
        GridViewCompleteWidget.DataBind();
        GridViewCompleteWidget.EditIndex = 1;
    }

    private int mostRecentRowIndex = -1;
    protected void GridViewCompleteWidget_RowCreated(object sender, GridViewRowEventArgs e)
    {
        mostRecentRowIndex = e.Row.RowIndex;

        //GridViewCompleteWidget.EditIndex = e.Row.RowIndex;
    }

4 个答案:

答案 0 :(得分:0)

您可能希望处理RowCreated事件。您可以使用传递给此事件处理程序的GridViewRowEventArgs对象访问行数据和标识/位置。

  void YourGridView_RowCreated(Object sender, GridViewRowEventArgs e)
  {
      YourGridView.EditIndex = e.Row.RowIndex;
  }

答案 1 :(得分:0)

更新:我处理了this post

我假设您在执行插入后只返回一个值,但您可以在插入记录的任何位置设置ID。

private int mostRecentRowIndex = -1;  //edit index
    private bool GridRebound = false;  //used to make sure we don't rebind
    private string ID;  //Is set to the last inserted ID

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack)
        {
            //Set so grid isn't rebound on postback
            GridRebound = true;
        }
    }

    protected void ButtonAdd_Click(object sender, EventArgs e)
    {
        SqlDataSourceCompleteWidget.InsertParameters[0].DefaultValue = Calendar1.SelectedDate.ToString("yyyy-MM-dd");
        ID = SqlDataSourceCompleteWidget.Insert();
        GridViewCompleteWidget.DataBind();
    }


    protected void GridViewCompleteWidget_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //Check that the row index >= 0 so it is a valid row with a datakey and compare 
        //the datakey to ID value
        if (e.Row.RowIndex >= 0 && GridViewCompleteWidget.DataKeys[e.Row.RowIndex].Value.ToString() == ID)
        {
            //Set the edit index
            mostRecentRowIndex = e.Row.RowIndex;
        }
    }

    protected void GridViewCompleteWidget_DataBound(object sender, EventArgs e)
    {
        //Set Gridview edit index if isn't -1 and page is not a post back
        if (!GridRebound && mostRecentRowIndex >= 0)
        {
            //Setting GridRebound ensures this only happens once
            GridRebound = true;
            GridViewCompleteWidget.EditIndex = mostRecentRowIndex;
            GridViewCompleteWidget.DataBind();
        }
    }



选择是否在gridview中插入最后一条记录(无排序)

您应该能够从数据源获取行数:(减1,因为行从0开始,计数从1开始)

GridViewCompleteWidget.EditIndex = ((DataTable)GridViewCompleteWidget.DataSource).Rows.Count - 1;

但是在绑定数据之前把它放进去:

protected void ButtonAdd_Click(object sender, EventArgs e)
    {
        SqlDataSourceCompleteWidget.Insert();
        GridViewCompleteWidget.EditIndex = ((DataTable)GridViewCompleteWidget.DataSource).Rows.Count - 1;
        GridViewCompleteWidget.DataBind();
    }

答案 2 :(得分:0)

修改 对不起,我跳过订购是由用户完成的。我已经测试了以下代码,并且它确实尊重用户对项目的排序,但我们必须将用户带到最后一行存在的页面。

1st:在插入后从数据库中检索Max(ID),将其存储在会话中

 select Max(ID) from tbl_name; -- this statement can retrieve the last ID
Session["lastID"]=lastID;

第二名:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{       
    if(e.Row.RowType==DataControlRowType.DataRow)
    if (Session["lastID"] != null)
        if ((int)DataBinder.Eval(e.Row.DataItem, "ID") == (int)Session["lastID"])
        {
            //Session["rowIndex"] = e.Row.RowIndex;
            int rowIndex=e.Row.RowIndex;



            if (Session["type"] != null)
                if (Session["type"].ToString() == "Normal")
                {
                    int integ;
                    decimal fract;
                    integ = rowIndex / GridView1.PageSize;
                    fract = ((rowIndex / GridView1.PageSize) - integ;
                    if (fract > 0)
                        GridView1.PageIndex = integ;
                    else if (integ > 0) GridView1.PageIndex = integ - 1;

                    GridView1.EditIndex = rowIndex;
                }
        }
}

3rd:将commandField转换为TemplateFields并设置其CommandArgument =“Command”     我将使用此参数来标识触发RowDataBound事件的内容。我将值存储在Session [“type”]中。默认值为页面加载事件中定义的“正常”。

if(!IsPostBack)
  Session["type"]="Normal";

另一个值在RowCommand事件

中设置
 protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandArgument == "Command")
        Session["type"] = "Command";
}

它对我很好。
对不起我的语言,可能是不必要的细节。

答案 3 :(得分:0)

使用gridview的onitem命令进行编辑。然后,您可以使用网格列上的绑定表达式来设置您想要的任何内容。如果您使用按钮或链接按钮或其他几个按钮,则可以使用CommandArgument

CommandArgument='<%# Eval("DataPropertyIWant") %>'