ConfigurationDAL dAL = new ConfigurationDAL();
DataTable dt = new DataTable();
if(drp_Volunteeer.SelectedValue !=null)
{
dt = dAL.FetchVolunteerDetails(volunteerID: drp_Volunteeer.SelectedIndex).Tables[0];
foreach (DataRow dr in dt.Rows)
{
txt_FirstName.Text = dr["firstname"].ToString();
txt_fatherName.Text = dr["fathername"].ToString();
}
}
SelectedValue count shows 32:
SelectedIndex works fine:
Filling the TextBox is not working.
User Not Selected
User Selected
When I User Select Data Can't Revtive Data From DataBase:
I don't know how you can fill (add) data to SelectedBox. But you maybe know, SelectedBox's Items supports any object, you can add any objects to items. I usually create my custom class for filling SelectedBoxs. For Example, you can create Volunteeer class for adding, and viewing data on the SelectedBox. Example:
public class Volunteeer
{
private int id;
private string firstname;
private string lastname;
public Volunteeer(int pid, string fname, string lname)
{
id = pid;
firstname = fname;
lastname = lname;
}
public string GetFirstName()
{
return firstname;
}
public string GetLastName()
{
return lastname;
}
public int GetId()
{
return id;
}
public override string ToString()
{
return firstname + ' ' + lastname;
}
}
Method ToString() are for viewing text data format on the SelectedBox. After then you can very easily get selected items. Example:
private void SelectedText_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (SelectedText.SelectedIndex >= 0)
{
txtFirstName.Text = (SelectedText.SelectedValue as Volunteeer).GetFirstName();
txtLastName.Text = (SelectedText.SelectedValue as Volunteeer).GetLastName();
}
}
And this is a sample how you can fill data to SelectedBox:
SelectedText.Items.Add(new Volunteeer(1, "Sara", "Montano"));
SelectedText.Items.Add(new Volunteeer(2, "Jon", "Smith"));
SelectedText.Items.Add(new Volunteeer(3, "Tom", "Serato"));
Related
How can I send a data which is saved in a class:
public class Person
{
public int Id { get; set; }
public string Fullname { get; set; }
public string Details { get; set; }
public string UserType { get; set; }
}
to the variable?
I'm saving data (in another class) like this:
var persons = new List<Person>();
while (dr1.Read())
{
// get the results of each column
int id = (int)dr1["ID_Instructor"];
string firstname = (string)dr1["f_name"];
string lastname = (string)dr1["l_name"];
string school = (string)dr1["d_school"];
string category = (string)dr1["category"];
var person = new Person
{
Id = id,
Fullname = firstname + " " + lastname,
Details = school + " " + category
};
persons.Add(person);
}
and in the same class I want to send it to the variable from menuItem by the BindingContext. I just need to print all of this data (from one person from list)
private void MenuItem_Clicked_1(object sender, EventArgs e)
{
var menuItem = sender as MenuItem;
if (menuItem != null)
var name = menuItem.BindingContex;
I'm creating an WindowsForms application that is using a list of persons with 4 parameters (ID, Name, Surname, Permissions):
public List<Osoba> ListaOsoba()
{
Osoba nr1 = new Osoba(1, "Name", "Surname", Permissions.Administrator);
Osoba nr2 = new Osoba(2, "Name2", "Surname2", Permissions.Użytkownik);
Osoba nr3 = new Osoba(3, "Name3", "Surname3", Permissions.Użytkownik);
listaOsób.Add(nr1);
listaOsób.Add(nr2);
listaOsób.Add(nr3);
return listaOsób;
}
I would like to post all those Parameters to CheckedListBox, but show only name and surname to the user. The ID and Permissions should be hidden, but they need to exist, because I want to use them later.
Every help will be appreciated.
public static bool CheckBoxListPopulate(CheckBoxList CbList, IList<T> liSource, string TextFiled, string ValueField)
{
try
{
CbList.Items.Clear();
if (liSource.Count > 0)
{
CbList.DataSource = liSource;
CbList.DataTextField = TextFiled;
CbList.DataValueField = ValueField;
CbList.DataBind();
return true;
}
else { return false; }
}
catch (Exception ex)
{ throw ex; }
finally
{
}
}
here Cb list is the control name and
List item Ilist is the list source name
Text field (should be concatination ) ="Name" + "Surname"
Value field will be Hidden it can be "1,2,3"
so only Text field will be visible to user
To bind only name and surname to checkedboxlist first store name and surname together and then try this:
NameS = "Name" + "Surname";
((ListBox)checkedListBox).DataSource = listaOsób;
((ListBox)checkedListBox).DisplayMember = "NameS";
try this, here you have to make arbitrary compound properties for display and value member like DisplayName and HiddenId and then you can easily bound with checkedlistbox.
public class Osoba
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public Permissions Permission { get; set; }
public string DisplayName { get; set; }
public string HiddenId { get; set; }
public Osoba()
{ }
public Osoba(int id, string fname, string lname, Permissions p)
{
Id = id;
FirstName = fname;
LastName = lname;
Permission = p;
DisplayName = FirstName + " " + LastName;
HiddenId = Id + "_" + Permission.GetHashCode();
}
public void ListaOsoba()
{
List<Osoba> objList = new List<Osoba>();
Osoba nr1 = new Osoba(1, "Name", "Surname", Permissions.Administrator);
Osoba nr2 = new Osoba(2, "Name2", "Surname2", Permissions.Uzytkownik);
Osoba nr3 = new Osoba(3, "Name3", "Surname3", Permissions.Uzytkownik);
objList.Add(nr1);
objList.Add(nr2);
objList.Add(nr3);
((ListBox)checkedListBox1).DataSource = objList;
((ListBox)checkedListBox1).DisplayMember = "DisplayName";
((ListBox)checkedListBox1).ValueMember = "HiddenId";
MessageBox.Show(((ListBox)checkedListBox1).Text);
MessageBox.Show(((ListBox)checkedListBox1).SelectedValue.ToString());
}
}
public enum Permissions
{
Administrator,
Uzytkownik
}
I had a similar thing with SQL. I returned many columns, but only wanted one to show.
Anyway
ArrayList arr = new ArrayList();
foreach (object o in ListaOsoba)
{
arr.Items.Add(o[1].ToString()+" "+o[2].ToString());
}
foreach (var item in arr)
{
chkNames.Items.Add(arr.ToString()); //chkNames is your CheckListBox
}
Then later when querying which ID and such goes where, loop through you original list, and see who was ticked based on the name and surname combo, find the ID related to that person and you should be sorted
I have list usrdetails that I'm using as my data source for my DataGridview.
Two questions:
How can I change the column type for the 3rd Column in my DataGridview from a DataGridTextBoxColumn to a DataGridComboBoxColumn?
Once I've changed the 3rd column to a ComboBox how would I populate with list of strings?
public class usrInfo
{
public string userID { get; set; }
public string username { get; set; }
public string group { get; set; }
public string seclev { get; set; }
public string isext { get; set; }
public usrInfo(string userID, string username, string group, string seclev, string isext)
{
this.userID = userID;
this.username = username;
this.group = group;
this.seclev = seclev;
this.isext = isext;
}
}
public static List<usrInfo> usrdetails = new List<usrInfo>();
private void Form5_Load(object sender, EventArgs e)
{
var comboColumn = new DataGridViewComboBoxColumn();
dataGridView1.DataSource = null;
dataGridView1.DataSource = usrdetails;
for (int i = 0; i < secgrps.Count; i++)
groups.Add(secgrps[i].ToString());
comboColumn.Name ="Security Group";
comboColumn.DataSource = groups;
dataGridView1.Columns.Insert(2, comboColumn);
usrdetails.Add(new usrInfo("domain\\userID", "User Name", "RIGSITE ONLY Wellsite Leader", "7", "Y"));
dataGridView1.Refresh();
if (usrdetails.Count > -1)
num_users = true;
}
You need to add a DataGridViewComboBoxColumn to your dataGridView1 control and assign its DataSource property to the collection of strings you want.
var myStringCollection = new[] {"String1", "String2", "String3"};
var comboColumn = new DataGridViewComboBoxColumn();
comboColumn.Name = "MyComboColumn";
comboColumn.DataSource = myStringCollection; //This sets the source of drop down items
Then insert it into your grid:
if (dataGridView1.Columns["MyComboColumn"] == null)
{
//The int value as first parameter of Insert() is the desired Column Index
dataGridView1.Columns.Insert(0, comboColumn);
}
I am looking for help, I have two lists that both add data to the same list box and it displays them as a summary, I would like to know how to let the user move an index up or down in the list box.
Items are added here
private void BtnAddpickup_Click(object sender, EventArgs e)
{
/*
* This method creates a new pickup object, allows the user to
* enter details and adds it to the List
*
*/
Pickupform.pickup = new Pickups();
//New Visit- note added to the pickupform object
Pickupform.ShowDialog();
//Show the pickupForm. ShowDialog ensures that the form has the exclusive focus until it is closed.
if (Pickupform.pickup != null)
//if null then the "cancel" button was pressed
{
Pickups newpic = Pickupform.pickup;
//Get the Pickup object from the form
thePickup.addPickups(newpic);
//Add the visit to the list
}
updateList();
//Update the list object to reflect the Pickups in the list
}
and this
public Pickups getPickups(int index)
{
//Return the pickup object at the <index> place in the list
int count = 0;
foreach (Pickups pic in pickups)
{
//Go through all the pickup objects
if (index == count)
//If we're at the correct point in the list...
return pic;
//exit this method and return the current visit
count++;
//Keep counting
}
return null;
//Return null if an index was entered that could not be found
}
This is the same for my other class, So any help would be appreciated
You can try something like this. The following code assumes a Windows form containing a ListBox named mainListBox, a button named upButton, and a button named downButton.
public partial class Form1 : Form
{
private class Person
{
public string LastName { get; set; }
public string FirstName { get; set; }
public override string ToString()
{
return string.Format("{0}, {1}", LastName, FirstName);
}
}
public Form1()
{
this.InitializeComponent();
this.mainListBox.SelectionMode = SelectionMode.One;
this.PopulateListBox();
}
private void PopulateListBox()
{
this.mainListBox.Items.Add(new Person() { FirstName = "Joe", LastName = "Smith" });
this.mainListBox.Items.Add(new Person() { FirstName = "Sally", LastName = "Jones" });
this.mainListBox.Items.Add(new Person() { FirstName = "Billy", LastName = "Anderson" });
}
private void upButton_Click(object sender, EventArgs e)
{
if (this.mainListBox.SelectedIndex > 0)
{
int selectedIndex = this.mainListBox.SelectedIndex;
object selectedItem = this.mainListBox.SelectedItem;
this.mainListBox.Items.RemoveAt(selectedIndex);
this.mainListBox.Items.Insert(selectedIndex - 1, selectedItem);
this.mainListBox.SelectedIndex = selectedIndex - 1;
}
}
private void downButton_Click(object sender, EventArgs e)
{
if (this.mainListBox.SelectedIndex > -1 &&
this.mainListBox.SelectedIndex < this.mainListBox.Items.Count - 1)
{
int selectedIndex = this.mainListBox.SelectedIndex;
object selectedItem = this.mainListBox.SelectedItem;
this.mainListBox.Items.RemoveAt(selectedIndex);
this.mainListBox.Items.Insert(selectedIndex + 1, selectedItem);
this.mainListBox.SelectedIndex = selectedIndex + 1;
}
}
}
This will only work if you are adding items to the ListBox using the ObjectCollection.Add method. If you are data binding, you can update the actual data source and use the ListBox's BindingContext to refresh.
private List<Person> people = new List<Person>();
private void PopulateListBox()
{
this.people.Add(new Person() { FirstName = "Joe", LastName = "Smith" });
this.people.Add(new Person() { FirstName = "Sally", LastName = "Jones" });
this.people.Add(new Person() { FirstName = "Billy", LastName = "Anderson" });
this.mainListBox.DataSource = people;
}
private void upButton_Click(object sender, EventArgs e)
{
if (this.mainListBox.SelectedIndex > 0)
{
int selectedIndex = this.mainListBox.SelectedIndex;
Person selectedItem = this.mainListBox.SelectedItem as Person;
this.people.RemoveAt(selectedIndex);
this.people.Insert(selectedIndex - 1, selectedItem);
this.mainListBox.SelectedIndex = selectedIndex - 1;
this.RefreshListSource();
}
}
private void RefreshListSource()
{
CurrencyManager boundList = this.mainListBox.BindingContext[this.people] as CurrencyManager;
boundList.Refresh();
}
I am just adding items to a list but can't get it to work. It keeps throwing errors expected ;, expected ).
using System;
using System.Collections.Generic;
public class Employee
{
private int _id;
private string _FName;
private string _MName;
private string _LName;
private DateTime _DOB;
private char _sex;
public int ID { get { return _id; } set { _id=value; } }
public string FName{get{return _FName;}set{_FName=value;}}
public string MName { get { return _MName; } set { _MName = value; } }
public string LName { get { return _LName; } set { _LName = value; } }
public DateTime DOB { get { return _DOB; } set { _DOB = value; } }
public char Sex { get { return _sex; } set { _sex = value; } }
public List<Employee> GetEmployeeList()
{
List<Employee> empList=new List<Employee>();
empList.Add(new Employee() { ID = 1, FName = "John", MName = "", LName = "Shields", DOB = DateTime.Parse("12/11/1971"), Sex = 'M' });
empList.Add(new Employee() { ID = 2, FName = "Mary", MName = "Matthew", LName = "Jacobs", DOB = DateTime.Parse("01/17/1961"), Sex = 'F' });
empList.Add(new Employee() { ID = 3, FName = "Amber", MName = "Carl", LName = "Agar", DOB = DateTime.Parse("12/23/1971"), Sex = 'M' });
empList.Add(new Employee() { ID = 4, FName = "Kathy", MName = "", LName = "Berry", DOB = DateTime.Parse("11/15/1976"), Sex = 'F' });
return empList;
}
}
If you're using ASP.NET 2, it's possibly using the C# 2 compiler (as per your title, actually) - which doesn't support the object initializer syntax you're using. If possible, try to upgrade to use the C# 3 compiler - or create an Employee constructor taking all of the relevant parameters.
So an expression like this:
new Employee() { ID = 1, FName = "John", MName = "", LName = "Shields",
DOB = DateTime.Parse("12/11/1971"), Sex = 'M' }
would become
new Employee(1, "John", "", "Shields", "DOB", 'M')
Note that if you were using the C# 3 compiler, you wouldn't have to have all those manual calls to Add... you could just write:
var employees = new List<Employee>
{
new Employee(... stuff here...),
new Employee(... stuff here...)
};
You could still use object initializer syntax if you wanted, but the collection initializer syntax makes it simpler whatever approach you take to constructing Employee objects.
Your code compiles just fine for me. Are you running a version of C# that supports object initializers (v. 3.0+)?
Here is the full code sample using a constructor:
using System;
using System.Collections.Generic;
public class Employee
{
private int _id;
private string _FName;
private string _MName;
private string _LName;
private DateTime _DOB;
private char _sex;
public int ID { get { return _id; } set { _id = value; } }
public string FName { get { return _FName; } set { _FName = value; } }
public string MName { get { return _MName; } set { _MName = value; } }
public string LName { get { return _LName; } set { _LName = value; } }
public DateTime DOB { get { return _DOB; } set { _DOB = value; } }
public char Sex { get { return _sex; } set { _sex = value; } }
public Employee(int id, string fname, string mname, string lname, DateTime dob, char sex)
{
ID = id;
FName = fname;
MName = mname;
LName = lname;
DOB = dob;
Sex = sex;
}
public List<Employee> GetEmployeeList()
{
List<Employee> empList = new List<Employee>();
empList.Add(new Employee(1, "John", "", "Shields", DateTime.Parse("12/11/1971"), 'M'));
//etc
return empList;
}
}