簡體   English   中英

在網格視圖中獲取行索引

[英]Get the row index in grid view

在我的項目中,我具有未與數據庫連接的網格視圖。 它以數據表為源,並在“ 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
}

}

除了@Alison的答案中列出的可能性(如果適合您,使用SelectedRow絕對是最簡單的選項),您還可以獲取實際行本身的RowIndex。

在按鈕單擊的事件處理程序中( sender是您的ButtonLinkButtonImageButton ),請使用以下內容(示例發送方類型ImageButton ):

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

要獲得作為GridViewRow的行,然后使用GridViewRow.RowIndex屬性。

編輯:與該選項相比,我不確定sender.Parent.Parent鏈接中的第三個選項如何工作-該選項使用sender.Parent.Parent獲取實際的表行,而該選項使用NamingContainer 我想說的是,如果您的GridView完全被手動更改(行被添加到表中或從表中移除),那么使用NamingContainer可能會遇到問題。

您可以將命令參數屬性添加到刪除按鈕,如下所示:

<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" >

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM