, and has a combobox as a kind of column. We also assume that each player has an Inventory property which is a collection type.
then inside the <datagrid.columns> ,
1. The current source of each column(for binding) is a Player(this is how i understand it so far). We can only bind to the property of the player not to the property of the datacontext "manager". There is no way to bind to the property of the "Manager". Am i correct?
2. However, if we move to the combobox columns, then assume i will let combobox's itemssource ='player 's inventory', then the current source for comboboxItem will be each item in the inventory. And if i use the binding, it can only bind to the property of those items.
However, sometimes i see the code that we can also bind to the property of the player inside the combobox's property especially Selected Value and SelectedItem. I am a little confused here
can you help me?
thank you
9 Answers, 1 is accepted
Here are some clarifications :
When you have a DataContext for RadGridView ... it will not be the DataContext for the combo .
At cell level the DataContext is the business object relevant to the row.
So for example :
Lets imagine the RadGridView is bound to a collection of Employee objects.
If you use ItemsSource ={Binding Countries} ,
RadGridView will search for a Countries property on the Employee object.
Now lets say you need to bind to a collection that is not in the Employee object , but in the DataContext itself.
In such case you will need to expose the collection as a static resource and use syntax like :
ItemsSource = {Binding Countries , Source={StaticResource ....}
In case all this sounds obscure, just let me know your business scenario , and I will try to provide a small sample for you.
Kind regards,
Pavel Pavlov
the Telerik team
Hi,
I am completely new to silverlight, so forgive me if I'm missing something obvious. We have a similar scenario. I have a telerik radgridview to which I am dynamically adding data using a wpf service. I have added a comboboxcolumn in the code which goes to get the relavant data from another web service function call. The problem I have at the moment is I can't see how to programatically select the correct item in the comboboxcolumn. Currently the data come into the combobox, but the combobox does not show up at all until I select the row, and then the combobox cell. Then I have to click the dropdown arrow and I find the selections. what I want is for the combobox to show up automatically with the correctly selected item in the combobox.
We are using VS 2010 and Silverlight 3. Although we would like to use Silverlight 4, our corporate policy from "the mother ship" will not able to support it at this time, so we can only use SL 3.
Since I am quite new to silverlight, I'm hoping to get advice and examples on best practices. Perhaps I went down the wrong path with doing this dynamically in the codebehind, but one of my objectives is to create generic solutions to the degree possible, and usually I get that through code. However, if XAML based configuration is much easier (from the examples I've seen XAML seems prefered) then I would want to know that as well. Anyway, thanks in advance for the help!
The dbml file has two tables:
REF_TestTable (which has a foreign key REF_Facility_LDS_LineID that links to the Primary Key ID field of the next table)
REF_Facility_LDS_Line
Here is the relavant code for the project:
Main Page Code Behind
Imports Telerik.Windows.Controls Imports Telerik.Windows.Controls.GridView Imports System.Windows.Data Partial Public Class MainPage Inherits UserControl Public Sub New() InitializeComponent() AddHandler Me.RadGridView1.CellEditEnded, AddressOf radGridView_CellEditEnded End Sub Private Sub webService_GetRowsCompleted(ByVal sender As Object, ByVal e As ServiceReference1.GetRowsCompletedEventArgs) Me.RadGridView1.ItemsSource = e.Result DirectCast(Me.RadGridView1.Columns("EntryDate"), GridViewDataColumn).DataFormatString = "{0:MM/dd/yyyy}" Dim webService As New ServiceReference1.Service1Client AddHandler webService.GetREF_Facility_LDS_LineCompleted, AddressOf webService_GetREF_Facility_LDS_LineCompleted webService.GetREF_Facility_LDS_LineAsync() End Sub Private Sub webService_GetREF_Facility_LDS_LineCompleted(ByVal sender As Object, ByVal e As ServiceReference1.GetREF_Facility_LDS_LineCompletedEventArgs) Try Dim column As New Telerik.Windows.Controls.GridViewComboBoxColumn column.ItemsSource = e.Result column.Header = "LDS" column.SelectedValueMemberPath = "ID" column.DisplayMemberPath = "LubeDistributionSystem" column.UniqueName = "cboLDS"
'-- we need the row in the combobox to be selected for the item that corresponds to the radgridview column REF_Facility_LDS_LineID
Me.RadGridView1.Columns.Insert(4, column) Catch ex As Exception End Try End Sub Private Sub GetData_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles cmdGetData.Click Dim webService As New ServiceReference1.Service1Client AddHandler webService.GetRowsCompleted, AddressOf webService_GetRowsCompleted webService.GetRowsAsync() End Sub Private Sub AddNew_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles cmdAddNew.Click Dim webService As New ServiceReference1.Service1Client webService.InsertDataAsync(Me.txtName.Text, Me.txtServerName.Text) AddHandler webService.GetRowsCompleted, AddressOf webService_GetRowsCompleted webService.GetRowsAsync() End Sub Private Sub radGridView_CellEditEnded(ByVal sender As Object, ByVal e As Telerik.Windows.Controls.GridViewCellEditEndedEventArgs) If e.EditAction = Telerik.Windows.Controls.GridView.GridViewEditAction.Cancel Then Exit Sub End If If e.EditAction = Telerik.Windows.Controls.GridView.GridViewEditAction.Commit Then Dim webService As New ServiceReference1.Service1Client Dim ID As Integer Dim Name As String Dim ServerName As String Dim Category As String Dim Active As Boolean Dim EntryDate As Date Dim REF_Facility_LDS_LineID As String ID = RadGridView1.SelectedItems.Item(0).ID Name = RadGridView1.SelectedItems.Item(0).Name ServerName = RadGridView1.SelectedItems.Item(0).ServerName Category = RadGridView1.SelectedItems.Item(0).Category Active = RadGridView1.SelectedItems.Item(0).Active EntryDate = RadGridView1.SelectedItems.Item(0).EntryDate REF_Facility_LDS_LineID = RadGridView1.SelectedItems.Item(0).REF_Facility_LDS_LineID webService.UpdateDataAsync(ID, Name, ServerName, Category, Active, EntryDate, REF_Facility_LDS_LineID) End If End Sub End Class Main Page XAML
<UserControl xmlns:my2="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Input" x:Class="Silverlight_Test3.MainPage" mc:Ignorable="d" d:DesignHeight="697" d:DesignWidth="812" xmlns:my="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data" xmlns:my1="http://schemas.telerik.com/2008/xaml/presentation" xmlns:telerikGridView="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.GridView" xmlns:dataInput="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data.Input"> <Grid x:Name="LayoutRoot" Background="AliceBlue" Height="623" Width="738"> <TextBox Height="23" HorizontalAlignment="Left" Margin="64,41,0,0" Name="txtName" VerticalAlignment="Top" Width="147" /> <TextBox Height="23" HorizontalAlignment="Left" Margin="239,41,0,0" Name="txtServerName" VerticalAlignment="Top" Width="147" /> <Button Content="Get Data" Height="38" HorizontalAlignment="Left" Margin="576,41,0,0" Name="cmdGetData" VerticalAlignment="Top" Width="147" /> <Button Content="Add New" Height="38" HorizontalAlignment="Left" Margin="415,41,0,0" Name="cmdAddNew" VerticalAlignment="Top" Width="139" /> <dataInput:Label Content="Telerik Grid" Height="18" HorizontalAlignment="Left" Margin="12,80,0,0" Name="lblTelerikGrid" VerticalAlignment="Top" Width="242" /> <dataInput:Label Content="Name" Height="18" HorizontalAlignment="Left" Margin="64,12,0,0" Name="lblName" VerticalAlignment="Top" Width="147" /> <dataInput:Label Content="Server Name" Height="18" HorizontalAlignment="Left" Margin="239,12,0,0" Name="lblServerName" VerticalAlignment="Top" Width="147" /> <telerikGridView:RadGridView AutoGenerateColumns="True" HorizontalAlignment="Left" Margin="12,104,0,0" Name="RadGridView1" VerticalAlignment="Top" Width="710" Height="493" SelectionMode="Extended" DataContext="{Binding}"> </telerikGridView:RadGridView> </Grid> </UserControl> Web Service:
Imports System.ServiceModel Imports System.ServiceModel.Activation Imports System.Collections.Generic Imports System.Text <ServiceContract(Namespace:="")> <AspNetCompatibilityRequirements(RequirementsMode:=AspNetCompatibilityRequirementsMode.Allowed)> Public Class Service1 <OperationContract()> _ Public Function GetRows() As List(Of REF_TestTable) Dim db As New DataClasses1DataContext() Dim selected_rows = From rows In db.REF_TestTables Return selected_rows.ToList() End Function <OperationContract()> _ Public Function GetREF_Facility_LDS_Line() As List(Of REF_Facility_LDS_Line) Dim db As New DataClasses1DataContext() Dim selected_rows = From rows In db.REF_Facility_LDS_Lines Return selected_rows.ToList() End Function <OperationContract()> _ Public Sub InsertData(ByVal Name As String, ByVal ServerName As String) Dim db As New DataClasses1DataContext() Dim row As New REF_TestTable() With { _ .Name = Name, _ .ServerName = ServerName, _ .Category = "P", _ .Active = True, _ .EntryDate = Today.ToShortDateString, _ .REF_Facility_LDS_LineID = "1" } ' Add the new object to the collection. db.REF_TestTables.InsertOnSubmit(row) ' Submit the change to the database. db.SubmitChanges() End Sub <OperationContract()> _ Public Sub UpdateData(ByVal ID As Integer, ByVal Name As String, ByVal ServerName As String, ByVal category As String, ByVal Active As Boolean, ByVal EntryDate As Date, ByVal REF_Facility_LDS_LineID As String) Dim db As New DataClasses1DataContext() Dim rowQuery = From REF_TestTable In db.REF_TestTables Where REF_TestTable.ID = ID Select REF_TestTable For Each REF_TestTable As REF_TestTable In rowQuery REF_TestTable.Name = Name REF_TestTable.ServerName = ServerName REF_TestTable.Category = category REF_TestTable.Active = Active REF_TestTable.EntryDate = EntryDate.ToShortDateString REF_TestTable.REF_Facility_LDS_LineID = REF_Facility_LDS_LineID Next ' Submit the change to the database. db.SubmitChanges() End Sub End Class From the code pasted , I can see that one of the things preventing the combo column to behave is the
missing setting of the DataMemberBinding .
Please set the DataMemberBinding of the ComboBoxColumn and there is a great chance for things start working better.
Greetings,
Pavel Pavlov
the Telerik team
Thank you for the information about the DataBindingMember. I was in the process of trying to figure out how to use it. In my scenario where I am tyring to do this in the code behind, and with the data as I presented it, would you be so kind as to show me an example. I tried a number of attempts to figure it out, but could not get it to work. The combobox should have as it's selected item the related REF_Facility_LDS_LineID from the RadDataGrid.
Since I do not have your database here , I will give a sample with some imaginary tables.
Lets say we have the following data structure:
We have a table with Persons. Each person holds a foreign key to the countries table in a field called CountryID .
The countries table has two fields- ID and CountryName.
We need the RadGridView to display the persons colelction so we set :
RadGridView.ItemsSource to the persons collection.
We need a combo column , displaying the country for each person ,
So we set up the column the following way
1. Set the ItemsSource of the combo column to the Counties collection
2. Set DataMemberBinding = new Binding(){Path="CountryID"}
3. Set the DisplayMemberPath = "CountryName"
4. Set the SelectedValueMemberPath to "ID"
* In case you can not achieve the same with your data, just send me your database or a simplified version of it , and I will prepare a small sample app for you with your data structure.
Sincerely yours,
Pavel Pavlov
the Telerik team
2. Set DataMemberBinding = new Binding(){Path="CountryID"}
??
could you provide a code example that corresponds to what I've done already so that I know how to integrate your advice? I tried several things and none of them worked. Thank you.
Mark
I would gladly provide a working sample project . Please let me know the structure of the relevant tables and fields in your db. + field names and data types , so I can create the sample applicable to your case.
I will need info about the primary table used as ItemsSource for the RadGridView and the foreign table containing the ItemsSource for the combo .
I was giving example with imaginary data as I do not know the structure of your DB .
If you paste me the table names and the fields relevant to the problem , I can create here a small mock of the DB and build a working project/sample over it.
Greetings,
Pavel Pavlov
the Telerik team
/****** Object: Table [dbo].[REF_TestTable] Script Date: 11/04/2010 12:59:55 ******/
SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[REF_TestTable]( [ID] [bigint] IDENTITY(1,1) NOT NULL, [Name] [varchar](50) NULL, [ServerName] [varchar](50) NULL, [Category] [varchar](1) NULL, [Active] [bit] NULL, [EntryDate] [date] NULL, [REF_Facility_LDS_LineID] [bigint] NULL, [Comments] [text] NULL, [Information] [text] NULL, [TestField1] [text] NULL, [TestField2] [text] NULL, CONSTRAINT [PK_REF_TestTable] PRIMARY KEY CLUSTERED ( [ID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] GO SET ANSI_PADDING OFF GO ALTER TABLE [dbo].[REF_TestTable] WITH CHECK ADD CONSTRAINT [FK_REF_TestTable_REF_Facility_LDS_Line] FOREIGN KEY([REF_Facility_LDS_LineID]) REFERENCES [dbo].[REF_Facility_LDS_Line] ([ID]) GO ALTER TABLE [dbo].[REF_TestTable] CHECK CONSTRAINT [FK_REF_TestTable_REF_Facility_LDS_Line] GO ALTER TABLE [dbo].[REF_TestTable] ADD CONSTRAINT [DF_REF_TestTable_EntryDate] DEFAULT (getdate()) FOR [EntryDate] GO /****** Object: Table [dbo].[REF_Facility_LDS_Line] Script Date: 11/04/2010 13:01:01 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[REF_Facility_LDS_Line]( [ID] [bigint] IDENTITY(1,1) NOT NULL, [REF_SAM_FacilityID] [int] NULL, [LubeDistributionSystem] [varchar](50) NULL, [Line] [varchar](50) NULL, [EntryDate] [date] NULL, CONSTRAINT [PK_REF_Facility_LDS_Line] PRIMARY KEY CLUSTERED ( [ID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO ALTER TABLE [dbo].[REF_Facility_LDS_Line] WITH CHECK ADD CONSTRAINT [FK_REF_Facility_LDS_Line_REF_SAM_Facility1] FOREIGN KEY([REF_SAM_FacilityID]) REFERENCES [dbo].[REF_SAM_Facility] ([ID]) GO ALTER TABLE [dbo].[REF_Facility_LDS_Line] CHECK CONSTRAINT [FK_REF_Facility_LDS_Line_REF_SAM_Facility1] GO ALTER TABLE [dbo].[REF_Facility_LDS_Line] ADD CONSTRAINT [DF_REF_Facility_LDS_Line_EntryDate] DEFAULT (getdate()) FOR [EntryDate] GO Great thanks a lot! Here are the two tables I'm working with. You can find the source code I'm working with in my original post. Much obliged!
Mark
I am attaching a sample with RadGridView set up according to the field names in your DB .
For simplicity and to avoid setup and sending of DB , I am using a small mockup classes that replicate the structure of your DB tables.
I have replaced the following VB code :
Dim column As New Telerik.Windows.Controls.GridViewComboBoxColumn column.ItemsSource = e.Result column.Header = "LDS" column.SelectedValueMemberPath = "ID" column.DisplayMemberPath = "LubeDistributionSystem" column.UniqueName = "cboLDS" with the respective settings in XAML ( see the XAML in MainPage.xaml.vb) .
the line column.ItemsSource = e.result is replaced with :
CType(Me.RadGridView1.Columns("MyComboColumn"), GridViewComboBoxColumn).ItemsSource = GetSourceForComboColumn()
To see this in action please run the app and press the GetData button.
Copying the XAML to your project and setting the items source as demonstrated should do trick for your project.
Kind regards,
Pavel Pavlov
the Telerik team