How to check all the items in objectlistview c#? - c#

I am using Objectlistview in my WFA to create a chekedlistbox.
I want to have a button called "Select all" that the user can click on it and all the rows are selected just by one click.
I have been using the following code which works and all the checkboxes will be selected
private void btnSelectallModule_Click(object sender, EventArgs e)
{
foreach (ListViewItem item in dataListView1.Items)
{
item.Checked = true;
}
}
The problem is that when I check all the items using this button then I hover over each item it will be unchecked automatically without even clicking on that item, which is so weird because I did not intend to do that in the code.
Does anyone know what is going on and how can I fix this?
Thanks

In general do NOT manipulate the ListViewItem objects when working with ObjectListView.
There is a Method dataListView1.CheckAll() that will do exactly what you are trying to do - check all items. Using that method will properly set the internal check states of the OLV control and prevent them from getting visually unchecked when the view refreshes (when hovering the mouse over items).

Related

Loading DataGridView only once based on listbox item selected

I am developing a WinForm App using Visual Studio 2017.
I created a list box as follows.
the following dataGridview would appear when i click Fruits for the first time.
i.e whenever i press fruits i should get the above data grid. and similarly corresponding values for vegetables too. But the problem is when i click fruits one more time again the values are getting added. I get the same values again instead of just retaining it. Here is my code.
public Form1()
{
FruitList.Add("Mango");
FruitList.Add("Apple");
InitializeComponent();
}
private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
{
string a = listBox2.SelectedItem.ToString();
if (a == "Fruits")
{
toolStripStatusLabel1.Text = a;
dataGridView2.Columns.Add("key", "Fruit");
dataGridView2.Columns.Add("value", "Name");
foreach (var var in FruitList)
{
dataGridView2.Rows.Add("Fruit", var);
}
}
}
And when i select Vegetable only values related to that should appear in dataGridview. But I am getting all things addedd up as i click the item in listbox
How to load the dataGridView values only once.
I am completely new to visual studio and C#. Just exploring various features of it. It would be nice if anyone could help. Thanks in advance
As per er-shoaib 's answer after adding,
dataGridView2.Rows.Clear();
On above the line
string a = listBox2.SelectedItem.ToString();
I get the contents reloaded.
This is fine unless i dont delete or make any changes. If i delete a selected row say the row Mango using a Button control and when i press the Fruits item in List again , the items are loaded from the beginning. How to make the changes retain throughout the app. Like till i close the Form. Any help on this.
You need to clear all the rows before filling new rows to data grid view when you click on item in list box.
Add this line
dataGridView2.Rows.Clear();
On above the line
string a = listBox2.SelectedItem.ToString();
Before for loop, clear the datagrid and then start for loop and add items.
datagrid2.rows.clear().

DataGridView checkbox doesn't update until another control is clicked on

I implemented a header checkbox (from this answer) for my DataGridView that checks/unchecks all the checkboxes. It works for all checkboxes other than the first one. The first checkbox will only update its state after another control has been clicked on, as seen here:
And even later I noticed that the checkbox that doesn't update its state is the last checkbox that has been manually clicked on.
To be honest I'm not even exactly sure what's going on. What I tried though was creating an invisible dummy button and PerformClick() it, hoping that it would count as a click on a control and would update the state of the checkbox.
I also looked into Refresh(), Update(), and Invalidate(), but the checkbox cell doesn't have those methods and I couldn't do it.
This function is launched when the header checkbox is checked/unchecked:
private bool selectAllChecked = false;
private void SelectAll(object sender, EventArgs e) {
selectAllChecked = !selectAllChecked;
foreach (DataGridViewRow row in myGridView.Rows) {
DataGridViewCheckBoxCell checkb = (DataGridViewCheckBoxCell)row.Cells["Checkbox"];
checkb.Value = selectAllChecked;
}
}
Looks like I only tried to refresh/update/invalidate the checkbox cell, not the DataGridView, and also I used the wrong methods.
Calling RefreshEdit on my DataGridView (myGridView.RefreshEdit()) updated the state of the checkbox.
I'm still not sure why exactly this even happened, but that fixes it.

Telerik Searchable radcombobox not repopulating after item is selected

I have a RadComboBox that allows for custom searching for about 1500 records. Once one is selected the User control changes accordingly. If I were to want to select a new record, the selected record is the only record that displays and when you type in the search field, nothing is found either.
However, if you select a record, do what you need then click the drop down then click off it then click on it again, it resets itself as desired. How can I make it so the drop down list populates itself on the initial click?
Probably you are losing the datasource.
On the SelectedIndexChange event, you can repopulate the datasoure and set currently selected value from arguments that are passed to this event.
So in a pseudocode it would look like:
procedure OnSelectedIndexChanged(object sender, SelectedIndexChangedEventArgs e)
{
var selectedValue = e.Value;
(sender as RadComboBox).DataSource = YourMethodToGetDatasource();
(sender as RadComboBox).DataBind();
(sender as RadComboBox).SelectedValue = selectedValue;
}
and yes i know currently pseudo code doesn't look so pretty but of course you will need to use the id of your control in place of (sender as RadComboBox).
If someone know a better method for this i would be glad to gain this knowledge :)

Cannot select the ComboBox item

I have two Comboboxes where the second one is dependent upon the first one.
When the SelectedIndexChanged event of the first Combobox fires then the second Combobox will be enabled.
After the event, the second Combobox is enabled but I cannot select the ComboBox item.
EDIT
I use Dev express Tools
First Combo I load in Page_Load Event
I use Server Side code:
protected void ASPxComboModule_SelectedIndexChanged(object sender, EventArgs e)
{
LoadSecondCombo();
LoadSecondCombo.Focus();
}
There is no problem in loading, but I'm unable to select 2nd combo item.
What do this do: LoadSecondCombo(); I assume it returns an instance of the control. Where does it set combo.Enabled at?
LoadSecondCombo(); LoadSecondCombo.Focus();
look like method that reference your comboBox and Load it.
When you want to focus LoadSecondCombo should ne an instance of Combo Box. I think it is not and so your combo is not selected.
Can you try going back a bit and look at the Id of combo Box (if it in a composit control like DataGrid / View you will have to do a FindControl) and then focus. I mean typing
comboBtn1.Focus();
Now if that works then you know that your control can be found. In that case mofidify the LoadSescondCombo() to return an instance of combo button if it is not doing so already.
then create a reference to that combobutton by
ComboButton cbtn = LoadSecondCombo(); and
cbtn.Focus();

.Net C# windows forms, listbox control question

I have a pretty simple form with a listbox, a text box, and two buttons.
The list box items are populated from a sql database table. The user may chose to select one or multiple items from the listbox.
The text box is used to write more details about the items in the listbox. One button can then be clicked to update another database table with these details.
I want to make it where if any items are selected from the listbox, those contents are automatically copied into the text box field on the fly as they are selected.
Is this possible?
I've been able to make this happen on the button click event - just not on the fly as they are selected. I want it to occur before the additional details are being sent to the database
I've also tried using several different listbox events, but have been unable to obtain the results I am looking for.
Any suggestions?
try this out
you will have to handle the SelectedIndexChanged event on the listbox.
here is an example with example controls.
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
textBox1.Text = "";
foreach (string nextitem in listBox1.SelectedItems)
{
textBox1.Text += nextitem + " ";
}
}
im not too sure HOW you want the text to appear in the textbox so that would be up to you in the foreach loop.
yes, the SelectedIndexChanged event fires on every selection change, and you can concatenate together the items in the listbox. But if you are talking the description that's not visible too, you need to store the description in each listboxitem tag property, and in your code retrieve the description from there.

Categories

Resources