Inserting individual items into a datagrid

W

W Akthar

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 ???
 
B

Bharat Biyani

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top