Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

A simple method to select a row in a GridView control in ASP.NET 2.0

1.50/5 (2 votes)
1 Aug 2007CPOL 1   191  
A simple way to select/deselect a row in a GridView control in ASP.NET 2.0.

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.

C#
DataSet ds = new DataSet(); //global dataset
int i = 0; //global variable 
protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        // for loading the values from xml to dataset
        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.

C#
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.

C#
protected void chkEdit_CheckedChanged(object sender, EventArgs e)
{
    CheckBox checkEdit;
    TextBox textHeading;
    Button buttonDelete;
    GridViewRowCollection gvRowColl = GridView1.Rows;

    foreach (GridViewRow dr in gvRowColl)
    {
        /*second time when a check box is checked, then the below code clears the last- 
          selected row to null (which is in the session object) 
          and disables the controls of that row*/
        if (Session["SelectedRow"] != null)
        {
            int row = int.Parse(Session["SelectedRow"].ToString());

            //for finding the controls in the grid and typecasting it to the type of the -
            //the control in the itemtemplate 
            checkEdit = (CheckBox)gvRowColl[row].Cells[0].FindControl("chkEdit");
            textHeading = (TextBox)gvRowColl[row].Cells[1].FindControl("txtHeading");
            buttonDelete = (Button)gvRowColl[row].Cells[2].FindControl("btnDelete");

                //disabling the controls and unchecking the checkbox
                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)
            {
                //this session object stores the current 
                //selected row( current checked checkbox)
                Session["SelectedRow"] = dr.DataItemIndex;

                //for finding the controls in the grid and typecasting it to the type of the -
                //the control in the itemtemplate  
                textHeading = (TextBox)dr.Cells[1].FindControl("txtHeading");
                buttonDelete = (Button)dr.Cells[2].FindControl("btnDelete");

                //enabling the controls in the current checked row
                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.

C#
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)//if not header then
    {
        textHeading.Text = ds.Tables[0].Rows[i][0].ToString();
        
        //this condition doesnot stisfy on the very first
        //page load ie during page postback to itself.
        //this condition satisfies only during subsequent 
        //postbacks ie on clicking the button.
        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

  • Version 1.0.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)