I have a RadGrid with a column that uses a ComboBox in edit mode. I have it defined as follows:
I'm setting the DataSource in my code behind:
Currently in display mode on the grid, if the value is null, it displays the first item in the ComboBox. And of course when you go into edit mode on the grid, it has the first item in the list selected.
What I would like to do is if the value is null, display a blank field in display mode on the grid, and when in edit mode, display a "Please Select a Value" text with a specific value that I can check to avoid writing anything to the database if they don't select a value. I could also handle having the ComboBox show a blank when going into edit mode instead of a "Please Select a Value".
Can someone point me in the right direction for this? Thanks
<telerik:GridTemplateColumn UniqueName="den_ldr_id" HeaderText="Den Leader"> |
<ItemStyle Width="175" /> |
<ItemTemplate> |
<asp:Label Width="170" CssClass="gridDropDown" runat="server" ID="lbl_den_ldr_id" Text='<%#DataBinder.Eval(Container.DataItem, "adult_nm")%>' /> |
</ItemTemplate> |
<EditItemTemplate> |
<telerik:RadComboBox Width="170" MarkFirstMatch="true" Skin="scSkin" EnableEmbeddedSkins="false" DataTextField="adult_nm" DataValueField="adult_id" DataSourceID="DenLeaderDataSource" ID="dd_den_ldr_id" runat="server" SelectedValue='<%#Bind("den_ldr_id") %>' /> |
</EditItemTemplate> |
</telerik:GridTemplateColumn> |
I'm setting the DataSource in my code behind:
DenLeaderDataSource.ConnectionString = ConfigurationManager.ConnectionStrings("LocalSqlServer").ToString |
DenLeaderDataSource.SelectCommand = "SELECT [sc_adult].[adult_id], [sc_adult].[adult_fnm] + ' ' + [sc_adult].[adult_lnm] AS [adult_nm] FROM [sc_adult] WHERE [sc_adult].[unit_id] = " & Profile.UnitID |
Currently in display mode on the grid, if the value is null, it displays the first item in the ComboBox. And of course when you go into edit mode on the grid, it has the first item in the list selected.
What I would like to do is if the value is null, display a blank field in display mode on the grid, and when in edit mode, display a "Please Select a Value" text with a specific value that I can check to avoid writing anything to the database if they don't select a value. I could also handle having the ComboBox show a blank when going into edit mode instead of a "Please Select a Value".
Can someone point me in the right direction for this? Thanks
7 Answers, 1 is accepted
0

Philip
Top achievements
Rank 1
answered on 11 Feb 2008, 09:31 AM
Almost got this one figured out. I set the TEXT property of the ComboBox to "Select a Value" and removed the data-binding on the ComboBox.
This works great for display and for when they select an item, but if they don't want to select an item from the list and leave it on "Select a Value", I get an error when the Update button is clicked. The error occurs on this line in the code behind:
So I need to find a way to figure out if the user hasn't select anything from this drop-down so that I can bypass the above variable setting and insert a null value into the field in my UPDATE command.
Any ideas? Thanks :)
This works great for display and for when they select an item, but if they don't want to select an item from the list and leave it on "Select a Value", I get an error when the Update button is clicked. The error occurs on this line in the code behind:
Dim den_ldr_id As String = (TryCast(item.FindControl("dd_den_ldr_id"), RadComboBox)).SelectedItem.Value |
So I need to find a way to figure out if the user hasn't select anything from this drop-down so that I can bypass the above variable setting and insert a null value into the field in my UPDATE command.
Any ideas? Thanks :)
0

Philip
Top achievements
Rank 1
answered on 11 Feb 2008, 09:44 AM
Thought I had another problem, but the above post still stands.
0

Philip
Top achievements
Rank 1
answered on 11 Feb 2008, 11:49 PM
Okay...almost there :)
I decided to go ahead and use ASP validators to force the user to select a value. Even though it's not what I really want to do, I can live with it.
So I added my validators and a validation summary in line on the edit command column. Everything works great, except I can't get the ShowMessageBox="true" property to actually pop a window.
Does this property of ValidationSummary work within a grid edit command column or am I stuck dumping the error messages directly into the row?
And if you have a solution to my original question, I'd still like to see that since it's my preferred method of handling this. Thanks :)
I decided to go ahead and use ASP validators to force the user to select a value. Even though it's not what I really want to do, I can live with it.
So I added my validators and a validation summary in line on the edit command column. Everything works great, except I can't get the ShowMessageBox="true" property to actually pop a window.
Does this property of ValidationSummary work within a grid edit command column or am I stuck dumping the error messages directly into the row?
And if you have a solution to my original question, I'd still like to see that since it's my preferred method of handling this. Thanks :)
0
Hi Philip,
There seems to be a problem with the Message Box of the ValidationSummary control. Please read more about it in the following forum thread: ValidationSummary Issue.
As for determining whether there is a selected item in the RadComboBox you can simply check whether the selected index of the control is higher than -1.
Another thing you can do is to add an item at index 0 in the RadComboBox with Text="Select a Value" and Value=Nothing. Then determining the selected item's value would be done like this:
I hope this helps.
Kind regards,
Simeon
the Telerik team
Instantly find answers to your questions at the new Telerik Support Center
There seems to be a problem with the Message Box of the ValidationSummary control. Please read more about it in the following forum thread: ValidationSummary Issue.
As for determining whether there is a selected item in the RadComboBox you can simply check whether the selected index of the control is higher than -1.
If RadComboBox1.SelectedIndex > -1 Then |
Dim den_ldr_id As String = (TryCast(item.FindControl("dd_den_ldr_id"), RadComboBox)).SelectedItem.Value |
End If |
Another thing you can do is to add an item at index 0 in the RadComboBox with Text="Select a Value" and Value=Nothing. Then determining the selected item's value would be done like this:
If RadComboBox1.SelectedItem <> Nothing Then |
Dim den_ldr_id As String = (TryCast(item.FindControl("dd_den_ldr_id"), RadComboBox)).SelectedItem.Value |
End If |
I hope this helps.
Kind regards,
Simeon
the Telerik team
Instantly find answers to your questions at the new Telerik Support Center
0

Philip
Top achievements
Rank 1
answered on 12 Feb 2008, 04:31 PM
All very good suggestions and some things I have already tried, except that the ComboBox is inside a Grid Edit Template, so I cannot access it directly from the code-behind as in your example. I also can't figure out how to add a row in the code-behind.
If you could give me a hand with either one of those, I'd really appreciate it...either how to reference the ComboBox in the code-behind when it's in a grid edit template so that I can check the value, or the best way to add a row to the ComboBox (either client side or server side) when it's in a grid edit template.
Thanks for the assistance.
If you could give me a hand with either one of those, I'd really appreciate it...either how to reference the ComboBox in the code-behind when it's in a grid edit template so that I can check the value, or the best way to add a row to the ComboBox (either client side or server side) when it's in a grid edit template.
Thanks for the assistance.
0
Hi Philip,
This is not as straightforward task as it seems, but it can be easily implemented by hooking to the RadGrid's events OnItemDataBound and OnUpdateCommand.
In the handler of the first event you could populate the RadComboBox for the first time or modify its Items collection if it is already bound.
In the second event handler you could get the value of the RadComboBox when the currently edited row is updated.
Please, check this help article for more information on the matter: Operations with MS DropDownList in EditItemTemplate of GridTemplateColumn (you could assume that there is a RadComboBox instead of MS DropDownList without any complications).
I hope this helps.
Best wishes,
Simeon
the Telerik team
Instantly find answers to your questions at the new Telerik Support Center
This is not as straightforward task as it seems, but it can be easily implemented by hooking to the RadGrid's events OnItemDataBound and OnUpdateCommand.
In the handler of the first event you could populate the RadComboBox for the first time or modify its Items collection if it is already bound.
In the second event handler you could get the value of the RadComboBox when the currently edited row is updated.
Please, check this help article for more information on the matter: Operations with MS DropDownList in EditItemTemplate of GridTemplateColumn (you could assume that there is a RadComboBox instead of MS DropDownList without any complications).
I hope this helps.
Best wishes,
Simeon
the Telerik team
Instantly find answers to your questions at the new Telerik Support Center
0
Accepted

Philip
Top achievements
Rank 1
answered on 13 Feb 2008, 06:37 PM
Once again you guys are incredible!
It actually ended up being a really easy solution once I read the document you linked to. I simply added a procedure for ItemDataBound which inserted an item into the dropdown with some text and a value of 0. Then in my UpdateCommand procedure, I checked to see if the value of the dropdown was 0. If it was, I wrote a NULL to the database column, otherwise I wrote the value. I didn't need to use Session variables at all. If anyone else needs the code, here it is:
I removed all the other fields that I'm updating to make it simpler to view.
Once again, you guys have been great. Thanks!!!
It actually ended up being a really easy solution once I read the document you linked to. I simply added a procedure for ItemDataBound which inserted an item into the dropdown with some text and a value of 0. Then in my UpdateCommand procedure, I checked to see if the value of the dropdown was 0. If it was, I wrote a NULL to the database column, otherwise I wrote the value. I didn't need to use Session variables at all. If anyone else needs the code, here it is:
Private Sub RadGrid1_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid1.ItemDataBound |
If (TypeOf e.Item Is GridEditableItem AndAlso e.Item.IsInEditMode) Then |
Dim item As GridEditableItem = e.Item |
Dim combo As RadComboBox = item.FindControl("dd_den_id") |
combo.Items.Insert(0, New RadComboBoxItem("Select a Den", "0")) |
End If |
End Sub |
Protected Sub RadGrid1_UpdateCommand(ByVal source As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) |
Dim item As GridEditableItem = e.Item |
Dim sql As String = "" |
Dim scout_id As String = item.OwnerTableView.DataKeyValues(item.ItemIndex)("scout_id").ToString() |
Dim den_id As String = (TryCast(item.FindControl("dd_den_id"), RadComboBox)).SelectedItem.Value |
sql = "UPDATE [sc_scout] SET " |
If den_id = "0" Then |
sql += "[den_id]=NULL " |
Else |
sql += "[den_id]='" + den_id + "' " |
End If |
sql += "WHERE [scout_id]=" + scout_id + " " |
Try |
Dim connstr = ConfigurationManager.ConnectionStrings("LocalSqlServer").ToString |
Dim dbconn As New SqlConnection(connstr) |
dbconn.Open() |
Dim cmd As New SqlCommand(sql, dbconn) |
cmd.ExecuteNonQuery() |
dbconn.Close() |
Catch ex As Exception |
RadGrid1.Controls.Add(New LiteralControl("Unable to update Scout. Reason: " + ex.Messaage)) |
e.Canceled = True |
End Try |
End Sub |
I removed all the other fields that I'm updating to make it simpler to view.
Once again, you guys have been great. Thanks!!!