在我的項目中,我有沒有與數據庫連接的網格視圖。它具有數據表作爲其來源,並且行被動態添加到「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
}
}
如上例所述,使用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