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

Access GridDropDownColumn on ServerSide

3 Answers 95 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Mahmoud
Top achievements
Rank 1
Mahmoud asked on 19 Nov 2012, 11:13 AM
Hi,

I have an Issue in GridDropDownColumn .. I want to set the (DataSourceID ,, ListTextField ,, ListValueField)
in Code-behind Dynamicaly for each Row based on some Condition.

the following Code is Changing the values of each Property but It's not binding or Reflecting anything..

Designer:
<telerik:GridCheckBoxColumn DataField="IsCustomer" SortExpression="IsCustomer"
    UniqueName="IsCustomer" DataType="System.Boolean" ReadOnly="true" Visible="false" >
</telerik:GridCheckBoxColumn>
 
<telerik:GridDropDownColumn DataField="CustomerID" UniqueName="CustomerID" 
    EnableEmptyListItem="True" >
    <HeaderStyle Width="120px" />
    <ItemStyle Width="120px" />
</telerik:GridDropDownColumn>
 
<telerik:GridDropDownColumn DataField="ContactID" UniqueName="ContactID"
     EnableEmptyListItem="True" >
    <HeaderStyle Width="100px" />
    <ItemStyle Width="100px" />
</telerik:GridDropDownColumn>

Code :
Protected Sub rgOpportunities_ItemCreated(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles rgOpportunities.ItemDataBound
    If (TypeOf e.Item Is GridDataItem) Then
        'If (TypeOf e.Item Is GridEditFormItem AndAlso e.Item.IsInEditMode) Then
        Dim editFormItem As GridDataItem = CType(e.Item, GridDataItem)
        Dim Cus As GridDropDownColumn = CType(rgOpportunities.MasterTableView.Columns(8), GridDropDownColumn)
        Dim Con As GridDropDownColumn = CType(rgOpportunities.MasterTableView.Columns(9), GridDropDownColumn)
        If DirectCast(editFormItem("IsCustomer").Controls(0), CheckBox).Checked = True Then
            Cus.DataSourceID = "odsCustomer"
            Cus.ListTextField = "Name"
            Cus.ListValueField = "CustomerID"
            Con.DataSourceID = "odsContact"
            Con.ListTextField = "Name"
            Con.ListValueField = "ContactID"
        Else
            Cus.DataSourceID = "odsLeadHdr"
            Cus.ListTextField = "Name"
            Cus.ListValueField = "LeadHdrID"
            Con.DataSourceID = "odsLeadDtl"
            Con.ListTextField = "Name"
            Con.ListValueField = "LeadDtlID"
        End If
    End If
End Sub

Thanks..

3 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 19 Nov 2012, 11:39 AM
Hi,

When in browser mode, GridDropDownColumn looks and behaves like a standard GridBoundColumn. When in edit mode, however, it displays a drop-down control for each edited cell in the column. Please check the following code snippet to bind the DropDownColumn in edit mode and to show the value in the view mode.

VB:
Protected Sub RadGrid1_ItemDataBound(sender As Object, e As GridItemEventArgs)
    If TypeOf e.Item Is GridDataItem Then
                'to show the value in view mode
        Dim item As GridDataItem = DirectCast(e.Item, GridDataItem)
        Dim row As DataRowView = DirectCast(e.Item.DataItem, DataRowView)
        item("CustomerID").Text = row("CustomerID").ToString()
        item("ContactID").Text = row("ContactID").ToString()
    End If
    If TypeOf e.Item Is GridEditableItem AndAlso e.Item.IsInEditMode Then
                'to populate in edit mode
        Dim ditem As GridEditableItem = DirectCast(e.Item, GridEditableItem)
        Dim combo1 As DropDownList = DirectCast(ditem("CustomerID").Controls(0), DropDownList)
        Dim combo2 As DropDownList = DirectCast(ditem("ContactID").Controls(0), DropDownList)
        combo1.DataSourceID = "odsCustomer"
        combo1.DataTextField = "Name"
        combo1.DataValueField = "CustomerID"
        combo2.DataSourceID = "odsLeadHdr"
        combo2.DataTextField = "Name"
        combo2.DataValueField = "ContactID"
        combo1.DataBind()
        combo2.DataBind()
    End If
End Sub

Thanks,
Shinu.
0
Mahmoud
Top achievements
Rank 1
answered on 19 Nov 2012, 12:15 PM
Hi Shinu,
thanks for the Reply..

it Throws an error : 
Dim row As DataRowView = DirectCast(e.Item.DataItem, DataRowView)

Error:
Unable to cast object of type 'DynamicClass1' to type 'System.Data.DataRowView
'.

 please put in mind that I don't use edit mode in my Grid ..
Just Showing Select Statement (Show Info.)..

please Provide the full solution or alternative one as I didn't fully understand your solution.

Many Thanks..
0
Shinu
Top achievements
Rank 2
answered on 20 Nov 2012, 05:37 AM
Hi,

I suppose your requirement is to show the data bound in the dropdowncolumn in the view mode. I cannot replicate the issue you are facing. Here is the full code I tried.

ASPX:
<Columns>
    <telerik:GridDropDownColumn  HeaderText="kku" UniqueName="CustomerID" EnableEmptyListItem="True">
        <HeaderStyle Width="120px" />
        <ItemStyle Width="120px" />
    </telerik:GridDropDownColumn>
    <telerik:GridDropDownColumn  HeaderText="kk" UniqueName="ContactID" EnableEmptyListItem="True">
        <HeaderStyle Width="100px" />
        <ItemStyle Width="100px" />
</Columns>

VB:
Protected Sub RadGrid1_ItemDataBound(sender As Object, e As GridItemEventArgs)
    If TypeOf e.Item Is GridDataItem Then
        Dim editFormItem As GridDataItem = DirectCast(e.Item, GridDataItem)
        Dim Cus As GridDropDownColumn = DirectCast(RadGrid1.MasterTableView.Columns(0), GridDropDownColumn)
        Dim Con As GridDropDownColumn = DirectCast(RadGrid1.MasterTableView.Columns(1), GridDropDownColumn)
        If (editFormItem("OrderID1").Text) = "2" Then
            Cus.DataSourceID = "SqlDataSource1"
            Cus.ListTextField = "CustomerID"
            Cus.ListValueField = "CustomerID"
            Con.DataSourceID = "SqlDataSource1"
            Con.ListTextField = "EmployeeID"
            Con.ListValueField = "EmployeeID"
        Else
            Cus.DataSourceID = "SqlDataSource1"
            Cus.ListTextField = "EmployeeID"
            Cus.ListValueField = "EmployeeID"
            Con.DataSourceID = "SqlDataSource1"
            Con.ListTextField = "CustomerID"
            Con.ListValueField = "CustomerID"
        End If
        Dim item As GridDataItem = DirectCast(e.Item, GridDataItem)
        Dim row As DataRowView = DirectCast(e.Item.DataItem, DataRowView)
        If item("OrderID1").Text = "2" Then
            item("CustomerID").Text = row("CustomerID").ToString()
            item("ContactID").Text = row("EmployeeID").ToString()
        Else
            item("CustomerID").Text = row("EmployeeID").ToString()
            item("ContactID").Text = row("CustomerID").ToString()
        End If
    End If
End Sub

Thanks,
Shinu.
Tags
Grid
Asked by
Mahmoud
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Mahmoud
Top achievements
Rank 1
Share this question
or