what´s the fastest way of adding a row of values in a arraylist?
for example, if I have a arraylist
arraylist arrayvalues = new arraylist();
arrayvalues.add("name1");
arrayvalues.add(3.56);
how do I add this arraylist as a new row in a datagridview?
and how do I add it as a new column?
| | penninha Thursday, July 20, 2006 10:15 PM | hi,
yes it does work with me here its what i have done so far
1) creat a project
2) add datagridview to the form
3) add class to the project and copy the content of the web page and past it in that class , then add the constractor to the class
4) add the 2 methods in the previous code to the form, but don't forget to add those 2 methods to the form constractor
it will work just notice i didn't use arrayList but i used a generic collection List<T>
here its the entire code
| |
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
namespace WindowsApplication1 { public partial class Form1 : Form
{ List<DemoCustomer> list = new List<DemoCustomer>(); public Form1() { InitializeComponent(); FillList(); ShowInDataGridView(); } private void FillList() { DemoCustomer cust1 = new DemoCustomer("Egyptian", "Mycompany", "1"); DemoCustomer cust2 = new DemoCustomer("Lebanese", "Libcompany", "2"); DemoCustomer cust3 = new DemoCustomer("Palestinian", "Palcompany", "3"); list.Add(cust1); list.Add(cust2); list.Add(cust3); } private void ShowInDataGridView() { dataGridView1.DataSource = list; } } // This class implements a simple customer type
// that implements the IPropertyChange interface.
public class DemoCustomer : INotifyPropertyChanged
{ // These fields hold the values for the public properties.
private Guid idValue = Guid.NewGuid(); private string customerNameValue = String.Empty; private string companyNameValue = String.Empty; private string phoneNumberValue = String.Empty; public event PropertyChangedEventHandler PropertyChanged; private void NotifyPropertyChanged(String info) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(info)); } } // The constructor is private to enforce the factory pattern.
private DemoCustomer() { customerNameValue = "no data"; companyNameValue = "no data"; phoneNumberValue = "no data"; } public DemoCustomer(string customer, string company, string value) { customerNameValue = customer; companyNameValue = company; phoneNumberValue = value; } // This is the public factory method.
public static DemoCustomer CreateNewCustomer() { return new DemoCustomer(); } public string CustomerName { get { return this.customerNameValue; } set
{ if (value != this.customerNameValue) { customerNameValue = value; NotifyPropertyChanged("CustomerName"); } } } // This property represents an ID, suitable
// for use as a primary key in a database.
public Guid ID { get
{ return this.idValue; } }
public string CompanyName { get { return this.companyNameValue; } set
{ if (value != this.companyNameValue) { this.companyNameValue = value; NotifyPropertyChanged("CompanyName"); } } } public string PhoneNumber { get { return this.phoneNumberValue; } set
{ if (value != this.companyNameValue) { this.phoneNumberValue = value; NotifyPropertyChanged("PhoneNumber"); } } } } }
|
best regards | | Egyptian Friday, July 21, 2006 7:51 AM | believe it or not I was also thinking about this earlier today.
So, Am I correct in saying that you wish to add the items in a specific column?
if this is the case....
| |
int currentRow = 0; foreach (string curObj in theCollection) { this.theDataGridView.Rows.Add(); //add a new row this.theDataGridView.Rows[counter].Cells["MyColumnName"].Value = curObj; //set the current value in the foreach loop to the current row of the column specified currentRow++; }
|
hope it helps! | | ahmedilyas Tuesday, July 25, 2006 5:31 PM | To add a new column in DataGridView:
| |
this.theDataGridView.Columns.Add("ColumnName", "HeaderName");
|
To add a new row:
| |
DataGridViewRow theRow = new DataGridViewRow(); this.theDataGridView.Rows.Add(theRow);
|
And as for binding the arraylist to datagridview....I dont think you can do it, as when I tried, it would just show the length of the arraylist.
you may need to loop through your array and add a new row into the datagridview and add the item to it
something like....
| |
int counter = 0; foreach (object curObj in theArrayList) { this.theDataGridView.Rows.Add(new DataGridViewRowCollection(this.theDataGridView)); this.theDataGridView.Rows[counter].SetValues(curObj); counter++; }
|
probably not the best efficient way, but ill be sure to post when I find a better version! | | ahmedilyas Thursday, July 20, 2006 11:12 PM | hi,
to show records like that in a datagridview you have to
1) make a collection of the same type to represent a single row , adding parts like that will not work,
2) use a collection that represent a type i didn't use arraylist b4 to do that but i use the generic collections
3) the best thing to do is to use INotifyPropertyChanged interface, this a link about how to implement it
i have tried to implement it with List<> and it worked fine just drop a datagridview in your form, also i have added a constractor to the class in this page http://msdn2.microsoft.com/en-us/library/ms229614.aspx
| |
namespace WindowsApplication1 { public partial class Form1 : Form
{ List<DemoCustomer> list = new List<DemoCustomer>(); public Form1() { InitializeComponent(); FillList(); ShowInDataGridView(); } private void FillList() { DemoCustomer cust1 = new DemoCustomer("Egyptian", "Mycompany", "1"); DemoCustomer cust2 = new DemoCustomer("Lebanese", "Libcompany", "2"); DemoCustomer cust3 = new DemoCustomer("Palestinian", "Palcompany", "3"); list.Add(cust1); list.Add(cust2); list.Add(cust3); } private void ShowInDataGridView() { dataGridView1.DataSource = list; } }
public class DemoCustomer : INotifyPropertyChanged
{ // i have added this part to the class and the rest are the same as teh webpage public DemoCustomer(string customer, string company, string value) { customerName = customer; companyNameValue = company; phoneNumberValue = value; } } }
|
hope this helps | | Egyptian Friday, July 21, 2006 12:29 AM | Egyptian - that does not quite work as I tried this during my own post. | | ahmedilyas Friday, July 21, 2006 12:56 AM | hi,
yes it does work with me here its what i have done so far
1) creat a project
2) add datagridview to the form
3) add class to the project and copy the content of the web page and past it in that class , then add the constractor to the class
4) add the 2 methods in the previous code to the form, but don't forget to add those 2 methods to the form constractor
it will work just notice i didn't use arrayList but i used a generic collection List<T>
here its the entire code
| |
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
namespace WindowsApplication1 { public partial class Form1 : Form
{ List<DemoCustomer> list = new List<DemoCustomer>(); public Form1() { InitializeComponent(); FillList(); ShowInDataGridView(); } private void FillList() { DemoCustomer cust1 = new DemoCustomer("Egyptian", "Mycompany", "1"); DemoCustomer cust2 = new DemoCustomer("Lebanese", "Libcompany", "2"); DemoCustomer cust3 = new DemoCustomer("Palestinian", "Palcompany", "3"); list.Add(cust1); list.Add(cust2); list.Add(cust3); } private void ShowInDataGridView() { dataGridView1.DataSource = list; } } // This class implements a simple customer type
// that implements the IPropertyChange interface.
public class DemoCustomer : INotifyPropertyChanged
{ // These fields hold the values for the public properties.
private Guid idValue = Guid.NewGuid(); private string customerNameValue = String.Empty; private string companyNameValue = String.Empty; private string phoneNumberValue = String.Empty; public event PropertyChangedEventHandler PropertyChanged; private void NotifyPropertyChanged(String info) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(info)); } } // The constructor is private to enforce the factory pattern.
private DemoCustomer() { customerNameValue = "no data"; companyNameValue = "no data"; phoneNumberValue = "no data"; } public DemoCustomer(string customer, string company, string value) { customerNameValue = customer; companyNameValue = company; phoneNumberValue = value; } // This is the public factory method.
public static DemoCustomer CreateNewCustomer() { return new DemoCustomer(); } public string CustomerName { get { return this.customerNameValue; } set
{ if (value != this.customerNameValue) { customerNameValue = value; NotifyPropertyChanged("CustomerName"); } } } // This property represents an ID, suitable
// for use as a primary key in a database.
public Guid ID { get
{ return this.idValue; } }
public string CompanyName { get { return this.companyNameValue; } set
{ if (value != this.companyNameValue) { this.companyNameValue = value; NotifyPropertyChanged("CompanyName"); } } } public string PhoneNumber { get { return this.phoneNumberValue; } set
{ if (value != this.companyNameValue) { this.phoneNumberValue = value; NotifyPropertyChanged("PhoneNumber"); } } } } }
|
best regards | | Egyptian Friday, July 21, 2006 7:51 AM | thanks
it helped a lot, it worked fine for me | | penninha Monday, July 24, 2006 1:39 AM | Egyptian, Thanks for the post maybe you can set me straight here: Here is what I'm doing.
ArrayList subfile = new ArrayList(); //initialize substring arraylist
subfile.Add(file.Name.Substring(3,5)); //filling aray with substring of file name
dataGridView1.DataSource = subfile; //Attempting to bind it to a datagridview
This produces a "length" column with "5" as the value. What do I need to do to display the file.name.substring in the datagridview?
| | emiller101 Tuesday, July 25, 2006 2:59 PM | emmiller101: you may wish to refer to my original post containing the code which allows you to do this | | ahmedilyas Tuesday, July 25, 2006 3:07 PM | ahmedilyas,
Thanks for replying, I'm not sure sure I'm following your example correctly.
This line:
this.DataGridView.Rows(counter).SetValues(curObj);
Rows is a property of DataGridView but is being used like a method, I'm not sure how to modify this line to change the result.
Thanks, emiller101
| | emiller101 Tuesday, July 25, 2006 3:26 PM | Ah, I think I know...
change the (counter) to: [counter] | | ahmedilyas Tuesday, July 25, 2006 3:32 PM | ahmedilyas,
Thanks, thats working well. I am having an issue getting the obect to display in the new Column, maybe you can tell me what I'm doing wrong. Code looks like this:
this.dataGridView1.Columns.Add("RouteNumber", "Route Number"); //Add Column ArrayList subfile = new ArrayList(); //initialize substring array subfile.Add(file.Name.Substring(3,5)); //populate array int counter = 0; foreach (object curObj in subfile) { this.dataGridView1.Rows.Add(new DataGridViewRowCollection(this.dataGridView1)); this.dataGridView1.Rows[counter].SetValues(curObj); counter++; } //end foreach add rows dataGridView1.Visible = true;
When I walk through the code I get the correct number of objects and the datagridview draws the correct number of rows, just no text in the Route Number Column. How do I display "curObj" in the Route Number Column. Thanks, emiller101
| | emiller101 Tuesday, July 25, 2006 4:33 PM | Solved my own problem:
in: this.dataGridView1.Rows[counter].SetValues(curObj);
the .SetValues("Column1", "Column2") and so on.
| | emiller101 Tuesday, July 25, 2006 5:23 PM | believe it or not I was also thinking about this earlier today.
So, Am I correct in saying that you wish to add the items in a specific column?
if this is the case....
| |
int currentRow = 0; foreach (string curObj in theCollection) { this.theDataGridView.Rows.Add(); //add a new row this.theDataGridView.Rows[counter].Cells["MyColumnName"].Value = curObj; //set the current value in the foreach loop to the current row of the column specified currentRow++; }
|
hope it helps! | | ahmedilyas Tuesday, July 25, 2006 5:31 PM | Much cleaner than what I was doing thanks.
| | emiller101 Tuesday, July 25, 2006 7:01 PM | What do I do if I want to add the arrayList item fields (members) in a specific order?
Let's say my arrayList has items of type Client with members: "Nr", "Name", "Address", etc... and I want to have the specific columns "Nr", "Name", "Address" - in that order, with custom sizes (smaller "Nr" column, larger "Address").
In DataGrid I used TableStyles, but since I don't know how to export the DataGrid to excel, I'm trying to use a DataGridView as an intermediary. Perhaps you could help with customizing the DataGridView or with exporting the DataGrid?
Thanks | | dtma Thursday, April 12, 2007 7:47 AM | I found this code to quickly add the all the records to a datagridview control. It is fine for me because all I want is read only to view information and perhaps export. I am experimenting with viewing the application log from a server running SQL Server 2005. It may be the beginning of an application to monitor various metrics and information.
Cheers!!!
Here is the exact code that I used to view the application log from a remote machine. Obviously you will need to have the appropriate permissions but I am not going to get into that here.
EventLog alog = new EventLog();
EventLogEntryCollection eCollect;
ArrayList arr = new ArrayList();
alog.Log = "Application";
alog.MachineName = "SERVERNAME";
eCollect = alog.Entries;
alog.Close();
EventLogEntry[] EventEntryArray = new EventLogEntry[eCollect.Count];
eCollect.CopyTo(EventEntryArray, 0);
System.Collections. IEnumerator Enum=EventEntryArray.GetEnumerator();
while (Enum.MoveNext())
{
EventLogEntry EventEntry = (EventLogEntry)Enum.Current;
arr.Add(EventEntry.Message.ToString());
}
try
{
//add array to the datagrid
int CurrentRow = 0;
foreach (string curObj in arr)
{
this.dataGridView1.Rows.Add(new DataGridViewRowCollection(this.dataGridView1));
this.dataGridView1.Rows[CurrentRow].SetValues(curObj);
CurrentRow++;
}
}
catch (Exception j)
{
Console.WriteLine("Caught exception",j);
} | | state_dba Wednesday, November 28, 2007 2:38 PM | hey guyz, I'm a beginner in C# & i've been using this webpage to help me in binding my arraylist to datagridview but have been unsuccessful. Basically, I've created a single form with 2 tabs, one where I can add the items, & in the 2nd tab, I've created a datagridview with the SIX respective columns. Here's part of my code: StockItemArraylist.StoreItems = new ArrayList(); //Initialize arraylist StockClass s = new StockClass(); s.stockcode = txtStockCode.Text; s.itemname = txtItemName.Text; s.suppliername = txtSupplierName.Text; s.unitcost = Double.Parse(txtUnitCost.Text); s.numberrequired = System.Convert.ToInt32(txtNumberReq.Text); s.currstocklevel = System.Convert.ToInt32(txtCurrStocklvl.Text); StockItemArraylist.StoreItems.Add(s); dataGrdInventory.DataSource = StockItemArraylist.StoreItems; int counter = 0; foreach (object curObj in StockItemArraylist.StoreItems) { this.dataGrdInventory.Rows.Add(); //adds a new row this.dataGrdInventory.Rows[counter].SetValues("Column1", "Column2", "Column3", "Column4", "Column5", "Column6"); counter++; } //end foreach add rows dataGrdInventory.Visible = true; } No errors showed up after I typed in the code, but when I ran it & put in some values in the textboxes, I get the error "Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound". Any help would be appreciated.
| | demonfox33 Monday, October 12, 2009 6:45 PM |
|