2012-01-20 44 views
1

考慮到用戶可能在網格中進行自定義排序(我無法使用最後一行),如何獲取GridView中插入的最後一行的行索引。獲取GridView中插入的最後一行的索引

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

我想插入後立即進入編輯模式行。

UPDATE

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; 
    } 
+0

您可以檢查我的更新答案,如果它符合 – Mubarek

回答

0

你將要處理的事件RowCreated。您可以使用傳遞給此事件處理程序的GridViewRowEventArgs對象訪問行數據和標識/位置。

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

的問題是,e.Row.RowIndex始終是-1 – jax

+0

@jax你能發佈你的代碼? – 2012-01-20 03:44:16

+0

好 - 問題更新 – jax

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; 

但是把這個bef礦石您綁定數據:

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

這將得到最後一行(如果用戶已經按降序排序他們的控制或什麼? – jax

+0

@jax - 是數據源中每行的唯一標識符是什麼? –

+0

是的,有一個ID – jax

0

編輯 對不起,我跳過的順序是由用戶來完成。我已經測試了下面的代碼,它在尊重用戶訂購項目的情況下工作,但我們必須將用戶帶到最後一行所在的頁面。

1:從數據庫中檢索最大(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; 
       } 
     } 
} 

3:將您CommandField中成TemplateFields並設置它們的CommandArgument =「Command」 我將使用這個參數來確定觸發RowDa taBound事件。我將該值存儲在Session [「type」]中。默認值是在頁面加載事件中定義的「正常」。

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

其他值在RowCommand事件

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

它爲我設定罰款。
對不起,我的語言,可能是不必要的細節。

0

使用gridview的onitemcommand進行編輯。然後,您可以在網格列上使用綁定表達式來設置您想要的任何內容。如果你正在使用一個按鈕或LinkBut​​ton的或幾個人,你可以使用CommandArgument

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