This is a migrated thread and some comments may be shown as answers.

Multiple controls with the same ID when programmatically creating items

1 Answer 57 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Brad
Top achievements
Rank 1
Brad asked on 15 Jul 2010, 02:18 AM

Hi all,
I am trying to add columns to my grid programmatically. It works OK with 1 column, but more than 1 throw an error about multiple controls with the same ID. I'm using the below code:

 

// Add columns programmatically
  
  
  
GridBoundColumn boundColumn;
  
int ii = 0;
  
int ij = 0;
  
foreach (DataRow row in dt.Rows)
  
{
  
// We only want to generate columns off the first row
  
  
  
if (ii>0) continue;
  
// Create the new column collection in our loop
  
  
  
boundColumn = new GridBoundColumn();
  
foreach (DataColumn col in dt.Columns)
  
{
  
// Increment to pass the first columns
  
  
  
ij++;
  
// We don't want the first column because it's our identity (ID) column
  
  
  
if (ij == 1) continue;
  
boundColumn.DataField = col.ToString();
  
boundColumn.HeaderText = col.ToString();
  
// Add the column to our grid
  
  
  
gvLUs.MasterTableView.Columns.Add(boundColumn);
  
}
  
ii++;
  
}
  
// Bind the data to the grid
  
  
  
gvLUs.DataBind();

 

 



I've checjed and the text from col.ToString() is different. The error is balking about an ID from the second column being added. The error is as the page is rendered.

Any ideas?

Thanks!

1 Answer, 1 is accepted

Sort by
0
Brad
Top achievements
Rank 1
answered on 15 Jul 2010, 02:30 AM
Figured it out, never code tired!
Tags
Grid
Asked by
Brad
Top achievements
Rank 1
Answers by
Brad
Top achievements
Rank 1
Share this question
or