Inserting individual items into a datagrid

Discussion in 'ASP .Net Datagrid Control' started by W Akthar, Aug 10, 2004.

  1. W Akthar

    W Akthar Guest

    Hi,

    Is there a way to insert items into a datagrid
    individually instead of using datasource and databind??

    For example:

    I have a collection of people,
    I want to be able to iterate through this collection and
    insert particular items into a datagrid.

    namesCollection = MyService.GetAllNames();
    int i=0;
    for (i=0;i<namesCollection.Count;i++)
    {
    Names item = namesCollection;
    dgNames.Items.Cells[0].Text = item.ID.ToString
    ().TrimEnd();
    dgNames.Items.Cells[1].Text =
    item.Salutation.ToString().TrimEnd();
    dgNames.Items.Cells[2].Text = item.Initial.
    ToString().TrimEnd();
    dgNames.Items.Cells[3].Text = item.ForeName.
    ToString().TrimEnd();
    dgNames.Items.Cells[4].Text = item.SurName.
    ToString().TrimEnd();
    }

    This falls over because dgNames is out of range!!
    Any ideas ???
     
    W Akthar, Aug 10, 2004
    #1
    1. Advertising

  2. Hi,

    I dont think u can individually assign values to cells in the grid. However,
    what u can do is to set the Autogeneratecolumns property of the datagrid to
    false and add itemtemplates to the grid and databind them to the grid. You
    can call functions in these item templates which can format the data
    according to your requirements.

    If u only want to trim data then u can write the code in the Itemdatabound
    event of the datagrid.

    ---
    Bharat Biyani ()
    http://www.orcim.com


    "W Akthar" wrote:

    > Hi,
    >
    > Is there a way to insert items into a datagrid
    > individually instead of using datasource and databind??
    >
    > For example:
    >
    > I have a collection of people,
    > I want to be able to iterate through this collection and
    > insert particular items into a datagrid.
    >
    > namesCollection = MyService.GetAllNames();
    > int i=0;
    > for (i=0;i<namesCollection.Count;i++)
    > {
    > Names item = namesCollection;
    > dgNames.Items.Cells[0].Text = item.ID.ToString
    > ().TrimEnd();
    > dgNames.Items.Cells[1].Text =
    > item.Salutation.ToString().TrimEnd();
    > dgNames.Items.Cells[2].Text = item.Initial.
    > ToString().TrimEnd();
    > dgNames.Items.Cells[3].Text = item.ForeName.
    > ToString().TrimEnd();
    > dgNames.Items.Cells[4].Text = item.SurName.
    > ToString().TrimEnd();
    > }
    >
    > This falls over because dgNames is out of range!!
    > Any ideas ???
    >
     
    Bharat Biyani, Aug 11, 2004
    #2
    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. r3bol
    Replies:
    5
    Views:
    256
    Terry Reedy
    Nov 9, 2008
  2. Henrik
    Replies:
    1
    Views:
    433
    Ken Cox [Microsoft MVP]
    Jul 6, 2006
  3. Dunno
    Replies:
    0
    Views:
    206
    Dunno
    May 16, 2005
  4. Joe Pizzanley
    Replies:
    3
    Views:
    122
    Colin Bartlett
    Sep 2, 2010
  5. ela
    Replies:
    12
    Views:
    355
    Uri Guttman
    Apr 6, 2009
Loading...

Share This Page