I seem to have a problem with changing my datasource.
I have 2 Objects: One with InvoiceLines made up of the following properties: int id, int index, string name and Order order
My Order object is made up of the following properties: int id and string name.
The goal is to be able to select, per InvoiceLine, a corresponding Order by selecting an Order with a combobox. the comboboxcolumn is created via this code:
gvTest.Columns.Add(new GridViewComboBoxColumn { HeaderText = "Order name", FieldName = "Order", DataSource = orderLines, DisplayMember = "Order.Name", ValueMember = ""});
problem is that when running the form, in the Combobox column, the first thing showed is: GridViewTets.Models.Order (which should be the name of the Order) and when selecting another object Order in the combobox and confirming the edit an errorbox shows with this message translated from dutch: an object of type System.String cannot be cast to type GridViewTest.Models.Order.
6 Answers, 1 is accepted
An easier way to explain all this is: I want to change my valuemember to an object in the comboboxcolumn datasource, not the property of an object.
I have created a small example that shows how you can implement this:
public
partial
class
RadForm1 : Telerik.WinControls.UI.RadForm
{
public
RadForm1()
{
InitializeComponent();
BindingList<InvoiceLines> data =
new
BindingList<InvoiceLines>();
for
(
int
i = 0; i < 10; i++)
{
var obj =
new
InvoiceLines() { Id = i, Index = i, Name =
"Name"
};
obj.order =
new
Order { Id = i, Name =
"Name "
+ i };
data.Add(obj);
}
radGridView1.DataSource = data;
var comboColumn =
new
GridViewComboBoxColumn();
comboColumn.Name =
"CustomOrderColumn"
;
comboColumn.Width = 100;
comboColumn.FieldName =
"Order"
;
comboColumn.DisplayMember =
"order.Name"
;
comboColumn.ValueMember =
"order"
;
comboColumn.DataSource = data;
radGridView1.Columns.Add(comboColumn);
}
}
class
InvoiceLines
{
public
int
Id {
get
;
set
; }
public
int
Index {
get
;
set
; }
public
string
Name {
get
;
set
; }
public
Order order {
get
;
set
; }
}
class
Order : IComparable
{
public
int
Id {
get
;
set
; }
public
string
Name {
get
;
set
; }
public
int
CompareTo(
object
obj)
{
return
Name.CompareTo(((Order)obj).Name);
}
public
override
string
ToString()
{
return
Name;
}
}
I hope this will be useful. Let me know if you have additional questions.
Regards,
Dimitar
Telerik by Progress
I am not sure what exactly you have meant by "assigned". Could you please elaborate?
Perhaps you can use separate data sources for the grid and the column.
Regards,
Dimitar
Telerik by Progress
Hi Dimitar,
What I mean is i can have 10 Orders but only 2 InvoiceLines and would have to be able to select an order when making an invoiceline which is not assigned yet to an InvoiceLIne yet. In the example you gave me I am unable to do this. And when using different datasources I am unable to set the value to orders.
Kind regards,
Erik Stevens,
I have extended the example and now it uses two data sources and there are orders that are not yet assigned. In addition, I have added a filter so only the "unassigned" orders are appearing in the drop down.
I hope this will be useful.
Regards,
Dimitar
Telerik by Progress