在网格视图中获取行索引

时间:2011-05-16 12:29:10

标签: c# asp.net

在我的项目中,我有一个与数据库无关的网格视图。它以数据表为源,在“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
}

}

3 个答案:

答案 0 :(得分:2)

除了@Alison的答案中列出的可能性(使用SelectedRow绝对是最简单的选项,如果它适用于你),你也可以获得实际行本身的RowIndex。

在按钮点击的事件处理程序中(sender是您的ButtonLinkButtonImageButton),请使用以下内容(示例发件人类型{{ 1}}):

ImageButton

要将行设为(GridViewRow)(((ImageButton)sender).Parent.Parent) ,然后使用GridViewRow属性。

编辑:我不确定@Alison的链接中的第三个选项与此相比如何 - 这个使用GridViewRow.RowIndex来获取实际的表行,而那个使用sender.Parent.Parent。我想说如果你的GridView已被手动修改(从表中添加/删除行),你可能会使用NamingContainer遇到问题。

答案 1 :(得分:1)

答案 2 :(得分: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" >