Cells[].Text or Cells[].Controls[0]

Discussion in 'ASP .Net Datagrid Control' started by Joel Finkel, Sep 1, 2003.

  1. Joel Finkel

    Joel Finkel Guest

    Folks,

    The following code illustrates two methods of obtaining the contents of a DataGrid Item. The function has been bound to the ItemCommand of the DataGridCommandEventHandler. It is invoked by clicking a button within the row.

    Note that because I am loathe to hard-code column numbers, as they may change, I at least make the code a bit easier to maintain by setting constants to represent the column numbers I need to use.

    In the first example, I get the Address by simply using the Text attribute of e.Item.Cells[].

    In the second example, which is the more common in the documentation, I use the two-step process of obtaining the object, e.Item.Cells[].Columns[0] as a TextBox, and then getting its Text attribute.

    QUESTION: Why is the second example more common in the documentation when the first method seems to work, is easier to code, and is faster?

    QUESTION: Am I missing something terribly important here?

    Thanks!

    /Joel Finkel



    private void DataGrid1_ItemCommand(object sender, DataGridCommandEventArgs e)

    {
    const int k_p_Address = 7;
    const int k_p_City = 8;
    const int k_p_State = 9;
    const int k_p_Zip = 10;
    const int k_p_Country = 11;

    try

    {
    myAddressCorrector.Address = e.Item.Cells[k_p_Address].Text;

    TextBox CityText = (TextBox)e.Item.Cells[k_p_City].Controls[0];
    myAddressCorrector.City = CityText.Text;

    TextBox StateText = (TextBox)e.Item.Cells[k_p_State].Controls[0];
    myAddressCorrector.State = StateText.Text;

    TextBox ZipText = (TextBox)e.Item.Cells[k_p_Zip].Controls[0];
    myAddressCorrector.Zip = ZipText.Text;

    TextBox CountryText = (TextBox)e.Item.Cells[k_p_Country].Controls[0];
    myAddressCorrector.Country = CountryText.Text;

    myAddressCorrector.CorrectAddress();

    }

    catch (Exception ex)

    {
    Label1.Text = ex.ToString();
    }
    Joel Finkel, Sep 1, 2003
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JDavies
    Replies:
    2
    Views:
    783
    JDavies
    Nov 11, 2003
  2. Carl Gilbert
    Replies:
    0
    Views:
    888
    Carl Gilbert
    Jul 20, 2005
  3. Replies:
    0
    Views:
    507
  4. Cells.Controls

    , Mar 12, 2007, in forum: ASP .Net
    Replies:
    0
    Views:
    326
  5. Nathan Sokalski
    Replies:
    4
    Views:
    1,126
    Muhammad Naveed Yaseen
    Sep 5, 2007
Loading...

Share This Page