Adding DataGrid Cols programatically

Krizalidfx

New Member
I wonder whether you can help me with this problem which has been driving me potty.<BR><BR>(I'm using Visual Studio .NET Enterprise Architect for the code)<BR><BR>I need to bind a datagrid to a table which is determined at runtime and I also need to control which columns are displayed<BR>I therefore need to set up the columns I need as boundcolumns programatically.I have set an edit button column in the designer and have written command handlers for the edit, cancel and update processes.<BR><BR>I have the following code which sets up the data and columns and binds it to the datagrid:<BR><BR>Private Sub BindData(ByVal Tablename As String) 'Tablename is supplied by user<BR> <BR> Conn1.Open() 'Connection to SQL database<BR> Dim SQL As String<BR> Dim A, I As Integer<BR> Dim BC as New BoundColumn() <BR> <BR> SQL = "Select * from " & Tablename<BR> CMD1.CommandText = SQL<BR> DA1.Fill(DS1, "T1") 'DA is a DataAdaptor DS1 a Dataset<BR> Conn1.Close()<BR> DG1.DataSource = DS1.Tables("T1").DefaultView<BR> <BR> DG1.AutoGenerateColumns = False<BR> A = DS1.Tables("T1").Columns.Count 'Gets the number of columns in the table in the dataset<BR> For I = 0 To A - 1 'Sets up the bound columns (zero indexed) ;in this case all of them <BR> BC = New BoundColumn()<BR> BC.HeaderText = DS1.Tables("T1").Columns(I).ColumnName<BR> BC.DataField = DS1.Tables("T1").Columns(I).ColumnName<BR> DG1.Columns.Add(BC)<BR> Next<BR> DG1.DataBind()<BR> End Sub<BR><BR>This works fine. All the columns and data appear on the webform<BR><BR>The problem occurs when I edit the datagrid, having put it into edit mode (which happens without any problem)<BR> I enter data into the text boxes and then click the update button The update commandhandler (with some debug code) is shown below<BR><BR> Private Sub DG1_UpdateCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs ) Handles DG1.UpdateCommand<BR><BR> Dim Numcells as Integer <BR> NumCells = e.item.cells.count<BR> (other code to extract the data and update the table)<BR><BR>End Sub<BR><BR>NumCells always evaluates to 1 (Which is the button column I presume) no matter how many other columns are in the grid in edit mode and therefore I cannot access the cells that I have just entered data into.<BR><BR>If I Allow the grid to set up its own columns by setting AutoGenerateColumns to true and commenting out the bound columns code, NumCells evaluates to the right number of cells and I can extract the data and update the table without any problem, but all the columns appear and all can be edited; not what I want.<BR><BR>Similarly if I set the bound columns using tags in .aspx webform at design time, again I have no problems, the columns appear exactly as specified, and I can extract the data and update the table; not what I want.<BR><BR>What have I forgotten to do in the code above which prevents the columns from being seen during the update process?<BR><BR>Any help would be gratefully received<BR><BR>TIA<BR><BR>Chas
 
Back
Top