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

Programmatically changing the object datasource paramter bound with GridDropDownColumn

7 Answers 306 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Shan
Top achievements
Rank 1
Shan asked on 30 Dec 2011, 10:47 PM
Hi There,

I've been trying achieve the following, but could not get it done. Looking for any of your help.
  • Have two dropdown column in grid (State & County)
  • State column is bound with Object DataSource
  • County column is also bound with another Object DataSource.

But, County column is related to State. When the grid is bound with DataSource, I need to display State Name and County Name in place of their IDs (Database record would have StateID & CountyID). That's the reason I chose Dropdown columns. so that I can have simple text rendered in view mode. 

Here is what I'm doing..
  1. Assign DataSource to the grid at runtime.
  2. In the grid ItemCreated event, passing the current row's StateID to the County ODS SelectParameter.
       (by expecting that ODS would get me the list of Counties for the give StateID)

Observation:
  1. For the first row, I'm getting the County Name displayed
  2. But for the second row, its empty.

I found that, ODS Selecting event first only once (when the grid was bound), but not for all the rows.
Note: I need grid only in view mode. Not editing rows in grid.
I tried having template columns, but could not find - how to display the county name for the corresponding id. (I do not want Combo box to be displayed, only county name has to be displayed)

Pls give your helping hand to pass out from this hurdle.
If you have any other solution, pls pass it to. 

Counting upon the positive response..

<telerik:GridDropDownColumn DataField="StateID" DataSourceID="odsState"
    FilterControlAltText="Filter StateID column" HeaderText="State"
    ListTextField="DisplayText" ListValueField="ValueText" UniqueName="StateID">
</telerik:GridDropDownColumn>
 
<telerik:GridDropDownColumn DataField="CountyID" DataSourceID="odsCounty"
    FilterControlAltText="Filter CountyID column" HeaderText="County"
    ListTextField="DisplayText" ListValueField="ValueText" UniqueName="CountyID">
</telerik:GridDropDownColumn>
 
 
<asp:ObjectDataSource ID="odsState" runat="server" SelectMethod="GetStates" TypeName="MyBLL">       
</asp:ObjectDataSource>
 
<asp:ObjectDataSource ID="odsCounty" runat="server" SelectMethod="GetCounties" TypeName="MyBLL">
    <SelectParameters>
        <asp:Parameter Name="stateID" Type="String" />
    </SelectParameters>
</asp:ObjectDataSource>

protected void grid_ItemCreated(object sender, GridItemEventArgs e)
{
 
    if (e.Item is GridDataItem)
        odsCounty.SelectParameters["stateSaid"].DefaultValue = "I'm assigning the currect row's state ID here";

 //Assuming that for every row, ods selecting event would fire. But its not happening
}

7 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 02 Jan 2012, 05:44 AM
Hello Shan,

Try the following code.
C#:
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
 if (e.Item is GridEditableItem && e.Item.IsInEditMode)
 {
    GridEditableItem item = (GridEditableItem)e.Item;
    DropDownList ddl = (DropDownList)item["stateSaid"].Controls[0];
 }
}

-Shinu.
0
Shan
Top achievements
Rank 1
answered on 02 Jan 2012, 07:05 PM
Hi Shinu,

Thanks for your respone. 
I'm sorry...My grid is Not in Edit mode (as I said). I'm just displaying the rows (in browse/Read/View mode)
So, I can not access as GridEditableItem.

My actual problem is..odsCounty Selecting event is not getting executed for each row. 
How do I make it happen, so that for each row's STATEID, I need to pull the Counties from database.
And as I mapped COUNTYID data field with grid's COUNTY column, that should display CountyName in view mode (provided County Dropdown column is filled with the County IDs - b'cos of odsCounty selecting event)

This is my requirement, which is not happening. Could you please revisit and get back to me.
Thank you again.

- Shan

0
Pavlina
Telerik team
answered on 03 Jan 2012, 10:09 AM
Hello,

To achieve your goal and extract the values for the dropdown from CountyName column you should set ListTextField property to CountyName as shown below:

<telerik:GridDropDownColumn DataSourceID="odsCounty" ListTextField="CountyName"
               ListValueField="CountyID" UniqueName="CountyID" SortExpression="CountyID"
               HeaderText="RadComboBox Column" DataField="CountyID" FilterControlAltText="Filter CountyID column">
           </telerik:GridDropDownColumn>

Also, note that DataField of the column and ListValueField that is used to get the dropdown values need to match.

Additionally, I recommend you check out the following help article:
Customize/Configure GridDropDownColumn

Give it a try and let me know if I can assist you further.

Kind regards,
Pavlina
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
0
Shan
Top achievements
Rank 1
answered on 03 Jan 2012, 04:24 PM
Hi Pavlina,
Thanks for your post. I made sure that I do have correct mapping of columns as you said and also the data types.
But still could not get it done.

I think the problem is with Object DataSource not being called for each of the grid's row. It's calling the underlying stored procedure through it's "Select" method ONLY ONCE - ie, while the grid is bound with data source.
This I observed by having "odsCounty_Selecting" event with break point. But I may be wrong. 

FYRef: attached the screen shot

So, how do we make/force odsCounty's Selecting event to be fired for each rows.

Rgs,
Shan

0
Pavlina
Telerik team
answered on 06 Jan 2012, 05:50 PM
Hello Shan,

Will it be convenient for you to open a support ticket and send us a sample working project where the problem can be replicated? Thus we will be able to test it locally and advise you further.

Looking forward for your reply.

All the best,
Pavlina
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
0
Shan
Top achievements
Rank 1
answered on 16 Jan 2012, 09:44 PM
Sure I'll do that. I'm sorry for late response..held up with hectic work schedule here.
FYI: I did a workaround for this. But would like to have direct solution. Will send you once I could manage to have a sample project.

Thanks.
Shan
0
Pavlina
Telerik team
answered on 16 Jan 2012, 10:06 PM
Hello,

Please refer to the blog post below for more information about how to isolate the problem in a sample project:
http://blogs.telerik.com/blogs/posts/10-09-29/isolating-a-problem-in-a-sample-project.aspx

After we receive the project we will examine it and will get back to you with a solution.

Kind regards,
Pavlina
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
Tags
Grid
Asked by
Shan
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Shan
Top achievements
Rank 1
Pavlina
Telerik team
Share this question
or