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

GridDropDownColumn is blank

5 Answers 297 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jeff
Top achievements
Rank 1
Jeff asked on 27 Feb 2009, 09:48 PM
I'm trying to develop an grid that is entirely dynamic - the columns are defined in the code-behind, as a result of a database query.

In insert/edit, some of the columns need to be displayed as dropdown lists - and for some of them the contents of the list are the result of a database query, and for others the content is a fixed set of strings.

Right now, I'm working with the latter, thinking it'd be simpler.  Populating the dropdowns from the database I'll worry about after I get the fixed dropdowns working.

You have a note one one of your pages to the effect that the GridDropDownColumn isn't really intended for use with custom object collections, and that I should consider using a template column.  My understanding is that template columns, when added in code-behind, need to be added in Page_Init() - which would seem to rule them out, as I won't know what columns need to be added that early in the page life-cycle.  So GridDropDownColumn is what I have to work with.

In Page_Load, when !IsPostBack, I use the following code to create the column:

GridDropDownColumn col = new GridDropDownColumn(); 
this.grdRecords.MasterTableView.Columns.Add(col); 
col.DataField = "foobar"
col.UniqueName = "foobar";


And then in OnItemDataBound, I populate the dropdown with:
if (e.Item is GridEditableItem && (e.Item as GridEditableItem).IsInEditMode) 
   GridEditableItem editedItem = e.Item as GridEditableItem; 
   GridEditManager editMan = editedItem.EditManager; 
 
   GridDropDownListColumnEditor editor =  
      editMan.GetColumnEditor("foobar"as GridDropDownListColumnEditor; 
 
   editor.DataSource = new object[] { "FOO""BAR" }; 
   editor.DataBind(); 
}

When I display the grid, the foobar column is blank, though the "FOO" and "BAR" choices show up in the dropdown during edit.

I know the "foobar" DataField is correct, because when I use a GridBoundColumn instead of a GridDropDownColumn, the proper value displays in the grid.  (Though, of course, in the edit form I get a textbox, instead of a dropdown.)

I'm clearly missing something.  What?

5 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 02 Mar 2009, 07:14 AM
Hello Jeff,

A work around for your scenario is to access the Literal control of the DropDownColumn when the grid is in normal mode and bind it to the required field. Check out the code below:
cs:
 protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
    { 
        if (e.Item is GridDataItem && !e.Item.IsInEditMode) 
          { 
              GridDataItem item = (GridDataItem)e.Item; 
              Literal litrlContrl = (Literal)item["foobar"].Controls[0]; 
              litrlContrl.Text = (string)DataBinder.Eval(item.DataItem, "foobar"); 
          }  
    } 

Thanks
Princy.
0
Jeff
Top achievements
Rank 1
answered on 02 Mar 2009, 05:10 PM
In my testing scenario, the grid has two rows, but grdRecords_ItemDataBound is being called four times, where e.Item is GridEditableItem and e.Item.IsIneditMode is false.

Two of calls work fine, the other two throw an exception on item["foobar"].

System.NullReferenceException: Object reference not set to an instance of an object
   at System.Web.UI.Control.FindControl(String id, Int32 pathOffset) 
   at System.Web.UI.Page.FindControl(String id) 
   at Telerik.Web.UI.GridEditFormItem.get_Item(String columnUniqueName) 
   at KorWeb.Maintenance.ReportTemplate.grdRecords_ItemDataBound(Object sender, GridItemEventArgs e) in E:\Dev\korweb_ajax\Maintenance\ReportTemplate.aspx.cs:line 240 

If I catch this exception and throw it away, the proper value displays in the grid.  I'd prefer to test on some condition of the event or the event item, to throwing away the exception.  Clearly the extra two calls to grdRecords_ItemDataBound are on items that we should not be processing. But I've been unable to find a test that would distinguish them.

if (e.Item is GridEditableItem) 
   GridEditableItem item = e.Item as GridEditableItem; 
 
   if (e.Item.IsInEditMode) 
   { 
      // ... populate dropdown control 
   } 
   else 
   { 
      try 
      { 
         TableCell cell = item["outputtype"]; 
         Literal literalControl = (Literal)cell.Controls[0]; 
         literalControl.Text = (string)DataBinder.Eval(item.DataItem, "outputtype"); 
      } 
      catch (Exception) 
      { 
      } 
   } 
 

0
Coty
Top achievements
Rank 1
answered on 02 Jun 2009, 09:28 PM
Hey Princy, nice work around code....it works great.

I had the same problem as the OP until I found your solution.  However I would like to ask a question.
Shouldn't the default behavior of a GridDropDownColumn be to put the text of the selected Item in the literal control upon going into normal mode (during insert/update)?  That would be the most logical behavior in my opinion.  Is the work around the way it was intended to function or is there some way to make it auto populate?

I normally use Template columns for when I use dropdownlists just because I can update the datasource and reapply the datasource to the grid, but I thought I would give the Drop Down column a try.  Which works great except for this functionality.

Thanks for your solution again.

Coty
0
Princy
Top achievements
Rank 2
answered on 03 Jun 2009, 05:24 AM
Hello Coty,

The built-in GridDropDownColumn is designed to be used mainly with DataTables in order to easily map and generate their dropdown item values in conjunction with the DataField/ListValueField/DataSourceID/ListDataMember values of the column. If you want to set the DataSource property for the dropdownlist then you may consider replacing the GridDropDownColumn with a GridTemplateColumn with a DropDownList in its EditItemTemplate. Refer to the following document which explains more on customizing GridDropDownColumns:
Customize/Configure GridDropDownColumn

Thanks
Princy.
0
Richard
Top achievements
Rank 2
answered on 14 Jul 2011, 04:53 PM
Princy,

You have no idea what a life saver that piece of code was.  Thanks!
Tags
Grid
Asked by
Jeff
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Jeff
Top achievements
Rank 1
Coty
Top achievements
Rank 1
Richard
Top achievements
Rank 2
Share this question
or