I want to create 2 ComboBox controls. I want the first ComboBox to show Hours and the second to show Minutes.
Furthermore, I'd like to first create an Hours list and Minutes list and then bind each list to its corresponding ComboBox. Finally, I would like to combine both ComboBox controls into a single custom control.
Can anyone provide some hints on how I should proceed?
Not sure if this is what you want; see ComboBox class, to add items to combo box programmatically.
Assuming that you have two combo boxes for hours(cbxHours) and minutes(cbxMinutes), you can call SetHours(), and SetMinutes() to populate your combos.
void SetHours()
{
for (int i = 0; i < 2400; i+=100) {cbxHours.Items.Add(string.Format("{0:0###}", i));}
}
void SetMinutes()
{
for (int i = 0; i < 60; i++) { cbxMinutes.Items.Add(string.Format("{0:0#}", i)); }
}
You can accomplish your binding objective in a straightforward fashion with the following approach:
comboBox1.DataSource = Enumerable.Range(1, 12).ToList();
comboBox2.DataSource = Enumerable.Range(1, 60).ToList();
Related
I created datatable and fill it with columns and rows then i "add" this datatable to datagrid and my question is:
How to hide one row? In datatable or datagrid it doesnt matter for me. Its possible to do that?
In Windows Forms was something like CurrencyManager and that do all the job.
Okay, I found a wayto hide rows:
for (int i = 0; i < dataGrid.Items.Count; i++)
{
DataGridRow row = (DataGridRow)dataGrid.ItemContainerGenerator
.ContainerFromIndex(i);
Console.WriteLine(row.GetIndex());
// Console.WriteLine(row.Item);
Console.WriteLine((row.Item as DataRowView).Row.ItemArray[3].ToString());
string plec = (row.Item as DataRowView).Row.ItemArray[3].ToString();
if (plec == "M")
{
row.Focus();
row.Visibility = System.Windows.Visibility.Collapsed;
}
}
But when rows are Collapsed after click on column header to asceding or descending my datagrid show all rows(collapsed rows too).
How to block this and use filter only on visble rows?
I have 10 comboBox in a groupBox
for I just want to display a calculated value in respective comboBox like this say if I set a varible double i=08.00; then on button click cmboBox should display values like this
CB1-08.00
CB2-09.50
CB3-10.00
CB4-10.50
CB5-11.00
CB6-11.50
.... and so on upto CB10 But I am getting output like this
And Code
private void button1_Click(object sender, EventArgs e)
{
double i=08.00;
foreach (var comboBox in groupBox1.Controls.OfType<ComboBox>())
{
comboBox.Text = i.ToString("00.00");
i = i + 0.5;
}
}
Your combobox order is different in the collection so it inserts the numbers randomly. May be you can name your combobox for instance like cmb1,cmb2,cmb3 etc. and if you update your code it will run.
Your controls in the Controls collection are not sorted by their appearance on the form. You will need to find a way to sort them if you need different values in each based on their position.
Foreach loop doesn't give the collection in the order you wanted. The way to go forward is to give a tag id to each combo box, then you can use that to assign a value to them them.
So your first combo box will start with tag id 0, and the last one will have 8,
double val = 08.00;
for (int i = 0; i < groupBox1.Controls.Count; ++i)
{
var combobox = groupBox1.Controls[i] as ComboBox;
int tag = int.Parse(combobox.Tag.ToString());
double value = val + (0.5 * tag);
combobox.Text = value.ToString("00.00");
}
Make sure you tag the cobbo box in the order you wanted them.
I am developing an application in WPF, and a part of the application involves pulling and merging data frfom three excel sheets and displaying in datagrid. Now if a row in datagrid has a queue that corresponds to more than one application , then it will display a combobox , whoseitems will be pulled from another excel sheets by looking up the correspoding queue name in that excel sheet.
I had before used the same functionality in ASP.NET and achieved quite easily , using the following code :
for (int i = 0; i < dt.Rows.Count; i++)
{
//Filling datatable dsq from excel sheet
//dsq contains the data where it is found out how many applications are there for
//ith row in Gridview
count[i] = dsq.Tables[0].Rows.Count;
foreach (GridViewRow row in gvApplications.Rows)
{
// Transferring gridview rows to a datatable
if (row.Cells[0].Text.Trim().Equals(dt.Rows[i][0].ToString().Trim()))
{
//Dropdownlist is added only when no. of applications for each queue , i.e. ,
/count[i] is greater than 1 , else level control is added.
if (count[i] > 1)
{
DropDownList drp = new DropDownList();
drp.DataSource = dsq.Tables[0];
drp.DataTextField = "Application Name";
drp.DataValueField = "Application Name";
drp.DataBind();
row.Cells[7].Controls.Add(drp);
}
}
else
{
Label l = new Label();
l.Text = dsq.Tables[0].Rows[0][0].ToString().Trim();
l.ID = "label" + row.RowIndex.ToString();
row.Cells[7].Controls.Add(l);
}
}
Now I want the same functionality in WPf. Kindly help me out.
You can use a DataGridTemplateColumn inside that you can put a ContentControl with DataTemplateSelector.
I want to add items from 13 textboxes to the 13 elements of an array. All at one time using a button. How would I go about that?
List<double> scoreArray = new List<double>();
TextBox[] textBoxes = { week1Box, week2Box, week3Box, week4Box, week5Box, week6Box, week7Box, week8Box, week9Box, week10Box, week11Box, week12Box, week13Box };
for (int i = 0; i < textBoxes.Length; i++)
{
scoreArray.Add(Convert.ToDouble(textBoxes[i].Text));
}
Put an event handler on the button's Click event.
In the handler, extract the textbox values and set them into the values of the array.
Easy peasy!
Is it possible in a Silverlight DataGrid to have a column that has rows with different types of controls? For instance the first two rows of the column should be text, the next two rows would have buttons and then the next 6 rows would have Checkboxes. I need to build it in the code behind also. Any help would be appreciated.
Maybe the code below code gives you the idea.
foreach (var item in ItemList)
{
//Row definition and column definitions are similar
LayoutRoot.RowDefinitions.Add(new RowDefinition()
{ Height = GridLength.Auto});
HelloObject hl=new HelloPObject();
//Attached property imeplemantation
Grid.SetRow(hl,Layout2.RowDefinitions.Count);
//You may add any UIElement as Children
LayoutRoot.Children.Add(hl);
}
EDIT: Sorry I didn't realize datagrid.
Ok its possible for datagrid also ,
AFAIK Telerik's radgridview gives you the index of row .But you can manage by yourself.
Beside this when you hit grids ordering this order may be lost depending on your sortingmemberpath of column. But you can switch your CellTemplate depending a property in DataGrid's ItemsSource elements.
DataGrid grid = new DataGrid();
int rowNdx = 0;
grid.LoadingRow += (s, a) =>
{
DataGridCell cell = myList.Columns[0].GetCellContent(a.Row).Parent as DataGridCell;
switch (rowNdx)
{
case 0:
cell.Content = new TextBlock() { Text= "Click" };
break;
default:
cell.Content = new Button() { Content = "Click" };
break;
}
rowNdx++;
};