I want to list all the items in the radcombobox (Values are binded from dataset) but user should not be allowed to select any value from radcombobox.
User should able to see all the items but selecting an item should be disabled.
I would appreciate any help. Thanks in advance.
You can do it in aspx part of the page. Right in this way.
<telerik:RadComboBox x:Name="radComboBox" Width="200">
<telerik:RadComboBoxItem Content="Alapattah" IsEnabled="False"/>
<telerik:RadComboBoxItem Content="Brickell Avenue" />
<telerik:RadComboBoxItem Content="Downtown Miami" IsEnabled="False"/>
</telerik:RadComboBox>
But if you are binding it programatically, you can do something like this:
foreach(RadComboBoxItem item in radComboBox.Items)
{
item.Enabled = false;
}
Then in both cases user can view, but can't select disabled items.
More info is here: http://docs.telerik.com/devtools/wpf/controls/radcombobox/howto/enable-disable-radcombobox-items
Assign the data to the RadComboBox.DataSource. Then disable the combobox. "Name" and "Value" must be a part of your dataset returned from the stored proc.
In this example, I am using a EntityFramework lambda expression to get list of Users.
The table has 3 column - UserId, Name, Salary
combo.DataSource = dbCtx.tbl_users.Where(u => u.salary > 1000).OrderBy(u => u.user_id).ToList();
combo.DataTextField = "Name"
combo.DataValueField = "UserId";
combo.Enabled = false;
Related
I've got a DataGrid in a WPF application (with Entity Framework) that has a ComboBox as one of the columns in it. This ComboBox is bound to a datasource that is using a joined reference to a table that contains the names being displayed in the dropdown. It's using an ID field (called SalesActMgrID) for this join. I'm populating the dropdown list with just a List<> of specific names from that table.
My problem is that when a Name is selected from the dropdown list, it's changing the name in that joined table instead of changing the SalesActMgrID to the selected name.
I've figured out a way to update the ID in my datasource, but I haven't figured out a way to find out what name was selected in the dropdown so that I can get the correct ID for that name.
The combo column is defined as:
<DataGridComboBoxColumn
SelectedItemBinding="{Binding Path=ClientContract.StaffRole_SalesActMgr.StaffName}"
Header="Sales Act Mgr"
x:Name="salesActMgrColumn" Width="Auto" >
<DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource"
Value="{Binding staffNamesListSAM}" />
<Setter Property="IsReadOnly"
Value="True" />
</Style>
</DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>
The DataGrid is bound to a Datasource of EmployeeTime.
The complete join of the tables are:
EmployeeTime.ClientContractID is joined to ClientContract.ClientContractID {M-1}
StaffRoles.StaffRoleID is joined to ClientContract.SalesActMgrID {1-M}
I'm using the following code to perform commits on a cell by cell edit.
private bool isManualEditCommit;
private void consultantsDataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
if (!isManualEditCommit)
{
isManualEditCommit = true;
string head = e.Column.Header.ToString();
bool doCommit = true;
switch (head)
{
case "Sales Act Mgr":
{
e.Cancel = true;
//This is where I have been able to 'hard code' in a different
//ID value into the SalesActMgrID field which then correctly
//updates the value, but I need to know what name was selected
//here so I can get the correct ID for that name and set it below.
((EmployeeTime)e.EditingElement.DataContext).ClientContract.SalesActMgrID = 11;
doCommit = false;
}
break;
}
DataGrid grid = (DataGrid)sender;
if (doCommit)
{
grid.CommitEdit(DataGridEditingUnit.Row, doCommit);
EmployeeTime et = e.Row.Item as EmployeeTime;
CreateBurdenValue(et);
}
else
{
grid.CancelEdit(DataGridEditingUnit.Row);
}
isManualEditCommit = false;
}
}
}
There may be some other 'better' way to do this, which I would love to find out. At the least, if someone can point me in a direction in which I can get the selected name that was just selected before any commit action is done, I would appreciate that.
Just for info, if I let it go through and do a normal CommitEdit on the cell, the selected name is actually getting updated in the StaffRole table so on every row in the grid that displayed the original name, they get changed to the new selected name (which is not what I'm wanting).
I will just summarize what the OP posted in the comment. To access the selected item in the edit handler, use:
(e.EditingElement as ComboBox).SelectionBoxItem
I am having a combobox and the data is assigned dynamically as follows
<combobox
name="clientbox"
itemsource={Binding},
displaymemberpath="ClientName"
selectedvaluepath="clientid" />
I am loading the client details from DB and setting them to a listbox and assigning to the combobox as follows.
clientbox.DataContext = <list>
I am able to see the data in the combox after run. This will select the 0th item, but I want to default select different item. How to do this?
clientbox.SelectedItem = ((ComboBoxItem)clientbox.Items[1]);
clientbox.Text = ((ComboBoxItem)clientbox.Items[1]).Content.ToString();
There are several possibilities:
Code-behind:
// Setting the 0-based index
clientBox.SelectedIndex = 1;
// Setting the item
clientBox.SelectedItem = activeClient;
// Setting the value (explanation follows..)
clientBox.SelectedValue = activeClientValue
Using the SelectedValue property you can define a property of the item which is used to fill the ComboBox. An example: You fill the ComboBox with items of a class Client which has the properties Id, Name and so on. If you select an item, the SelectedItem property will be an instance of the class Client. By setting the SelectedValuePath property of the ComboBox to Id the SelectedValue will always just contain the id of the selected client.
Binding:
Of course you can always use bindings.
<ComboBox x:Name="clientBox"
ItemsSource={Binding ClientList}, DisplayMemberPath = "Name"
SelectedValuePath="Id"
SelectedValue={Binding ActiveClient} />
I'm not sure how to correct the following problem. I have dropdown list that has a object data source. Then in the code there is a method like this
void InitPageData()
{
MembershipUser user = Membership.GetUser();
DataSetTableAdapters.MemberInfoTableAdapter da = new DataSetTableAdapters.MemberInfoTableAdapter();
DataSet.MemberInfoDataTable dt = da.GetMember((Guid)user.ProviderUserKey);
if (dt.Rows.Count == 1)
{
DataSet.MemberInfoRow mr = dt[0];
//rank.Text = mr.rank;
//position.Text = mr.position;
UserName.Text = user.UserName;
...
}
This method populates form fields on the page. What I'm trying to do is to have the rank dropdown list populated from the ods but use this method above to populate the selected item of the rank dropwon list with the line rank.Text = mr.rank. In this example the the line of code that throws the error is commented out otherwise it throws this: "'rank' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value".
I've chaned the code to rank.DataTextFiled = mr.rank and rank.DataValueField = mr.rankid.ToString() but this threw another error: "DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'Star'." "Star" is the value of the mr.rank.
Here is what the dropdown list and the ods look like:
<asp:DropDownList runat="server" ID="rank" CssClass="txtfield" DataSourceID="ODCRanks"
DataTextField="Rank" DataValueField="ID" AppendDataBoundItems="True">
<asp:ListItem Text="--- Select a Rank ---" Value="-1" />
<asp:ObjectDataSource ID="ODCRanks" runat="server"
OldValuesParameterFormatString="original_{0}" SelectMethod="GetRanks"
TypeName="RanksTableAdapters.RankTableAdapter"></asp:ObjectDataSource>
You should try adding data columns to your data tables (with ID and Rank being the column name) so that the data can be binded to control.
The Text property sets it by value.
See http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listcontrol.text.aspx
You seem to have what will be the text associated with the value and you want to set that as the selected item. I guess this mostly because your value collection is bound to something called ID and I figure a rank of Star isn't an ID.
If Star is what will show up as something in DataTextField, use the items collection FindByText method to select it.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listitemcollection.findbytext.aspx
Example
ListItem li = DropDownList1.Items.FindByText("one");
if (li != null) li.Selected = true;
If Star is indeed an ID, then check that the collection is fully loaded before trying to select anything in it.
try something like
rank.SelectedIndex = rank.Items.IndexOf(rank.Items.FindByText(mr.rank));
I'm using a Telerik RadGrid and have a GridTemplateColumn with the unique name "ChangeAddr". Basically, a user would check one or more boxes and then click a "Change Address" button so the user could change the address for the selected rows/products.
The checkbox id is chkChangeAddr.
The DataKeyName is OrderProductID.
Now, here's the code I have for when the user clicks the button:
var OrderProductIDs = (from GridDataItem item in rgShipProducts.Items
where ((CheckBox)item.FindControl("chkChangeAddr")).Checked
select int.Parse(rgShipProducts.MasterTableView.DataKeyValues[item.ItemIndex]["OrderProductID"].ToString())).ToList();
However, it doesn't return anything.
If it helps, I have an event for OnItemDataBound where I can successfully retrieve the DataKeyValue using this same snippet:
rgShipProducts.MasterTableView.DataKeyValues[item.DataSetIndex]["OrderProductID"].ToString()
So, it seems like I'm not "accessing" each item or something. I've done this with regular ListViews, but never on a RadGrid. Any help would be greatly appreciated.
Thanks,
Andrew
try with below code.
List<GridDataItem> Items = (from item in rgShipProducts.MasterTableView.Items.Cast<GridDataItem>()
where ((CheckBox)item.FindControl("chkChangeAddr")).Checked
select item).ToList();
if (Items.Count > 0)
{
string strkey = Items[0].GetDataKeyValue("ID").ToString();
}
I can get the selected index of the gridview but i want to get the actual data that is inside the grid. I want to select a row in the grid and be able to access the "Client Id" column's actual data value. The grid is working fine and I am able to access the SelectedIndexChanged event. I have then been trying with no luck to find a way to get the information that is displayed in the grid. Any help would be greatly appreciated.
Again, I need to get access to all data that is displayed in the grid form the codebehind.
This is what data keys are for. Just designate the columns you want to access as data keys, like in the example shown below.
<telerik:RadGrid ID="RadGrid1" runat="server" ...>
<MasterTableView DataKeyNames="Column1, Column2, Column3" ...>
...
</MasterTableView>
</telerik>
Once the data keys have been assigned in the markup, you can access them in code-behind by row, or using the SelectedValues property.
if (RadGrid1.SelectedItems.Count > 0)
{
//access a string value
string column1 = RadGrid1.SelectedValues["Column1"].ToString();
//access an integer value
int column2 = (int)RadGrid1.SelectedValues["Column2"];
}
You could do it like this:
foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
{
if (item.selected == true)
string mydata = item["ColumnName"].Text;
}
I recommend you read the documentation on this site http://www.telerik.com/help/aspnet/grid/grdaccessingcellsandrows.html; it will sure help you a lot with Telerik components.
Use DataKeys as James Johnson suggested. You cannot access DataItem property of the GridDataItem in SelectedIndexChanged event. It will be null. According to Telerik documentation "DataItem object is available only when grid binds to data."
When DateItem is available, as in the ItemCreated event, you could do a cast to your original data type MyType:
private void RadGrid_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
if ((e.Item is GridDataItem)) {
GridDataItem gridDataItem = (GridDataItem)e.Item;
MyType dataItem = (MyType)gridDataItem.DataItem;
}
}