Reputation: 17
Is there a way I can use a DataColumn from a DataTable as the ValueList
for an UltraGrid column?
I have the column style set to DropDownList
. The question is, what do I use for ValueList
?
ultraGrid1.DisplayLayout.Bands[0].Columns["col"].Style = ColumnStyle.DropDownList;
ultraGrid1.DisplayLayout.Bands[0].Columns["col"].ValueList = ???
Upvotes: 1
Views: 3771
Reputation: 3228
Create a BindableValueList. One of the Constructors takes the object for the datasource, the dataMember, the displayMember, valueMember, and bindingContextControl.
The line of code to set the ValueList:
this.ultraGrid1.DisplayLayout.Bands[0].Columns["col"].ValueList = new BindableValueList(dt, "", "displayColumn","valueColumn", this.ultraGrid1);
In the above example, dt is the DataTable you are binding to, displayColumn and valueColumn would be the keys for the columns that you want to use as the display and value portions of the drop down. If you are binding to a DataSet and want to bind to a table other than the first table you would use the second parameter to pass in the name of the table to bind to.
Upvotes: 3
Reputation: 216303
This could be of help. It a simple procedure that receives a DataTable, the value for the Key property of the ValueList and the name of the field to use as list for the the ValueList and returns a Value list to set in one of your UltraGridColums
public ValueList DataTableToValueList(DataTable dt, string vlKey, string fieldName)
{
ValueList vl = new ValueList();
if (!string.IsNullOrEmpty(vlKey)) vl.Key = vlKey;
foreach (DataRowView r in dt.DefaultView)
vl.ValueListItems.Add(r[fieldName]);
return vl;
}
use in this way (probably in the InitializeLayout event)
grd.DisplayLayout.Bands[0].Columns["col"].ValueList =
DataTableToValueList(dtCustomers,"vlCustomer", "CustomerName");
Upvotes: 0