In my project, I have grid view which is not connected with database. It has data table as its source and rows are dynamically added on button click event of “AddNewRowBtn”. Each of these rows contain a button “Remove”. If user clicks on remove button in any of the row, then that row has to be deleted. For that I need row index of the row of which the button is clicked. How to get the row index of that row?
The code for my .aspx.cs page is as below.
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
}
}
Besides the possibilities listed in @Alison’s answer (using SelectedRow is definitely the simplest option, if it works for you), you can also go get the RowIndex of the actual row itself.
In the event handler for the button click (where the
senderis yourButton,LinkButton, orImageButton), use the following (example sender typeImageButton):To get the row as a
GridViewRow, and then use theGridViewRow.RowIndexproperty.EDIT: I’m not sure how the third option in @Alison’s link works compared to this one – this one uses
sender.Parent.Parentto get the actual table row, whereas that one uses theNamingContainer. I’d say if your GridView has been manually altered at all (rows being added/removed from the table itself) you could run into issues using theNamingContainer.