2011-05-16 61 views
0

在我的項目中,我有沒有與數據庫連接的網格視圖。它具有數據表作爲其來源,並且行被動態添加到「AddNewRowBtn」的按鈕點擊事件上。每個這些行都包含一個「刪除」按鈕。如果用戶單擊任何行中的刪除按鈕,則該行必須被刪除。爲此,我需要點擊按鈕的行的行索引。如何獲取該行的行索引?獲取網格視圖中的行索引

我的.aspx.cs頁面的代碼如下。

using System; 
using System.Collections; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Data; 

public partial class AppForm : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
    if (!IsPostBack) 
    { 
     setInitialRow(); 
    } 
} 
protected void addRowBtn_Click(object sender, EventArgs e) 
{ 
    AddNewRow(); 
} 
public void setInitialRow() 
{ 
    DataTable Table = new DataTable(); 
    DataRow dr = null; 
    Table.Columns.Add(new DataColumn("Qualification", typeof(string))); 
    Table.Columns.Add(new DataColumn("QualiId", typeof(Int32))); 
    Table.Columns.Add(new DataColumn("Percentage", typeof(float))); 
    Table.Columns.Add(new DataColumn("PassingYear", typeof(Int32))); 
    Table.Columns.Add(new DataColumn("InstituteName", typeof(string))); 
    dr = Table.NewRow(); 

    dr["Percentage"] = DBNull.Value; 
    dr["PassingYear"] = DBNull.Value; 
    dr["InstituteName"] = string.Empty; 

    Table.Rows.Add(dr); 
    Session.Add("CurTable", Table); 
    QualificationGrid.DataSource = Table; 
    QualificationGrid.DataBind(); 


    //ArrayList Array = new ArrayList(); 
    //DataSet ds = new DataSet(); 
    DropDownList DDL = (DropDownList)QualificationGrid.Rows[0].Cells[0].FindControl("QualificationList"); 
    FillDropDownList(DDL); 
} 
public void AddNewRow() 
{ 
    if (Session["CurTable"] != null) 
    { 
     DataTable CurTable = (DataTable)Session["CurTable"]; 
     DataRow CurRow = null; 
     if (CurTable.Rows.Count > 0) 
     { 
      CurRow = CurTable.NewRow(); 
      CurTable.Rows.Add(CurRow); 
      Session.Add("CurTable", CurTable); 
      for (int count = 0; count < CurTable.Rows.Count - 1; count++) 
      { 
       DropDownList DDL = (DropDownList)QualificationGrid.Rows[count].Cells[0].FindControl("QualificationList"); 
       TextBox PercentageBox = (TextBox)QualificationGrid.Rows[count].Cells[1].FindControl("percentageBox"); 
       DropDownList YearList = (DropDownList)QualificationGrid.Rows[count].Cells[0].FindControl("yearList"); 
       TextBox InstituteNameBox = (TextBox)QualificationGrid.Rows[count].Cells[1].FindControl("InstituteNameBox"); 

       CurTable.Rows[count]["Percentage"] = PercentageBox.Text; 
       CurTable.Rows[count]["PassingYear"] = YearList.SelectedItem.Text; 
       CurTable.Rows[count]["InstituteName"] = InstituteNameBox.Text; 
       CurTable.Rows[count]["Qualification"] = DDL.SelectedItem.Text; 
       CurTable.Rows[count]["QualiId"] = DDL.SelectedValue; 
      } 
      QualificationGrid.DataSource = CurTable; 
      QualificationGrid.DataBind(); 
     } 
    } 
    setPreviousData(); 
} 
public void setPreviousData() 
{ 
    int RowIndex = 0; 
    if (Session["CurTable"] != null) 
    { 
     DataTable RestoreTable = (DataTable)Session["CurTable"]; 
     if (RestoreTable.Rows.Count > 0) 
     { 
      for (int row = 0; row < RestoreTable.Rows.Count; row++) 
      { 
       DropDownList DPList = (DropDownList)QualificationGrid.Rows[row].Cells[0].FindControl("QualificationList"); 
       TextBox PercentageBox = (TextBox)QualificationGrid.Rows[row].Cells[1].FindControl("percentageBox"); 
       // TextBox YearBox = (TextBox)QualificationGrid.Rows[row].Cells[2].FindControl("yearBox"); 
       DropDownList YearList = (DropDownList)QualificationGrid.Rows[row].Cells[0].FindControl("yearList"); 
       TextBox InstituteName = (TextBox)QualificationGrid.Rows[row].Cells[3].FindControl("InstituteNamebox"); 

       FillDropDownList(DPList); 

       if (row < RestoreTable.Rows.Count - 1) 
       { 
        PercentageBox.Text = RestoreTable.Rows[row]["Percentage"].ToString(); 
        InstituteName.Text = RestoreTable.Rows[row]["InstituteName"].ToString(); 

        DPList.ClearSelection(); 
        DPList.Items.FindByText(RestoreTable.Rows[row]["Qualification"].ToString()).Selected = true; 

        YearList.ClearSelection(); 
        YearList.Items.FindByText(RestoreTable.Rows[row]["PassingYear"].ToString()).Selected = true; 
       } 
       RowIndex++; 
      } 
     } 
    } 
} 
private ArrayList FillArrayList() 
{ 
    ArrayList ArrayList = new ArrayList(); 
    DataSet ds = new DataSet(); 
    using (DataOperation oDo = new DataOperation()) 
    { 
     DataTable dt = oDo.DropDownList("select * from tblQualificationMaster"); 
     for (int count = 0; count < dt.Rows.Count; count++) 
     { 
      ArrayList.Add(new ListItem(dt.Rows[count][1].ToString(), dt.Rows[count][0].ToString())); 
      //ArrayList.Add(new ListItem(ds.Tables[0].Rows[count][1].ToString(), ds.Tables[0].Rows[count][0].ToString())); 
     } 
    } 
    return ArrayList; 
} 
private void FillDropDownList(DropDownList DDL) 
{ 
    ArrayList ArrayList = FillArrayList(); 
    foreach (ListItem item in ArrayList) 
    { 
     DDL.Items.Add(item); 
    } 
    DDL.Items.Insert(0, "Select Year"); 
} 

protected void removeBtn_Click(object sender, EventArgs e) 
{ 
    int row = QualificationGrid.c 
} 

}

回答

2

除了在@艾莉森的回答中列出的可能性(使用SelectedRow絕對是最簡單的選擇,它是否適合你),你也可以去得到實際行本身的RowIndex。

在該按鈕點擊(其中sender是你ButtonLinkButton,或ImageButton),請使用以下(例如發件人類型ImageButton)事件處理程序:

(GridViewRow)(((ImageButton)sender).Parent.Parent) 

爲了得到行作爲一個GridViewRow ,然後使用GridViewRow.RowIndex屬性。

編輯:我不知道@艾莉森的鏈接中的第三個選項如何與此相比 - 這個使用sender.Parent.Parent來獲得實際的表格行,而那個使用NamingContainer。我想說如果你的GridView已經被手動修改(行被添加/從表本身刪除),你可能會遇到使用NamingContainer的問題。

1

你可以如下顯示命令參數屬性添加到您的刪除按鈕:

<asp:TemplateField> 
     <ItemTemplate> 
        <asp:Button ID="btnDelete" CommandArgument='<%# ((GridViewRow) Container).RowIndex %>' CommandName="Delete" /> 
     </ItemTemplate> </asp:TemplateField> 

,那麼你必須創建活動爲以下:

protected void DeleteRowBtn_Click(object sender,GridViewCommandEventArgs e) 
{ 
    int rowIndex = Convert.ToInt32(e.CommandArgument); 
} 

,並在你的GridView標記你需要綁定事件:

<asp:GridView ID="GridView1" runat="server" AutoGenerateEditButton="True" 
    DataKeyNames="Id" 
    onrowcommand="DeleteRowBtn_Click" > 
+0

如上例所述,使用GridView的DataKeys/DataKeyName屬性和d按照預期的方式進行。 http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.datakeys.aspx – mikey 2011-05-16 12:51:34