Searching data in datagridview - c#

I have a datagridview that displays data from a database table. There are two columns ID and NAME.
I have a textbox in which i enter name and data of those names appear in datagridview. I have achieved searching of data but i want to search as done in comboBox. When i type "a" then all names starting with "a" should appear in datagridview. Then if i type "arn" then all names starting with "arn" should appear in datagridview. I need to know if there is a built-in method or some LOGIC that i should consider.
I am using Linq to Sql.
EDIT-1
I have made a subclass in the form class
public class Table1
{
public int ID;
public string Name;
public MyList(string _newID, string _newName)
{
_id = _newID;
_name = _newName;
}
public int _id
{
get { return ID; }
set { ID = value; }
}
public string _name
{
get { return Name; }
set { Name = value; }
}
}
Used BindingList to bind it with dataGridView1. I have applied textChanged event on a textBox1.
BindingList<Table1> tempData = new BindingList<Table1>();
string name = textBox1.Text;
var result = from row in context.Tables
where row.Name == name
select row;
foreach (Table std in result)
{
tempData.Add(new MyList(row.ID, row.Name);
}
dataGridView1.DataSource = tempData;
This is how i am doing but this searches for exact same name. I want to make it like comboBox drop down search. In which at each key typed the search result gives names/strings containing those characters.
Thanks.

Try this
Entity Framework wildcards & Linq
Like operator or using wildcards in LINQ to Entities
http://www.codeproject.com/Articles/634104/Csharp-Wildcard-Search-Using-LINQ

Related

Inconsistent information in DataGrid using List Concat c# wpf

I have one table called Registry, in a Registry class.
It contains
Number, Description, People_id
People_id is a foreign key from People table, in a People class.
It contains
id, Name, birth_date
I need to show all the data from the Registry table in a DataGrid and the age for the People_id (calculated with the birth_date) in the Registry table. I already can retrieve all the Registry data and show it on the Datagrid but I'm missing the Age part.
I tried with something like this. Retrieve the Registry data, foreach to get Birth Dates, do the calculation and fill a List with only the Age result. Then, join the two List with the Concat method in a new List called CombRegistryAge as you can see below in the first method.
public List<object> RegistryJoinAge()
{
List<object> ResultAge = new List<object>();
List<Registry> ListRegistry = new Registry().LoadRegistry();
//method LoadRegistry retrieve all items in Registry table
foreach (var item in ListRegistry)
{
var age = AgeCalculation(item.People.birth_date);
ResultAge.Add(age);
}
List<object> CombRegistryAge = ListRegistry.Cast<object>().Concat(ResultAge).ToList();
return CombRegistryAge;
}
For the AgeCalculation method, something simple to start testing
private int AgeCalculation(DateTime birthdate)
{
var todayIs = DateTime.Today;
int age = todayIs.Year - birthdate.Year;
return age;
}
But when I call the method using the List CombRegistryAge in the datagrid
datagrid_fillData.ItemsSource = RegistryJoinAge();
I still get the Registry data inside but not Age and I noticed, from the datagrid, it adds empty rows for every age calculated but in reality, I need them to be show as a single one, something like this in the datagrid.
Id, Description, People_id, Age
101, Passed, 1802, 35
That's my doubt. Maybe it's a problem with the List because I tried reversing the list order in the concat method but it show and empty grid.
Any help with be appreciated.
I will suggest you create a class UserRegistryModel, to carry your data which can binding data in datagrid.
public class UserRegistryModel
{
public int Number { get; set; }
public string Description { get; set; }
public int People_id { get; set; }
public int Age { get; set; }
}
Use Lambda select make the code more clear.
public List<UserRegistryModel> RegistryJoinAge()
{
List<UserRegistryModel> ListRegistry = new Registry().LoadRegistry()
.select(x => new UserRegistryModel()
{
Number = x.Number,
Description = x.Description,
People_id = x.People_id,
Age = AgeCalculation(x.People.birth_date)
}).ToList();
return ListRegistry;
}
Final return UserRegistryModel List

How to get a recursive selection in c#?

I'm retrieving this selection from database into a datable in c#.
DataTable cataloguess = catalogue.CatalogueChirMedTech(id);
i have Categorie object, I can have under Categorie other Categorie objects(List) and under it. I can have also other Categories objects and etc(recursive).
So in c#, I want to create each Categorie with its list of categories if exists, and its modeles.Modele are rows with estterminal='o'.
Rows with pereid=-256 are parent and their children are rows with pereid is the id of the parent (typeid).
public class Categorie {
private List<Categorie> sousCategoriesField;
private List<Modele> modelesField;
public List<Categorie> Categories {
get {
return this.sousCategoriesField;
}
set {
this.sousCategoriesField = value;
}
}
public List<typeModele> modeles {
get {
return this.modelesField;
}
set {
this.modelesField = value;
}
}
}
Thanks.
First I suspect your Categorie object is going to need an identifier of some kind. Something as simple as:
public int ID { get; set; }
The recursive part should be simple enough. Logically a method might look something like this:
private List<Categorie> GetCategories(int ID, DataTable sourceData)
{
var result = GetCategoriesFromData(ID, sourceData);
foreach (var categorie in result)
categorie.Categories = GetCategories(categorie.ID, sourceData);
return result;
}
You'd need to expand on the placeholder GetCategoriesFromData in here, of course. That's really up to you and outside the scope of the recursion itself. Basically you'd query the DataTable for rows which have the given ID as their parent, convert those rows to Categorie objects (populate the ID and the modeles, whatever those are), and return that list.
But once you have this structure, the recursion is simple. All you need to do is call it with your initial "magic number" for the top-level parent "categories":
DataTable cataloguess = catalogue.CatalogueChirMedTech(id);
var categories = GetCategories(-256, cataloguess);
Then the GetCategories method will recursively populate the child categories, passing along the ID of each "parent" at each level of recursion.

Retrieve Listview items and display a column data as combo box display member and other value as value member

I have a list view with two columns- name and number. I want to read all these items and assign name to a combo box display member and number to value member. I have tried thinking the approach to follow but couldn't help myself. This is what I have tried. How should I proceed?
public class numbers
{
public string name;
public string number;
}
public class names : List<numbers>
{
}
names cname = new names();
public void addcontacts()
{
foreach(ListView lv in bufferedListView1)
{
//No idea how to proceed
First you set your own type:
public class myContact
{
public string Name { get; set; }
public string Number { get; set; }
public myContact(string name, string number)
{
this.Name = name;
this.Number = number;
}
public override string ToString()
{
return Name;
}
}
Then you transfer all items from the listview to the combobox like this:
foreach (ListViewItem item in listView1.Items)
{
comboBox1.Items.Add(new myContact(item.Text, item.SubItems[0].Text));
}
This example assumes, that each listviewitem holds the name and that its first child holds the number.
When you add objects to the combobox, C# will use the objects' ToString() method to create something that you can actually see when the program is running. You override the default ToString() method with your own and only return the name. If you want to use the selection from the combobox you just cast the selectedItem back to myContact and can access the number there. Welcome to OOP :)

How to add next column to ListBox while using DataSource?

Let's say I have a ListBox called animalList. As DataSource I use following class:
class Animal
{
private int id;
private string name;
private string description;
public Animal(int id, string name, string description)
{
// implementation
}
public int Id
{
// implementation
}
public string Name
{
// implementation
}
public string Description
{
// implementation
}
}
I'd like to have 2 columns in ListBox: Name and Description. Is it possible to do so?
I managed to add one column like this:
List<Animal> animals = // LINQ sucking data from XML
animalList.MultiColumn = true;
animalList.DataSource = animals;
animalList.DisplayMember = "Name";
animalList.ValueMember = "Id";
but I cannot really figure out how to actually implement next column.
The name of the MultiColumn property can be slightly misleading. The ListBox control does not support the type of columns you're looking for.
What MultiColumn actually does is "overflow" items into a new column instead of showing a vertical scrollbar.
http://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.multicolumn.aspx

Adding custom class objects to listbox in c#

i have a class which looks like this
public class Process_Items
{
String Process_Name;
int Process_ID;
//String Process_Title;
public string ProcessName
{
get { return Process_Name; }
set { this.Process_Name = value; }
}
public int ProcessID
{
get { return Process_ID; }
set { this.Process_ID = value; }
}
}
now i want to create a Process_Items[] Array and display all the elements in a multi column listbox. Such that first column must have the processName and 2nd must have the processID. How can i achieve this in C# 2.0?
You should use a ListView control and add two columns (ListBox only has one column)
Process_Items[] items = new Process_Items[] // Initialize array
foreach(Process_Items p in items) {
listView.Items.Add(p.ProcessName).Subitems.Add(p.ProcessID.ToString());
}
A list box has a single ListItem (string) displayed to the user.
So you could override ToString() as
public override string ToString()
{
return string.Format("{0} [ProcID: {1}]", this.Process_Name , this.ProcessID);
}
If this is for a winforms app, have a look at the ListView Control or a DataGridView
What kind of control do you use for the list? If you use a ListView, then you can do like this (assuming that instance is a Process_Items - which btw is a strange name for a class IMO - instance):
listView1.Items.Add(instance.ProcessName).SubItems.Add(instance.ProcessID.ToString());

Categories