Dynamically Create Bound Fields Server-Side

Topics: Developer Forum, New Control Requests, Project Management Forum, User Forum
Sep 6, 2007 at 11:05 PM
First let me say that this control is exactly what I am looking for. Unfortunately I'm a VB guy.

First is there a VB version of this code?

Second, we use a lot of datagrids and dynamically assign columns on the server side depending on what "GridID" is passed to the page. For instance in the Grid Init event I have:

Protected Sub ccGridView_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles ccGridView.Init

Dim field As WebControls.BoundField
If ccGridView.Columns.Count = 0 Then
'* ** Set Column Headers and Widths
field = New WebControls.BoundField
field.DataField = "PartNum"
field.SortExpression = "PartNum"
field.HeaderText = "Part #"
ccGridView.Columns.Add(field)

'... Next Column, etc etc etc

My question is can I modify the source code to let me dynamically add columns from the server side? I keep on getting a null referenced exception when I try to load my page? I'm bombing on "foreach (DataControlFieldHeaderCell th in this.HeaderRow.Cells)".

I'm also using an ObjectDataSource instead of a SqlDataSource, does that make a difference?

Thanks,
-- Joe --
Sep 7, 2007 at 12:22 AM
I am using a ObjectDataSource with the Frozengrid. Works just fine.

I am not really sure I understand your question about adding columns. I added a command column just fine and see no reason why this wouldn't work for other columns.


I know this is in C# but VB.Net version would not be much different.

// new column
CommandField cfiNewSelectField = new CommandField();
cfiNewSelectField.SelectText = "Select";
cfiNewSelectField.ShowSelectButton = true;
FrozenGridView1.Columns.Add(cfiNewSelectField);

// setting a Object DataSource
FrozenGridView1.DataSource = ObjectDataSource1.Select();
FrozenGridView1.DataBind();


Jeff
Sep 7, 2007 at 1:35 PM
Cool thanks for the help. It could be that I am just using the control wrong. This grid is informational only, the grid is a worklist, users double click the row to open the account and then work the account. I don't really need a Select button, I dynamically add javascript to open a new window on a double click. I add this on the Row_Created event, like this:

If e.Row.RowType = DataControlRowType.DataRow Then
DblClickEvent = "fittabs.aspx?DataSource=" & DataSource & "&Name=" & AcctName & "&ID" & AcctId, "'", "`")
e.Row.Attributes.Add("ondblclick", "do_client('" & DblClickEvent & "','" & RowId & "');")
End If

So I don't have any Command fields, only BoundFields, which I add dynamically from the server. I tried a simple example where I defined all of the columns in the client side code and everything worked fine. When I tried to incorporate adding all of my bound columns on the Server side, I ran into this:

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 224: {
Line 225: if (this.FreezeHeader)
Line 226: {
Line 227: foreach (DataControlFieldHeaderCell th in this.HeaderRow.Cells) *** This line highlighted in RED
Line 228: {

I compiled the source code, added the DLL to the GAC with the gacutil, and dropped the DLL in the Bin folder. That's to proper way to start using the control right?

Again thanks for your reply I really really appreciate it.

Thanks,
-- Joe --

Sep 7, 2007 at 1:54 PM
Edited Sep 7, 2007 at 1:54 PM
Right! JefferyDuty your last post lead me in the right direction. I was setting the DataSourceId property of the grid to the id of my ObjectDataSource. This is the way I do it for all of the Grid View controls that I use. As soon as I deleted the DataSourceId from the client side code and moved setting the DataSource and Binding the grid on the Server-Side everything started to work.

Thanks a lot you really helped me!!

Thanks,
-- Joe --
Sep 7, 2007 at 4:30 PM
Glad I could help, I really like the idea of the clicking on the row. My project is basically the same as yours. the grid is just for information and the selected row is used trigger other processses. I might look into adding that functionality in to the control as well as the scroll position.


Jeff
Sep 7, 2007 at 5:26 PM
Hey if you see this, how did you implement sorting? I'm trying to use the SortParameterName property with no luck