Hello all,
I have a few GridDropDownColumns in my grid. If I defined the datasource for these columns in the aspx page, everything worked perfectly. When doing edit, the dropdown list shows the correct choices with previously saved value as selected value.
But our coding guideline does not allow direct datasource on the aspx page. So I put the logic in code behind (see below)
Now I have a problem. When doing edit, even though the dropdown list is populated with the correct choices, but the previously saved value does not show at the top as selected value. I tried serveral ways to find saved value in code and always get an empty string. Does anyone have any idea what I missed?
Thank you!
Ying
I have a few GridDropDownColumns in my grid. If I defined the datasource for these columns in the aspx page, everything worked perfectly. When doing edit, the dropdown list shows the correct choices with previously saved value as selected value.
But our coding guideline does not allow direct datasource on the aspx page. So I put the logic in code behind (see below)
| protected void RadGrid1_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 agencyEditor = editMgr.GetColumnEditor("AgencyDDL") as GridDropDownListColumnEditor; |
| string s = agencyEditor.SelectedValue; //this will return "" |
| agencyEditor.DataSource = getAgency(); //call data layer function to get the agency list |
| agencyEditor.DataBind(); |
| } |
Thank you!
Ying
6 Answers, 1 is accepted
0
Hello Ying,
The built-in GridDropDownColumn is designed to be used mainly with DataTables in order to easily map and generate their dropdown items values in conjunction with the DataField/ListValueField/DataSourceID/ListDataMember values of the column.
If you want to bind the data in the grid dropdown editors programmatically, consider replacing the GridDropDownColumn with a template column holding MS DropDownList in its edit template (generating/binding the items for the MS DropDownList inside the ItemDataBound handler of RadGrid). For more details refer to this topic from the product help.
Kind regards,
Sebastian
the Telerik team
Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
The built-in GridDropDownColumn is designed to be used mainly with DataTables in order to easily map and generate their dropdown items values in conjunction with the DataField/ListValueField/DataSourceID/ListDataMember values of the column.
If you want to bind the data in the grid dropdown editors programmatically, consider replacing the GridDropDownColumn with a template column holding MS DropDownList in its edit template (generating/binding the items for the MS DropDownList inside the ItemDataBound handler of RadGrid). For more details refer to this topic from the product help.
Kind regards,
Sebastian
the Telerik team
Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Ying
Top achievements
Rank 1
answered on 30 Jun 2009, 03:54 PM
Hello Sebastian,
I am using DataTable for the GridDropDownColumn. My function getAgency() returns a DataTable. In fact, I tried also DataSet and the results is the same. Are you telling me that I can only define datasource in the page in order to use GridDropDownColumn?
Ying
I am using DataTable for the GridDropDownColumn. My function getAgency() returns a DataTable. In fact, I tried also DataSet and the results is the same. Are you telling me that I can only define datasource in the page in order to use GridDropDownColumn?
Ying
0
Hello Ying,
What I had in mind is that if you plan to assign a data source for the dropdown editor dynamically, the better choice would be to use a template column with dropdown list inside its edit template.
In case you would like to keep the built-in GridDropDownColumn, you need to assign SelectedValue or SelectedIndex to the editor right after you bind it to data. See the relevant paragraph of this help topic for more info.
To determine what to select, use DataBinder.Eval(e.Item.DataItem, "AgencyDLL").ToString() inside the ItemDataBound handler since the editor's selected value will not be available (it is not yet bound to data).
Kind regards,
Sebastian
the Telerik team
Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
What I had in mind is that if you plan to assign a data source for the dropdown editor dynamically, the better choice would be to use a template column with dropdown list inside its edit template.
In case you would like to keep the built-in GridDropDownColumn, you need to assign SelectedValue or SelectedIndex to the editor right after you bind it to data. See the relevant paragraph of this help topic for more info.
To determine what to select, use DataBinder.Eval(e.Item.DataItem, "AgencyDLL").ToString() inside the ItemDataBound handler since the editor's selected value will not be available (it is not yet bound to data).
Kind regards,
Sebastian
the Telerik team
Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Ying
Top achievements
Rank 1
answered on 02 Jul 2009, 06:30 PM
Hello Sebastian,
I tried both of your suggestions. First, I used a template column with dropdown list as its edit template. Again, the list is populated with the correct data from data source but I have no way to find what is the saved value. The document that you mentioned in your post
http://www.telerik.com/help/aspnet-ajax/grdoperationswithdropdownlistinedititemtemplate.html did not tell how I can obtain the previous saved value. Then I went back to gridDropDownColumn using the code below hoping DataBinder.Eval() will give me what I want and only to get an exception saying unknown property. Anything I am missing or doing wrong here?
I tried both of your suggestions. First, I used a template column with dropdown list as its edit template. Again, the list is populated with the correct data from data source but I have no way to find what is the saved value. The document that you mentioned in your post
http://www.telerik.com/help/aspnet-ajax/grdoperationswithdropdownlistinedititemtemplate.html did not tell how I can obtain the previous saved value. Then I went back to gridDropDownColumn using the code below hoping DataBinder.Eval() will give me what I want and only to get an exception saying unknown property. Anything I am missing or doing wrong here?
|
protected void RadGrid1_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 agencyEditor = editMgr.GetColumnEditor("AgencyDDL") as GridDropDownListColumnEditor; |
| string s = DataBinder.Eval(editItem.DataItem, "AgencyDLL").ToString(); //this throws an exception |
| DataTable dt = getAgency(); //call data layer function to get an agency datatable agencyEditor.DataSource = dt; agencyDDL.DataSource = dt; |
| agencyDDL.DataBind(); |
| agencyDDL.SelectedValue = s; |
| } |
<telerik:GridDropDownColumn HeaderText="Agency" DropDownControlType="DropDownList"
UniqueName="AgencyDDL" DataType="System.Int32" DataField="AgencyID"
EditFormColumnIndex="0" ListTextField="AgencyName"
ListValueField="AgencyID">
</telerik:GridDropDownColumn>
0
Hello Ying,
From your column definition I see that the code snippet from my post should be modified as follows:
You may also debug the code inside the ItemDataBound handler to verify that the source/selected value fro the dropdown editor are set as expected.
Best regards,
Sebastian
the Telerik team
Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
From your column definition I see that the code snippet from my post should be modified as follows:
| protected void RadGrid1_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 agencyEditor = editMgr.GetColumnEditor("AgencyDDL") as GridDropDownListColumnEditor; |
| string s = DataBinder.Eval(editItem.DataItem, "AgencyID").ToString(); //the field should point to the ListValueField of the dropdown editor |
| DataTable dt = getAgency(); //call data layer function to get an agency datatable |
| agencyEditor.DataSource = dt; |
| agencyDDL.DataSource = dt; |
| agencyDDL.DataBind(); |
| agencyDDL.SelectedValue = s; |
| } |
You may also debug the code inside the ItemDataBound handler to verify that the source/selected value fro the dropdown editor are set as expected.
Best regards,
Sebastian
the Telerik team
Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Ying
Top achievements
Rank 1
answered on 06 Jul 2009, 08:30 PM
Sebastian
Thank you for point out my mistake. It's all working well now.
Ying
Thank you for point out my mistake. It's all working well now.
Ying