TreeView, give value - c#

I have this code:
foreach (MyClass info in data)
{
if (info.year!= "" && info.year!= null)
{
TreeViewYear.SelectedNode = TreeViewYear.RootNodes[0].Children.FirstOrDefault(x => x.Content?.ToString() == info.year);
}
}
Imagining that the foreach runs twice with the years "5" and "2", he selects correctly but then when he runs the second time, gets only the 2 value, that is, the value 2 and withdraws the 5.

If you want to select more than one item, you should set the SelectionMode of the TreeView to Multiple and add the items to be selected to the SelectedNodes property:
TreeViewYear.SelectedNodes.Add(
TreeViewYear.RootNodes[0].Children.FirstOrDefault(x => x.Content?.ToString() == info.year));

Related

Restrict items reordering in RadListBox in c#

I have a RadListBox which has 11 items. And I need to have the first 7 items as static which means they shouldn't be reordered. I have written the below javascript and it works just fine.
The 8th item in listbox is still movable(up) and this shouldn't happen. I need to reorder the items only after 7th item till 11th item and only within themselves. Means out of 11 items, first 7 items order should be static and from 8 to 11 these fields can be reordered.
Can anyone suggest how this can be achieved?
HTML:
<telerik:RadListBox ID="RadListBox" runat="server"
AllowReorder="true OnClientSelectedIndexChanging="RadListBox_Reordering"/>
JS:
function RadListBox_Reordering(sender, eventArgs) {
var value = eventArgs.get_item().get_value();
if (value == "Item1" || value == "Item2" || value == "Item3" || value == "Item4" || value == "Item5" || value == "Item6" || value == "Item7") {
eventArgs.set_cancel(true);
}
}
You can use the OnClientDropping event, which is cancelable, to detect where the reordered item is landing.
function OnClientDroppingHandler(sender, eventArgs) {
var index = args.get_destinationItem().get_index();
if (index < 8) {
alert('you may not drop on the first 7 items');
args.set_cancel(true);
}
}
https://www.telerik.com/forums/how-to-disable-dragging-dropping-to-first-position

How to set ComboBox index?

"I understand question title may be repeated but did not find a solution which I am looking for."
A groupbox which contains approx 50 controls in the combination of textboxes and comboBoxes. I have to set value for them and don't want to write 50 individual lines to set value for each control, so I came up with below code. But this is not working in case of comboboxes. OR If you guys can suggest something better that would be great.
if(controlsInGroupBox == editStep.Count)
{
int i = 0;
foreach (Control ctr in universalGroupBoxObject.Controls)
{
if (ctr is TextBox)
{
ctr.Text = editStep[i];
}
if (ctr is ComboBox)
{
//ctr.SelectedIndex = cntrlObjListMain.comboBoxLocation.FindStringExact(editStep[i]);
//ctr.SelectedIndex is not working
}
i++;
}
}
If you databind your data to the combobox then the first item is selected by default saving you having to select one.
E.G.
List<string> items = new List<string>() { "aa", "bb", "cc", "dd" };
combobox1.DataSource = items;
SelectedIndex should work as long as you have items in your list.
ctr.SelectedIndex = ctr.Items.Count > 0 ? 0 : -1;
The above will select an item if there are any items, otherwise won't select anything.

WPF filtering ListView removes Selected Items

I am struggling with filtering of listView. I tried 2 methods to filter without selected items removed.
My issue: whenever an selected item disappers because of the filter, it is no longer selected when it comes back.
My methods:
Self written code:
try
{
string s = isci_tb.Text;
if (isci_tb.Text == "")
{
osebe_listView.Items.Clear();
foreach (Oseba o in seznamOseb)
{
osebe_listView.Items.Add(o);
}
}
else
{
osebe_listView.Items.Clear();
foreach (Oseba o in seznamOseb)
{
if (contejns(o.ime, s) || contejns(o.priimek, s) || contejns(o.mobilnaSt.ToString(), s) || contejns(o.posta, s) || contejns(o.nazivPodjetja, s) || contejns(o.stacionarnaSt.ToString(), s) || contejns(o.naslov, s) || contejns(o.eMail, s))
{
osebe_listView.Items.Add(o);
}
}
}
}
catch { }
What this does is it searches all items and displayes only the one that matches with he filter, but this method removes items comepletly and re-adds them.
I tried to add a value for selected items, that saves onSelectionChanged and is used when the filter is changed, but it didn't work.
The second method was from this tutorial. I was hoping it would work because it uses a Filter, but aparently, it has the same issue.
You could try setting the Visibility of your ListViewItems depending on some property of Oseba. Then, instead of
osebe_listView.Items.Clear();
foreach (Oseba o in seznamOseb)
{
if (someConditions)
{
osebe_listView.Items.Add(o);
}
}
You could write something like this:
foreach (Oseba o in seznamOseb)
{
if (someConditions) // when true, we want to hide the object
{
o.IsVisible = false;
}
}
and in your .xaml file use a DataTemplate which sets the item's Visibility to Collapsed when IsVisible is set to false.
Another approach would be to store the IsSelected-state inside your object Oseba, so that when you remove and re-add the object to your listView, the selection state is persistent.

How to get IDs of only checked rows of a datagridview

I have a datagridview that contains list of subjects populated from Subject table from database.Columns include
Select(checkbox),
SubjectId,
SubjectName,
SubjectGroup.
Now I want if a user Selects on any of the desired rows, the corresponding SubjectId's should be added to a List. I have made and inserted into the desired table in the database.
The problem is that the new column of checkboxes I have added to this datagridview is not being detected.
My code is:
foreach (DataGridViewRow row in gvSubjectsOpted.Rows)
{
if (Convert.ToBoolean(gvSubjectsOpted.SelectedRows[0].Cells["SelectId"].Value=true))
{
olist.Add(gvSubjectsOpted.SelectedRows[0].Cells["SubjectId"].Value.ToString());
}
}
Late to the party. I had the same issue with trying to get the checkbox column by name, use the index instead. Here is a linq example assuming the checkbox is column 0 and the stored values for TrueValue and FalseVale are true and false respectively.
var checkedRows = from DataGridViewRow r in gvSubjectsOpted.Rows
where Convert.ToBoolean(r.Cells[0].Value) == true
select r;
foreach (var row in checkedRows)
{
olist.Add(row.Cells["SubjectId"].Value.ToString());
}
I realise this is an old post but I came across it and didn't think it was really answered in an efficient way so I thought I would add my method.
I have a similar block in my windows app. I read the values from the grid when the user clicks a button, and I want to know which rows they checked. As the checkboxes are in Cell 0 and the data I want is in Cell 1, I use the following code. Note the cast: it is important as it allows us the use the Where clause and therefore just a single line of code to get the collection of data. I could use the name of the cells instead of magic index numbers but then it would not fit your app so I put numbers instead (you should use names)
var checkedRows = dataGridView
.Rows
.Cast<DataGridViewRow>()
.Where(x => x.Cells[0].Value.ToString() == "1")
.Select(x => x.Cells[1]);
Note that this will give you an IEnumerable of type DataGridViewCell. If you want you can either add something like .Value.ToString() to the select or do this when you use your collection.
You question is similar to another SO question.
Check the answer of this Datagridview checkboxcolumn value and functionality.
Try this
foreach(GridViewRow r in gvSubjectsOpted.Rows)
{
GridViewCheckBoxColumn c = r.cells[0].Controls[0] as GridViewCheckBoxColumn;
if(c.Checked)
{
//Do something.
}
}
private void button1_Click(object sender, EventArgs e)
{
string subjId;
List<string> lines = new List<string>();
for (int i = 0; i < gvSubjectsList.Rows.Count; i++)
{
bool Ischecked =Convert.ToBoolean(gvSubjectsList.Rows[i].Cells["Select"].Value);
if (Ischecked == true)
{
subjId = gvSubjectsList.Rows[i].Cells["SubjectId"].Value.ToString();
lines.Add(subjId);
}
}
comboBox1.DataSource = lines;
}
//the most important thing is to set 'true' and 'false' values against newly added checkboxcolumn instead of '0' and '1'...that is,
CBColumn.FalseValue = "false";
CBColumn.TrueValue = "true";

dropdownlist taking another's value

I have two drop down lists on my page, ddlMin and ddlMax.
On page load I have it select the value of a request.querystring. When debugging it take the selected value until it reaches the next line which it then takes IT'S selected value?
http://localhost:37661/Default.aspx?search=&min=450000
ddlMin.SelectedValue = !FpsFunctions.IsNothing(Request.QueryString["min"])
? Request.QueryString["min"]
: "0"; <-- at this point it's 450000
ddlMax.SelectedValue = !FpsFunctions.IsNothing(Request.QueryString["max"])
? Request.QueryString["max"]
: "0"; <-- after this ddlMin.SelecgtedValue changes back to 0
I've checked the .cs.designer to make sure it wasn't inheriting something daft but it all seems right.
Cheers for the help in advance.
When creating my dropdownlist I was creating my items like so
foreach (string item in ConfigurationManager.AppSettings["Budget"].Split(','))
{
items = item.ToLower() == "any"
? new ListItem(item, "0")
: item.Contains("+")
? new ListItem(String.Format("{0:0,0}+", Convert.ToInt32(item.Replace("+", ""))),
"999999999")
: new ListItem(String.Format("{0:0,0}", Convert.ToInt32(item)), item);
ddlMin.Items.Add(items);
ddlMax.Items.Add(items);
}
What this meant, although I'm unclear as to why it has so much effect, was that the listitem was being attached to both dropdownlists as a singular item. So even though ddlMin was getting the right value and selecting it when the ddlMax was coming back as 0 it would select the same listitem in both... in order to correct this problem you need to just separate the adding to ddlMin and ddlMax...
foreach (string item in ConfigurationManager.AppSettings["Budget"].Split(','))
{
items = item.ToLower() == "any"
? new ListItem(item, "0")
: item.Contains("+")
? new ListItem(String.Format("{0:0,0}+", Convert.ToInt32(item.Replace("+", ""))),
"999999999")
: new ListItem(String.Format("{0:0,0}", Convert.ToInt32(item)), item);
ddlMin.Items.Add(items);
}
foreach (string item in ConfigurationManager.AppSettings["Budget"].Split(','))
{
items = item.ToLower() == "any"
? new ListItem(item, "0")
: item.Contains("+")
? new ListItem(String.Format("{0:0,0}+", Convert.ToInt32(item.Replace("+", ""))),
"999999999")
: new ListItem(String.Format("{0:0,0}", Convert.ToInt32(item)), item);
ddlMax.Items.Add(items);
}
Annoying but necessarily apparently

Categories

Resources