Can you guys help me code if I select one of the checkboxes on the ListView section, the rest of the checkboxes should be checked.
My ListView name is lvBase and I want to used the ListView ItemCheck events.
This is my code.
private void lvBase_ItemCheck_1(object sender, ItemCheckEventArgs e)
{
}
Use the following line to add an itemcheck or itemchecked event:
this.listView1.ItemCheck += new ItemCheckEventHandler(listView1_ItemCheck);
Hope I understood your question correctly, If you want to check all the checkbox in the list, you can loop through them and set Checked Property to true.
private void lvBase_ItemChecked(object sender, ItemCheckedEventArgs e)
{
for (int i = 0; i < lvBase.Items.Count; i++)
{
lvBase.Items[i].Checked = e.Item.Checked;
}
}
Related
I'm strungling to sync. the selectedIndexs of two multi-select Listboxes.
With single-select enabled the code is just:
private void libHT_SelectedIndexChanged(object sender, EventArgs e)
{
libMonth.SelectedIndex = libHT.SelectedIndex;
}
But this doesn't work if multi-select is enabled.
Can you help me? Do I have to use a for or foreach?
Thanks for your help.
Thomas
There is the SelectedIndices property.
private void libHT_SelectedIndexChanged(object sender, EventArgs e)
{
libMonth.SelectedIndices.Clear();
foreach (var index in libHT.SelectedIndices.Cast<int>())
{
libMonth.SelectedIndices.Add(index);
}
}
Try that
Yes, you will have to loop over all the selections. Code like below can help you with that
private void libHT_SelectedIndexChanged(object sender, EventArgs e) {
libMonth.SelectedIndices.Clear();
foreach (int indx in libHT.SelectedIndices)
libMonth.SelectedIndices.Add(indx);
}
Don't forget:
To hook the index changed event: libHT.SelectedIndexChanged += libHT_SelectedIndexChanged;
To set the selection mode correctly libHT.SelectionMode = libMonth.SelectionMode = SelectionMode.MultiExtended;
To watch out for your programmatic selection, causing infinite recursion
I have this code behind a winforms which simply has a listbox as its only control:
private void Form1_Load(object sender, EventArgs e)
{
listBox1.DataSource = dtv;
listBox1.DisplayMember = "Name";
listBox1.ValueMember = "IDName";
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
DataSet dss = UseDatabase.FillDataSet("Select * From Table Where IDName=" + listBox1.SelectedValue);
string st = dss.Tables[0].Rows[0][0].ToString();
MessageBox.Show(st);
}
Run it say: "Additional information: There is no row at position 0."
Debug I see Event SelectedIndexChanged called when add one item.
Why user have not select item, that event is called
And how to fix this?
As far as I know the only way for you to fix this is by checking that SelectedIndex != -1 before doing anything else on the event handler method.
I have a ListBox in winforms Application, now the business logic demands me to fire one function if an item in the List Box is Selected and fire another if an item in the List Box is Deselected.
But the way I see it none of the Events Listed in VS is giving that power of Execution. I do see Events like
SelectedIndexChanged(object sender, EventArgs e)
and
SelectedValueChanged(object sender, EventArgs e)
But both these event fires if there is a change in the selection of the ListBox. But it doesn't specify if an item was selected or deselected which raised the event.
Any suggestion on this would be very helpful.
I even found the following link on MSDN
https://msdn.microsoft.com/en-us/library/system.windows.controls.listboxitem.unselected%28v=vs.110%29.aspx
But I am not sure how to apply the same in this situation.
Posting the Tedious Solution. Might help some1 in the future to Copy Paste.
public static int ListCount;
private void listBoxPackService_SelectedIndexChanged(object sender, EventArgs e)
{
int CurrCount;
ListBox.SelectedObjectCollection col = listBoxPackService.SelectedItems;
CurrCount = col.Count;
if (CurrCount > ListCount)
{
//Item Selected
}
else
{
//Item DeSelected
if(CurrCount == 0)
{
//All Items Were Deselected
}
}
ListCount = CurrCount;
}
On load of the ListBox
ListCount = 0
rename the Controls as per your requirement.
I am still open for a better solution :)
I've made customCheckedListBox, which I want to use to filter dataGridView with mulitselect option. I would like to be able to catch CheckedListBox CheckedChange state, but CheckedListBox only supports ItemCheck event.
Here is my code:
private void customCheckedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
FilterDataGrid();
}
private void FilterDataGrid()
{
var list4 = customCheckedListBox1.SelectedItems.Cast<string>().ToList();
if (customCheckedListBox1.SelectedItems.Count != 0)
{
var result = list3.Where(Srodek => list4.Any(x => x == Srodek.Srodek.category1));
DataTable ListAsDataTable3 = BuildDataTable2<CalaLinijka>(result);
DataView ListAsDataView3 = ListAsDataTable3.DefaultView;
dataGridView4.DataSource = view = ListAsDataView3;
}
}
Problem is that ItemCheck event can handle only one choice, so even when user decided to choose more than one opiton it will show only first selected item. I guess that CheckedChanged event would work in my case, but when ItemCheck event is called there are no CheckedItems yet. They become "Checked" after ItemCheck event is finished. So when it goes inside FilterDataGrid CheckedChanged.Count equals to 0.
My question is how should I handle CheckedChanged event in CheckedListBox. I hope that I didn't messed up too much. If there will be any questions, just let me know and I will try to expain more.
I solved this problem by using foreach loop (just like KingKing suggested) and putting it inside MouseLeave event.
private void customCheckedListBox1_MouseLeave(object sender, EventArgs e)
{
foreach (string itemChecked in customCheckedListBox1.CheckedItems)
{
CheckedList.Add(itemChecked);
}
FilterDataGrid();
}
I have a DataGridView (Selectionmode: FullRowSelect) on a windows form along with some textboxes, so what i want to do is that whenever a user selects a row(click or double_click maybe), the contents of that row must be displayed in the text boxes,
I tried out this code:
private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
MessageBox.Show("CEll Double_Click event calls");
int rowIndex = e.RowIndex;
DataGridViewRow row = dataGridView1.Rows[rowIndex];
textBox5.Text = row.Cells[1].Value;
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
int rowIndex = e.RowIndex;
DataGridViewRow row = dataGridView1.Rows[rowIndex];
textBox5.Text = dataGridView1.Rows[1].Cells[1].Value.ToString();// row.Cells[1].Value;
}
there are many other textboxes, but the main problem is that none of the event seems to be triggered, what event should i use to do so, or is there some property of datagrid that i might have set wrong?
Any help would be appreciated...:(
You can use SelectionChanged event since you are using FullRowSelect selection mode. Than inside the handler you can access SelectedRows property and get data from it. Example:
private void dataGridView_SelectionChanged(object sender, EventArgs e)
{
foreach (DataGridViewRow row in dataGridView.SelectedRows)
{
string value1 = row.Cells[0].Value.ToString();
string value2 = row.Cells[1].Value.ToString();
//...
}
}
You can also walk through the column collection instead of typing indexes...
You can try this click event
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0)
{
DataGridViewRow row = this.dataGridView1.Rows[e.RowIndex];
Eid_txt.Text = row.Cells["Employee ID"].Value.ToString();
Name_txt.Text = row.Cells["First Name"].Value.ToString();
Surname_txt.Text = row.Cells["Last Name"].Value.ToString();
First take a label.
set its visibility to false, then on the DataGridView_CellClick event write this
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
label.Text=dataGridView1.Rows[e.RowIndex].Cells["Your Coloumn name"].Value.ToString();
// then perform your select statement according to that label.
}
//try it it might work for you
You should check your designer file. Open Form1.Designer.cs and
find this line:
windows Form Designer Generated Code.
Expand this and you will see a lot of code. So check Whether this line is there inside datagridview1 controls if not place it.
this.dataGridView1.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellClick);
I hope it helps.
Simple solution would be as below. This is improvement of solution from vale.
private void dgMapTable_SelectionChanged(object sender, EventArgs e)
{
int active_map=0;
if(dgMapTable.SelectedRows.Count>0)
active_map = dgMapTable.SelectedRows[0].Index;
// User code if required Process_ROW(active_map);
}
Note for other reader, for above code to work FullRowSelect selection mode for datagridview should be used. You may extend this to give message if more than two rows selected.
You can use the SelectionChanged Event.
CurrentRow.DataBoundItem will give the bound item.
SelectionMode property should be full row select.
var item = ([CastToBindedItem])dataGridLocations.CurrentRow.DataBoundItem;
tbxEditLocation.Text = item.Name;