I have a ComboBox which is populated on Page Load event. Just after populating the ComboBox I call another method which returns a value that I want to make the default value of the ComboBox when Page loads. How can I change the selectedIndex to a value that is returned when I call another method?
XAML for ComboBox
<ComboBox Name="cboProductType" DisplayMemberPath="ProductTypeName" SelectedValuePath="ProductTypeID" SelectedIndex="0"/>
Page Load event:
void OnProductDetailLoad(object sender, RoutedEventArgs e)
{
GetServiceReference.Service1Client service = new GetServiceReference.Service1Client();
service.GetProductDetailsCompleted += service_GetProductDetailsCompleted;
service.GetProductTypeCompleted += service_GetProductTypeCompleted;
service.GetProductTypeAsync();
service.GetProductDetailsAsync(ProductId);
}
Populating ComboBox when Page Loads:
void service_GetProductTypeCompleted(object sender, GetProductTypeCompletedEventArgs e)
{
cboProductType.ItemsSource = e.Result;
}
Calling the other method which returns a particular ProductTypeName. I tried to get the index of that particular ProductTypeName but returns -1 always.
void service_GetProductDetailsCompleted(object sender, GetServiceReference.GetProductDetailsCompletedEventArgs e)
{
if (e.Result.Count != 0)
{
p.ProductID = e.Result[0].ProductID;
int index = cboProductType.Items.IndexOf(e.Result[0].ProductTypeName);
cboProductType.SelectedIndex = index;
}
Also is this a right approach for setting the SelectedIndex property?
Say I have following items loaded in ComboBox in following Index order:
Index DisplayMemberName(ProductTypeName)
0 Color
1 Size
2 Variant
3 N-size
and e.Result[0].ProductTypeName contains Variant so I now want SelectedIndex = 2 of ComboBox
I hope my question is clear
The SelectedIndex is only valid when you manually fill a combobox. If you are using the ItemsSource to fill the items in a combobox, you must define a SelectedValuePath which will then allows you to use the SelectedItem property to SET/GET the selected item. When using binding to a DataContext to bind the controls, the SelectedValuePath should be the property that links the Parent/Child together.
Related
I have a DataGrid (named OperatorDataGrid) and the row number is set via the LoadingRow event:
private void DataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
{
e.Row.Header = (e.Row.GetIndex() + 1).ToString();
}
And the DataGrid itself is bound to an ObservableCollection<FilterRow> where FilterRow as a property called Index (not currently being used). The binding is done in the user control's constructor:
public AdvancedFilter()
{
InitializeComponent();
SetDefaults();
FilterRows.Add(new FilterRow());
OperatorDataGrid.ItemsSource = FilterRows;
}
What I can't figure out is how to bind the Row.Header value back to the model as items can constantly be added/removed. The LoadingRow method handles showing the number in the DataGrid but I can't get that value back into the object bound to that specific row since the header is not a specific column that is defined in the <DataGrid.Columns> section in the XAML. I did try the solution found here, however whenever I added a new row, the header cell would show Index is 0 for every row. Any suggestions would be appreciated.
You could simply set the source property yourself when you set the Header:
private void DataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
{
int rowNumber = e.Row.GetIndex() + 1;
e.Row.Header = rowNumber.ToString();
var dataObject = e.Row.DataContext as YourClass;
if (dataObject != null)
dataObject.RowNumber = rowNumber;
}
Then you know that the row number that you see in the view is synchronized with the property of the data object.
There is no way to bind directly to a method like GetIndex() in XAML which is why you handle the LoadingRow event in the first place.
In my webfrom in asp.net I have a grid view a button, a text box and a Dropdownlist.
I have a method like this to call and select the data in to my grid view.
public void fillGridByAuthor(string searchKey)
{
GVDetails.DataSource = new ViewAllBKByAuthorOP().searchAuthorByAUNM(searchKey);
GVDetails.DataBind();
}
This is my business layer method.
public DataTable searchAuthorByAUNM(string searchKey)
{
string query2 = "EXEC SelectBooksDTByAuthor'" + searchKey + "'";
return new DataAccessLayer().executeTable(query2);
}
I'm calling fillGridByAuthor method in form in the drop downlist selected index change event like this.
protected void DDAuthor_SelectedIndexChanged(object sender, EventArgs e)
{
fillGridByAuthor(DDAuthor.Text);
}
and in the button click event like this
protected void btnSearch_Click(object sender, EventArgs e)
{
fillGridByAuthor(txtAuName.Text);
}
It is working fine when the button is clicked. Though I select the same Item in the drop down list, it doesn't give me the same output.
What's incorrect here?
From MSDN:
The Text property gets and sets the same value that the SelectedValue
property does. The SelectedValue property is commonly used to
determine the value of the selected item in the ListControl control.
If no item is selected, an empty string ("") is returned.
So the Text property returns the Value not the Text property of the currently selected item. Use SelectedItem.Text instead.
fillGridByAuthor(DDAuthor.SelectedItem.Text);
Just set AutoPostBack property of your dropdownlist to true and it will work like a charm.
Try adding autopostback = true to your dropdownlist. It will probably help
And, you should do this:
fillGridByAuthor(DDAuthor.SelectedValue);
EDIT
what Tim Schmelter is probably better because you want the text so:
fillGridByAuthor(DDAuthor.SelectedItem.Text);
Whenever we add data source to combobox with code below.
comboBoxBusNo.DataSource = busNo.Tables[0];
comboBoxBusNo.DisplayMember = "BusId";
comboBoxBusNo.ValueMember = "Id";
Why private void comboBoxBusNo_SelectedIndexChanged(object sender, EventArgs e) event is called. How we can stop this to be called at loading.
When you bind a list to a ComboBox, the first item is selected by default, which sets the SelectedIndex to 0. There are no items prior to binding so no item is selected, thus the SelectedIndex is -1 to begin with. The SelectedIndex changes from -1 to 0 so the SelectedIndexChanged event is raised.
This will stop the event from firing on load.
comboBoxBusNo.SelectedIndexChanged -= comboBoxBusNo_SelectedIndexChanged;
comboBoxBusNo.DataSource = busNo.Tables[0];
comboBoxBusNo.DisplayMember = "BusId";
comboBoxBusNo.ValueMember = "Id";
comboBoxBusNo.SelectedIndexChanged += comboBoxBusNo_SelectedIndexChanged;
I want to know how to check whether a gridview's row got selected or not.
I am working on windows application.
I want to put a if condition ie if a particular row gets selected then fill the textbox with the correspoding cell value.
I am just not getting the way how to give the condition in the if clause.
Handle the DataGridView.SelectionChanged event. Use the DataGridView.SelectedRows property to get the selected rows collection.
private void dataGridView_SelectionChanged(object sender, EventArgs e)
{
// Update the text of TextBox controls.
textBox1.Text = dataGridView.SelectedRows[0].Cells[1].Value.ToString();
textBox2.Text = dataGridView.SelectedRows[0].Cells[2].Value.ToString();
....
}
Check DataGridViewRow.Selected property.
if (dataGridView.Rows[rowIndex].Selected)
{
// Do something ..
}
Check the selected property of DataGridViewRow, it returns true for selected else false.
bool isSelected = dataGridView1.Rows[e.RowIndex].Selected;
You can subscribe to the SelectionChanged event of the control and iterate through each selected row if multi-selection is enabled or just the first one if single-row selection only.
private void MyGridView_SelectionChanged(object sender, EventArgs e)
{
for (int i = 0; i < MyGridView.SelectedRows.Count; i++)
{
MyTextBox.Text = MyGridView.SelectedRows[i].Cells[0].Value.ToString(); //assuming column 0 is the cell you're looking for
// do your other stuff
}
}
More information can be found on the SelectedRows property.
I want to use a ComboBox with the DropDownList style (the one that makes it look like a button so you can't enter a value) to insert a value into a text box. I want the combobox to have a text label called 'Wildcards' and as I select a wildcard from the list the selected value is inserted in to a text box and the combobox text remains 'Wildcard'. My first problem is I can't seem to set a text value when the combobox is in DropDownList style. Using the properties pallet doesn't work the text value is simply cleared when you click off, adding comboBox.Text = "Wildcards"; to form_load doesn't work either. Can anyone help?
The code you specify:
comboBox.Text = "Wildcards";
...should work. The only reason it would not is that the text you specify is not an item within the comboBox's item list. When using the DropDownList style, you can only set Text to values that actually appear in the list.
If it is the case that you are trying to set the text to Wildcards and that item does not appear in the list, and an alternative solution is not acceptable, you may have to be a bit dirty with the code and add an item temporarily that is removed when the drop-down list is expanded.
For example, if you have a form containing a combobox named "comboBox1" with some items and a button named "button1" you could do something like this:
private void button1_Click(object sender, EventArgs e)
{
if (!comboBox1.Items.Contains("Wildcards"))
{
comboBox1.Items.Add("Wildcards");
}
comboBox1.Text = "Wildcards";
}
private void comboBox1_DropDown(object sender, EventArgs e)
{
if (comboBox1.Items.Contains("Wildcards"))
comboBox1.Items.Remove("Wildcards");
}
That's pretty quick and dirty but by capturing the DropDownClosed event too you could clean it up a bit, adding the "Wildcards" item back as needed.
You can select one of items on formload or in form constructor:
public MyForm()
{
InitializeComponent();
comboBox.SelectedIndex = 0;
}
or
private void MyForm_Load(object sender, EventArgs e)
{
comboBox.SelectedIndex = 0;
}
Try this
comboBox1.SelectedValue = "Wildcards";
This may be a possible solution:
comboBox1.SelectedValue = comboBox1.Items.FindByText("Wildcards").Value;