how to change code to retrieve particular column in datagridview?
I have a list of data in datagridview. For example, one column is named Class (each row has a different value, see below), is it possble that to have a combo box, that contains all of the possible record Class values in the combo box? *It should be have a list of 6A, 6B, 6C If more classes are added to the database later (example 6D), these should also be in the combobox.*
A view of datagridview
Class Name
6A Jane,14 May 1980;Mary,4 June 1980;
6B leen, 31 May 1980; Peter 6 Jan 1980;
6C开发者_Go百科 Eillen, 19 Aug 1980; Yvwon, 28 Mar 1980;
6D Evan, 24 Dec 1980; Ivan, 20 Nov 1980;
Here is the code i found, but how do I change it to what i want?
var input = Convert.ToString(datagridview1.CurrentRow.Cells[0].Value);
var resultList = Regex.Matches(input, **@".*?,(.*?),.*?;")**
.Cast<Match>()
.Select(arg => arg.Groups[1].Value)
.ToList();
// bind to a combobox
comboBox1.DataSource = resultList;
If this is a Windows Forms app then you can create a class, similar to
class Obj {
public string Class { get; set; }
public string Data { get; set; }
}
then create a list of objects:
var list = new List<Obj>();
list.Add(new Obj() { Class = "6A", Data = "Jane,14 May 1980;Mary,4 June 1980;" });
// continue adding to the list here
and then
comboBox1.DataSource = list;
comboBox1.DisplayMember = "Class";
And finally hook your comboBox1.SelectedIndexChanged
to an event and grab the comboBox1.SelectedItem
, cast it to Obj and you're good.
精彩评论