Asp.net 获取在GridView中插入的最后一行的索引
考虑到用户在网格中可能有自定义排序(我不能使用最后一行),如何获取插入到GridView中的最后一行的行索引 我想在插入发生后立即将行置于编辑模式 更新Asp.net 获取在GridView中插入的最后一行的索引,asp.net,Asp.net,考虑到用户在网格中可能有自定义排序(我不能使用最后一行),如何获取插入到GridView中的最后一行的行索引 我想在插入发生后立即将行置于编辑模式 更新 protected void ButtonAdd_Click(object sender, EventArgs e) { //SqlDataSourceCompleteWidget.InsertParameters.Add("EFFECTIVE_DATE", Calendar1.SelectedDate.ToSt
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;
}
您可能希望处理该事件。您可以使用传递给此事件处理程序的对象访问行数据和标识/位置
void YourGridView_RowCreated(Object sender, GridViewRowEventArgs e)
{
YourGridView.EditIndex = e.Row.RowIndex;
}
更新:我从 我假设您在执行插入后只返回一个值,但是您可以在插入记录的任何位置设置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开始) 但在绑定数据之前,请先执行以下操作:
protected void ButtonAdd_Click(object sender, EventArgs e)
{
SqlDataSourceCompleteWidget.Insert();
GridViewCompleteWidget.EditIndex = ((DataTable)GridViewCompleteWidget.DataSource).Rows.Count - 1;
GridViewCompleteWidget.DataBind();
}
编辑
很抱歉,我跳过了订购是由用户完成的。我已经测试了下面的代码,它在尊重用户对项目的排序的情况下工作,但是我们必须将用户带到最后一行所在的页面
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事件。我将值存储在会话[“type”]中。默认值为页面加载事件中定义的“正常”
if(!IsPostBack)
Session["type"]="Normal";
另一个值在RowCommand事件中设置
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandArgument == "Command")
Session["type"] = "Command";
}
这对我很管用。
对不起,我的语言可能是不必要的细节。使用gridview的onitemcommand进行编辑。然后可以在网格列上使用绑定表达式来设置所需的内容。如果您使用的是按钮、链接按钮或其他几个按钮,则可以使用CommandArgument
CommandArgument='<%# Eval("DataPropertyIWant") %>'
CommandArgument=''
问题是e.Row.RowIndex总是-1添加,这似乎也会使程序崩溃(就像是一个无休止的循环),这将得到最后一行(如果用户按降序或其他方式对控件排序会怎么样?@jax-数据源中的每一行都有唯一的标识符吗?您可以检查我更新的答案是否合适
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandArgument == "Command")
Session["type"] = "Command";
}
CommandArgument='<%# Eval("DataPropertyIWant") %>'