Introduction
This is a simple article on how to select a particular row in a GridView
control in ASP.NET 2.0 using XML as the data source.
Using the code
What does this code do? It simply loads the XML to the GridView
control and then when a user checks a particular checkbox, the textbox and button are enabled. The XML file NewsData.xml is in the downloadable version.
In the Page_Load
event of SelectingRow.aspx.cs, add the following code for loading the GridView
from the NewsData.xml file.
DataSet ds = new DataSet();
int i = 0;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
ds.ReadXml(Server.MapPath("~\\NewsData.xml"));
GridView1.DataSource = createDataSource(ds);
GridView1.DataBind();
}
}
createDataSource
creates the data source structure for the GridView
and returns the structure as a collection object.
protected ICollection createDataSource(DataSet dsNew)
{
DataTable dt = new DataTable();
DataView dv;
DataColumnCollection dc = dt.Columns;
foreach (DataColumn dataColumn in dsNew.Tables[0].Columns)
{
dc.Add(dataColumn.ColumnName, typeof(System.String));
}
for (int i = 0; i <= dsNew.Tables[0].Rows.Count - 1; i++)
{
DataRow dr = dt.NewRow();
foreach (DataColumn dColumn in dsNew.Tables[0].Columns)
{
dr[dColumn.ColumnName] = "";
}
dt.Rows.Add(dr);
}
dv = new DataView(dt);
return dv;
}
chkEdit_CheckedChanged
is for getting the current row and enabling/disabling the controls of that row.
protected void chkEdit_CheckedChanged(object sender, EventArgs e)
{
CheckBox checkEdit;
TextBox textHeading;
Button buttonDelete;
GridViewRowCollection gvRowColl = GridView1.Rows;
foreach (GridViewRow dr in gvRowColl)
{
if (Session["SelectedRow"] != null)
{
int row = int.Parse(Session["SelectedRow"].ToString());
checkEdit = (CheckBox)gvRowColl[row].Cells[0].FindControl("chkEdit");
textHeading = (TextBox)gvRowColl[row].Cells[1].FindControl("txtHeading");
buttonDelete = (Button)gvRowColl[row].Cells[2].FindControl("btnDelete");
textHeading.Enabled = false;
textHeading.ReadOnly = true;
buttonDelete.Enabled = false;
checkEdit.Checked = false;
Session["SelectedRow"] = null;
}
checkEdit = (CheckBox)dr.Cells[0].FindControl("chkEdit");
if (checkEdit.Checked)
{
Session["SelectedRow"] = dr.DataItemIndex;
textHeading = (TextBox)dr.Cells[1].FindControl("txtHeading");
buttonDelete = (Button)dr.Cells[2].FindControl("btnDelete");
textHeading.Enabled = true;
textHeading.ReadOnly = false;
buttonDelete.Enabled = true;
break;
}
}
}
The GridView1_RowDataBound
method is invoked by GridView.databind()
. This is where the binding of the child controls to the parent control happens.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
CheckBox checkEdit;
TextBox textHeading;
Button buttonDelete;
checkEdit = (CheckBox)e.Row.FindControl("chkEdit");
textHeading = (TextBox)e.Row.FindControl("txtHeading");
buttonDelete = (Button)e.Row.FindControl("btnDelete");
if (e.Row.RowIndex != -1)
{
textHeading.Text = ds.Tables[0].Rows[i][0].ToString();
if (Session["SelectedRow"] != null)
{
if (e.Row.RowIndex ==
int.Parse(Session["SelectedRow"].ToString()))
{
checkEdit.Checked = true;
textHeading.Enabled = true;
textHeading.ReadOnly = false;
buttonDelete.Enabled = true;
}
}
i += 1;
}
}
Simply download the working version (SelectRow.aspx and NewsData.xml) of this simple implementation and add it to a new web project!
Recommended articles on doing this in a different way
History