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

Dynamic Grid column

4 Answers 123 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Paul Wood
Top achievements
Rank 1
Paul Wood asked on 26 Aug 2008, 03:03 AM
I have a table containing two fields (amongst others), being TableName and TableID. In my managing RadGrid I wish to be able to edit the TableID field using a dropdown box populated with items from the appropriate TableName.

So far I have thought of two solutions both of which I can't seem to implement using RadGrid.

1. Have a GridDropdownColumn and specify the DataSourceID, DataTextField & DataValueField server-side in an event
2. Have a GridTemplateColumn with an EditItemTemplate containing a dropdown getting the Stored Procedure do all the work and return the dropdown items with standard ID & Name fields.

I was able to get Option 2 working for the EditItemTemplate, however I've now got the issue of displaying the correct value in the ItemTemplate.

  Protected Sub grdRecordMatchup_EditCommand(ByVal source As Object, ByVal e As GridCommandEventArgs) Handles grdRecordMatchup.EditCommand 
    sqlRecordMatchupTableList.SelectParameters("ID").DefaultValue = e.Item.OwnerTableView.DataKeyValues(e.Item.ItemIndex)("LU_RecordMatchupID").ToString() 
  End Sub 

Does anyone have any thoughts how I might get Option 1 to work, or how I could modify Option 2 to get the ItemTemplate working as well?

4 Answers, 1 is accepted

Sort by
0
Paul Wood
Top achievements
Rank 1
answered on 26 Aug 2008, 03:52 AM
I have got the ItemTemplate to work for Option 2 by just doing another database lookup in grdRecordMatchup_ItemDataBound. However, I'd rather this be bound a little nicer using the same datasource used for the edititemtemplate.

If this isn't possible then I guess Option 1 is the only way to keep it full databound and clean. However I don't know which event to break in with here and how I'd make the modifications to the griddropdowncolumn
0
Veli
Telerik team
answered on 27 Aug 2008, 12:26 PM
Hi Paul Wood,

Let me first mention that Option 1 seems like it cannot automatically accomplish the requirement, as, by default, a GridDropDownColumn displays the value of the ListTextField property both in regular mode and in edit mode. Therefore you do not automatically get it to display TableID in regular mode and TableName in edit mode. This can still be achieved, but only by modifying the dropdownlist during edit form generation in the ItemDataBound event. But having reached to this requirement, Option 2 seems more feasible.

Here is how you need to proceed for Option2:

Your GridTemplateColumns can be something like this:

<telerik:GridTemplateColumn UniqueName="TemplateColumn"
    <ItemTemplate> 
        <%# Eval("ProductID") %> 
    </ItemTemplate> 
    <EditItemTemplate> 
        <telerik:RadComboBox ID="RadComboBox1" runat="server" DataSourceID="AccessDataSource1" 
            DataTextField="ProductName" DataValueField="ProductID">                                 
        </telerik:RadComboBox> 
    </EditItemTemplate> 
</telerik:GridTemplateColumn> 

Please note that AccessDataSource is also RadGrid's data source (as part of the requirement). Now you have your column display ProductID (or TableID in your case) in regular mode, and come up with a combobox with ProductNames (TableNames).

Greetings,
Veli
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Paul Wood
Top achievements
Rank 1
answered on 27 Aug 2008, 11:50 PM
Thanks for that, however I have already got that far and further with Option 2 (as described at 8/25/2008 10:52:24 PM). I want to to have the Table Name listed for the item template. At present I am doing a lookup on itemdatabound to achieve this:

  Protected Sub grdRecordMatchup_ItemDataBound(ByVal sender As Object, ByVal e As GridItemEventArgs) Handles grdRecordMatchup.ItemDataBound 
    If e.Item.ItemType = GridItemType.Item Or e.Item.ItemType = GridItemType.AlternatingItem Then 
      Dim gdiRow As GridDataItem = CType(e.Item, GridDataItem) 
      Using con As New SqlConnection(ConfigurationManager.ConnectionStrings("Intranet").ConnectionString) 
        con.Open() 
        Using cmd As New SqlCommand("usp_RecordMatchupTableItem", con) 
          cmd.CommandType = CommandType.StoredProcedure 
          cmd.Parameters.AddWithValue("@ID", e.Item.OwnerTableView.DataKeyValues(e.Item.ItemIndex)("LU_RecordMatchupID").ToString()) 
          cmd.Parameters.AddWithValue("@License", SecureCookie.GetTripleDESEncryptedCookieValue("LicenseID")) 
 
          'Get query result and populate field 
          Dim sdr As SqlDataReader = cmd.ExecuteReader() 
          While sdr.Read() 
            gdiRow("TableID").Text = sdr("Name").ToString() 
          End While 
          sdr.Close() 
        End Using 
      End Using 
    End If 
  End Sub 

I am interested in if there is a neater way of doing this (and one that doesn't require a database lookup for each line).
I guess this would be more of a question of how to do it in the SQL neatly without using cursors.
0
Accepted
Sebastian
Telerik team
answered on 01 Sep 2008, 12:55 PM
Hi Paul,

I think that the more cleaner solution for your case would be to go with Option1 and built-in GridDropDownColumn usage. If you would like to modify the set of items that will be displayed in the dropdown editor of that column in a custom manner, consider the solution discussed in the bottom section of this help topic.

Best regards,
Stephen
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
Grid
Asked by
Paul Wood
Top achievements
Rank 1
Answers by
Paul Wood
Top achievements
Rank 1
Veli
Telerik team
Sebastian
Telerik team
Share this question
or