Binding an ArrayList to a DataGrid
I need to populate a DataGrid from a CSV or FlatFile. I stumbbled upon George Sheperd’s Blog in binding an arraylist to a DataGrid instead.
public void CreateArrayList(Building[] res) { arrayList = new ArrayList(); foreach (Building mybldg in res) { arrayList.Add(mybldg); } } public void BindArrayListToDataGrid() { dataGrid.DataSource = arrayList; //create a custom tablestyle and add five columnstyles DataGridTableStyle ts = new DataGridTableStyle(); ts.MappingName = "ArrayList"; AddTextBoxColumnToGrid(ts, "bldg_id", "id", 50); AddTextBoxColumnToGrid(ts, "CN_bldg_name", "Name (CN)", 50); AddTextBoxColumnToGrid(ts, "EN_bldg_name", "Name (EN)", 50); AddTextBoxColumnToGrid(ts, "CN_address", "Address (EN)", 50); AddTextBoxColumnToGrid(ts, "EN_address", "Address (CN)", 50); dataGrid.TableStyles.Clear(); dataGrid.TableStyles.Add(ts); } private void AddTextBoxColumnToGrid(DataGridTableStyle tableStyle, String strMappingName, String strHeaderText, int width) { DataGridTextBoxColumn cs = new DataGridTextBoxColumn(); cs.MappingName = strMappingName; //public property name cs.HeaderText = strHeaderText; cs.Width = width; tableStyle.GridColumnStyles.Add(cs); } |