How to cast a row so it can be called by name rather than index?

Last post 12-03-2009 10:26 PM by chetan.sarode. 1 replies.

Sort Posts:

  • How to cast a row so it can be called by name rather than index?

    11-28-2009, 5:28 AM
    • Member
      1 point Member
    • Qweeg
    • Member since 02-28-2009, 11:05 AM
    • Posts 4

    I have added totals to the footer of the ProjectList gridview by using the RowDataBound event in this kind of way:

       protected void ProjectListGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
          if (e.Row.RowType == DataControlRowType.DataRow)
          {
            _MonTotalHours += Convert.ToDecimal(e.Row.Cells[3].Text);
            
          }
          else if (e.Row.RowType == DataControlRowType.Footer)
          {
            e.Row.Cells[3].Text = _MonTotalHours.ToString();
          }
        }


     

    This works, but it seems like referencing the cells by index is a bit of a pain - if I add columns I have to remember to change the index.  I found a similar example on MSDN where the the row is cast into something more useful like this:

            // Reference the ProductsRow via the e.Row.DataItem property
            Northwind.ProductsRow product =
              (Northwind.ProductsRow)
              ((System.Data.DataRowView)e.Row.DataItem).Row;
    


     

    But I don't know what to cast to to replicate this in the starter kit.  Any tips would be much appreciated.

    Thanks,

    Tim

  • Re: How to cast a row so it can be called by name rather than index?

    12-03-2009, 10:26 PM

    You need to use gridView.FindControl("controlName"); to get the control in the row.

    You can also get at the DataItem property of the Row in question and cast it to the appropriate type and extract the information directly

    http://forums.asp.net/p/1246157/2292904.aspx#2292904

    http://geekswithblogs.net/michelotti/archive/2006/03/30/73896.aspx

     

    Chetan Sarode
    Software Engineer,
    Approva Systems Pvt Ltd,
    Pune, India.
Page 1 of 1 (2 items)