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

Setting GridDropDownColumn DataSource server side

4 Answers 418 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Michael
Top achievements
Rank 1
Michael asked on 25 Feb 2010, 03:43 AM

After reviewing the documentation and help files I'm still doing something wrong.  I can actually get the dropdown to display correctly on and insert and edit modes. Everything is working there fine and saving correctly.  I'm having troubles with the value displaying on load. Here's what I've got.

<telerik:GridDropDownColumn UniqueName="WatchdogFrequencyddl" ListTextField="Name" ListValueField="Watchdogfrequencyid" DataField="Watchdogfrequencyid" 
  SortExpression="Watchdogfrequencyid" HeaderText="Frequency" DropDownControlType="DropDownList" FooterText="RadComboBox column footer" > 
</telerik:GridDropDownColumn>   
protected void WDTGrid_ItemDataBound(object sender, GridItemEventArgs e)  
{  
   if (e.Item is GridEditableItem && e.Item.IsInEditMode) //fire for both edit and insert           
   {  
     GridEditableItem eeditItem = e.Item as GridEditableItem;  
     GridEditManager editMgr = editItem.EditManager;  
     GridDropDownListColumnEditor wdfEditor = editMgr.GetColumnEditor("WatchdogFrequencyddl") as GridDropDownListColumnEditor;  
      string s = DataBinder.Eval(editItem.DataItem, "Watchdogfrequencyid").ToString(); //the field should point to the ListValueField of the dropdown editor         
 
      wdfEditor.DataSource = getWatchdogFrequency();  
      wdfEditor.DataBind();  
      wdfEditor.SelectedValue = s;  
 
                
 
    }  

4 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 25 Feb 2010, 07:56 AM
Hi,

For the GridDropdownColumn to populate in the normal mode you need to assign a datasource .An alternative would be to  access the control  in the ItemDataBound event and set the value  as shown below:

C#
  if (e.Item is GridDataItem) 
        { 
            GridDataItem dataItem = (GridDataItem)e.Item; 
 
            ((Literal)dataItem["CategoryNameDropdownColumn"].Controls[0]).Text = dataItem.GetDataKeyValue("CategoryName").ToString(); 
        } 


Thanks,
Princy
0
Michael
Top achievements
Rank 1
answered on 25 Feb 2010, 01:27 PM
When I add that to my ItemDataBound event. I get the 'Object reference not set to an instance of an object' error.  This is what I added. I know I'm missing something but just can't make the connection to what it is.
Here's what I added to the ItemDataBound event:
            if (e.Item is GridDataItem)  
            {  
                GridDataItem dataItem = (GridDataItem)e.Item;  
 
                ((Literal)dataItem["WatchdogFrequencyddl"].Controls[0]).Text = dataItem.GetDataKeyValue("Name").ToString();  
            }  

Where dataItem.GetDataKeyValue("Name"). is the value I want to display. The error fires on the line starting with ((Literal)dataItem.........

Is there another way to set the datasource of a griddropdowncolumn on the server? Or is it best to set it clientside using a declarative clientside datasource?

Thanks
0
Princy
Top achievements
Rank 2
answered on 26 Feb 2010, 05:51 AM
Hi,

Please  make sure that you have added "Name" datafield to the DataKeyNames collection of RadGrid.

ASPX:
    <MasterTableView DataSourceID="SqlDataSource1"  DataKeyNames="Name"  Name="Master"


Thanks,
Princy
0
Tarang
Top achievements
Rank 1
answered on 05 Oct 2016, 07:00 AM

how the same condition fire for both edit and insert ? 

please tell so that i can get help from your post..

 

Tags
Grid
Asked by
Michael
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Michael
Top achievements
Rank 1
Tarang
Top achievements
Rank 1
Share this question
or