How to bind List<> to a column in ListView - c#

I've got a DataTable with this fields
datatable.Columns.Add("ProductID", typeof(int));
datatable.Columns.Add("LocationList", typeof(List<string>));
datatable.Columns.Add("LocationIds", typeof(List<int>));
datatable.Columns.Add("ProductName", typeof(string));
datatable.Columns.Add("Brand", typeof(string));
datatable.Columns.Add("Price", typeof(decimal));
datatable.Columns.Add("Quantity", typeof(int));
datatable.Columns.Add("Locations", typeof(string));
And I bind it to a ListView
foreach (DataRow row in productsDataTable.Rows)
{
var item = new ListViewItem(row[0].ToString());
for (var i = 1; i < productsDataTable.Columns.Count; i++)
item.SubItems.Add(row[i].ToString());
lvSearchResults.Items.Add(item);
}
I want to bind the List<> fields, so that when a row is selected, I'd be able to get the data from the Lists and do some calculations with it.
Is there any way to do this?

The ListViewItem has a Tag property that you can store data in to retrieve and use later, for example:
foreach (DataRow row in productsDataTable.Rows)
{
var item = new ListViewItem(row[0].ToString());
// Store the specific values you want to later retrieve
item.Tag = new object[] { row["LocationList"], row["LocationIds"] };
// Or, store the whole row
item.Tag = row;
lvSearchResults.Items.Add(item);
}

The ListViewItem.Tag property allows you to associate anything you want with the item. You could store the entire DataRow in there and retrieve any columns you want.

Related

How to remove columns from ArrayList or GridView c#

Is there any way to remove "columns" from an ArrayList?
I got this site up and running before attempting populating my DropDownLists from txt files so I hard-typed each value in. Now I've made an ArrayList from each DropDownList so I can display those lists in DataGridView on the site. The only issue is that "Enabled" and "Selected" show up as columns and I cannot seem to remove the column in the ArrayList, or specify which columns to bring in when creating the ArrayList, or GridView using GridView.Columns.Remove(); because the integers 0 or 1 or 2 don't seem to correspond with anything and the site doesn't run and I can't specify a string as the column title for what to remove.
The DataGrids show up with columns as |Enabled|Selected|Text|Value|
Here's the code for this piece as it stands (You can see what I've tried out and that didn't work that I've commented away):
// Create ListArrays from DropDownLists
ArrayList BuildingList = new ArrayList(Building.Items);
ArrayList DepartmentList = new ArrayList(Department.Items);
//Building.Items.Remove("Enabled");
//Building.Items.Remove("Selected");
// Populate Building GridView
BuildingGrid.DataSource = BuildingList;
BuildingGrid.DataBind();
//BuildingGrid.Columns.Remove;
//BuildingGrid.Columns[0].Visible = false;
// Populate Department GridView
DepartmentGrid.DataSource = DepartmentList;
DepartmentGrid.DataBind();
//DepartmentGrid.Columns[0].Visible = false;
//DepartmentGrid.Columns[1].Visible = false;
I would just go ahead and create a simple 2d array in a txt file with fields for "Value" and "Text" so the DropDownList will pull it in properly, but I can't figure that out either without being terribly inefficient and confusing.
Any help would be appreciated. Thanks.
So, here's the solution I ended up at. I finally figured out how to extract everything from a txt file, and place it into the grid the way I wanted to.
// Populate Department GridView
// get all lines of csv file
string[] BuildingString = File.ReadAllLines(Server.MapPath("Content/BuildingsCSV.csv"));
string[] DepartmentString = File.ReadAllLines(Server.MapPath("Content/DepartmentsCSV.csv"));
// create new datatable
DataTable BuildingTable = new DataTable();
DataTable DepartmentTable = new DataTable();
// Building Table
// get the column header means first line
string[] tempbuild = BuildingString[0].Split(',');
// creates columns of gridview as per the header name
foreach (string t in tempbuild)
{
BuildingTable.Columns.Add(t, typeof(string));
}
// now retrive the record from second line and add it to datatable
for (int i = 1; i < BuildingString.Length; i++)
{
string[] t = BuildingString[i].Split(',');
BuildingTable.Rows.Add(t);
}
// Department Table
// get the column header means first line
string[] tempdept = DepartmentString[0].Split(',');
// creates columns of gridview as per the header name
foreach (string t in tempdept)
{
DepartmentTable.Columns.Add(t, typeof(string));
}
// now retrive the record from second line and add it to datatable
for (int i = 1; i < DepartmentString.Length; i++)
{
string[] t = DepartmentString[i].Split(',');
DepartmentTable.Rows.Add(t);
}
// assign gridview datasource property by datatable
BuildingGrid.DataSource = BuildingTable;
BuildingGrid.DataBind();
BuildingGrid.Rows[0].Visible = false;
DepartmentGrid.DataSource = DepartmentTable;
DepartmentGrid.DataBind();
DepartmentGrid.Rows[0].Visible = false;
foreach (DataRow drb in BuildingTable.Rows)
{
BuildingDrop.Items.Add(new ListItem(drb[0].ToString(), drb[1].ToString()));
}
foreach (DataRow drd in DepartmentTable.Rows)
{
DepartmentDrop.Items.Add(new ListItem(drd[0].ToString(), drd[1].ToString()));
}

Create Dynamic properties from Datatable

I have below class with one property.
public class MfrYearEqpType
{
public string EqpType { get; set; }
public MfrYearEqpType()
{
}
}
I wanted to create dynamic properties(Datatable column names) to my class "MfrYearEqpType" and set the values from below Datatable.
DataTable dt = getData();
This table contains 26 rows and 10 columns of data.
I have gone through the below link. But I am not sure how to handle my case.
Dynamically adding properties to an Object from an existing static object in C#
And also I have used ExpandoObject. I have done below sample.
DataTable dt = getData();
List<dynamic> expandoList = new List<dynamic>();
foreach (DataRow row in dt.Rows)
{
//create a new ExpandoObject() at each row
var expandoDict = new ExpandoObject() as IDictionary<String, Object>;
foreach (DataColumn col in dt.Columns)
{
//put every column of this row into the new dictionary
expandoDict.Add(col.ToString(), row[col.ColumnName].ToString());
}
//add this "row" to the list
expandoList.Add(expandoDict);
}
But my aim is to create the List of MfrYearEqpType. So that I can bind List of MfrYearEqpType to Gridview.
Please help me on this.
Assuming you are talking about ASP.NET here, you can directly bind your data table to a GridView's DataSource property.
If you are happy with these 10 columns displaying as default, you can set AutoGenerateColumns property to true. Otherwise, you may want to define <asp:BoundField /> or <asp:TemplateField /> columns as appropriate. Bound fields will let you at least format the value for a particular column while template fields will allow full customization where you can provide an defining how to render a particular field.

Adding List<string> to DataTable

I created a class library and I am trying to add data from a List<string[]> to a DataGridView.
The string is in the format as follows:
"Test, 1^Glucose^10/24/2013 10:00;Test, 2^BUN^10/25/2013 11:00;Test, 3^BUN^10/25/2013 11:00"
I am passing the string from another program. I then an making it into a list and then trying to add it to the DataTable but no such luck. I created the datagridview with four columns.
Selected - Checkbox
Patient Name - String
Order Name - String
Order Date - String
Reason - Combo Box
I am getting an error:
the list is greater than the number of columns.
Note: I can make the string anyway I want before passing to this program so if I need to do something with the string before passing it to the program, please let me know. Is there an easier way?
I just want the data to display and I will work on the rest of it.
Any help would be appreciated.
This is my code:
public partial class RenewOrders : Form
{
public static string strMLMPatientData = string.Empty;
public RenewOrders(string all_patient_data)
{
InitializeComponent();
strMLMPatientData = "Test, 1^Glucose^10/24/2013 10:00;Test, 2^BUN^10/25/2013 11:00;Test, 3^BUN^10/25/2013 11:00"
}
private void RenewOrders_Load(object sender, EventArgs e)
{
this.ConvertStringToList(strMLMPatientData);
}
private void ConvertStringToList(string strMLMPatientData)
{
var patient_list = strMLMPatientData.Split(';').Select(x => x.Split('^')).ToList();
DataTable dtTable = ConvertListToDataTable(patient_list);
dataGridView1.DataSource = dtTable;
}
// Convert to DataTable.
static DataTable ConvertListToDataTable(List<string[]> patient_list)
{
// New table.
DataTable dtTable = new DataTable();
dtTable.Columns.Add("Name", typeof(string));
dtTable.Columns.Add("Order Name", typeof(string));
dtTable.Columns.Add("Order Date/Time", typeof(string));
foreach (var row in patient_list)
{
table.Rows.Add(row);
}
return dtTable;
}
}
First you need to spit the string appropriately to get a list of string arrays. Something like this:
var patient_list = new List<string[]>(strMLMPatientData.Split(';').Select(x => x.Split(',')));
or even better:
var patient_list = strMLMPatientData.Split(';').Select(x => x.Split(',')).ToList();
You need Linq for that, but you get the idea.
Then you need to add columns to your data table. You cant add rows to it when there are no columns..
Try something like this in your function
//add columns appropriately
DataTable table = new DataTable();
table.Columns.Add("Name", typeof(string));
table.Columns.Add("Order", typeof(string));
table.Columns.Add("Date", typeof(string));
foreach (var row in patient_list)
table.Rows.Add(row);
return table;
See an example here. As it stands, your comma separated input string doesnt seem to match your data table column structure. You need to work it out. But I hope you got the idea which way to go.

Extract a value from Datatable and store to a collection in c#

I want to extract a value from the datatable and store to a collection. What would be the best way to do it?
The rows in my Datatable will have data like :
Row 1 <configuration><Store parameter="Atribs">AB,CD</Store></configuration>
Row 2 <configuration><Store parameter="Atribs">EF,GH,IJ</Store></configuration>
......
I want to retrieve and store the values in a collection like,
Collection 1 :
AB
CD
Collection 2 :
EF
GH
IJ
Collection ....
Here you go...
var table = new DataTable();
var column = new DataColumn("col1");
table.Columns.Add(column);
var row = table.NewRow();
row[0] = #"<configuration><Store parameter=""Atribs"">AB,CD</Store></configuration>";
table.Rows.Add(row);
row = table.NewRow();
row[0] = #"<configuration><Store parameter=""Atribs"">EF,GH,IJ</Store></configuration>";
table.Rows.Add(row);
var data = new List<List<string>>();
foreach (DataRow dRow in table.Rows)
{
var temp = new List<string>();
string xml = dRow.Field<string>("col1");
var element = XElement.Parse(xml);
string[] values = element.Descendants("Store").First().Value.Split(',');
temp.AddRange(values);
data.Add(temp);
}
You say a DataTable, but provided xml data in your example, so I am not sure.. bu basically, this is what you need to do:
var myList = new List<string>();
foreach (DataRow row in dataTable.Rows)
{
myList.Add(row.Field<string>("myCol"));
}
Something like that. It would be easier to give a better example if you could provide us with info about your DataTable and not your source xml file. :)
You might need to be a bit more clear on what you mean by "datatable" in that your example data is XML. Provided you know enough to fetch the rows from the data/xml:
Read each row and use String.Split() on the inner value to generate a string array which can be fed into a List.
var list = new List<string>(String.Split(",", innerData));
Of course you'll want to validate that the inner data has content.

Sort DataTable rows by length in c#

How to do following scenario:
I have some DataTable which contains for example some rows:
1.rowa
2.rowab
3.row
4.rowaba
...
n. rowabba
How to sort rows by lenght, not by name. I wan to to sort Table by length of fields.
You could add an extra column to your DataTable, supplying an expression containing a call to the len() function, causing the column's values to be automatically computed:
table.Columns.Add("LengthOfName", typeof(int), "len(Name)");
Then you can simply sort on your new column before binding the DataTable to a grid to whatever kind of UI control you plan to use:
table.DefaultView.Sort = "LengthOfName";
If you must use DataTable, you could introduce an extra column for the sort. In this case, you could set the value in the column simply to the length of each desired cell, and then sort by the new column:
DataTable table = new DataTable();
DataColumn val = table.Columns.Add("Value", typeof(string));
table.Rows.Add("abc");
table.Rows.Add("defgh");
table.Rows.Add("i");
table.Rows.Add("jklm");
// sort logic: ***** schou-rode's "len(...)" approach is better *****
DataColumn sort = table.Columns.Add("Sort", typeof(int));
foreach (DataRow row in table.Rows) {
row[sort] = ((string)row[val]).Length;
}
DataView view = new DataView(table);
view.Sort = "Sort";
foreach (DataRowView row in view) {
Console.WriteLine(row.Row[val]);
}
Personally, I'd use a typed list - of either a class, or a string in this case (since you only list one value):
List<string> list = new List<string> {
"abc", "defgh", "i", "jklm"};
list.Sort((x, y) => x.Length.CompareTo(y.Length));
foreach (string s in list) {
Console.WriteLine(s);
}

Categories

Resources