Hi Tony,
See answers inline
Hello!
I'm unsure when I can use a bound datagrid and when I can't.
What limitations has a bound datagrid?
1. For example if I want to manipulate the data in the datasource before
displaying the data in the datagrid can I then use a bound datagrid?
Maybe, although I am not sure how. Then again, my knowledge of the DataGrid is limited
2. For example if I only want to display some of the columns of a datasource
table can I then use a bound datagrid?
Yes
3. For example if I some of the columns in a datagrid should be readonly can
I then use a bound datagrid?
Yes
This is only some of the example.
So as a summary exactly what limitations does a bound datagrid have?
//Tony
If you can, I would recomment using the DataGridView instead of a DataGrid as it is much easier to configure and manipulate.
Using a DataGrid:
protected override void OnLoad(System.EventArgs e)
{
DataGrid grid = new DataGrid();
grid.Dock = DockStyle.Fill;
this.Controls.Add(grid);
DataTable table = new DataTable("Table");
table.Columns.Add("Id", typeof(Int32));
table.Columns.Add("Name", typeof(String));
table.Columns.Add("Hidden", typeof(String));
DataRow newRow = table.NewRow();
newRow["Id"] = 0;
newRow["Name"] = "Zero";
newRow["Hidden"] = "Don't display";
table.Rows.Add(newRow);
newRow = table.NewRow();
newRow["Id"] = 1;
newRow["Name"] = "One";
newRow["Hidden"] = "Don't display this either";
table.Rows.Add(newRow);
grid.DataSource = table;
// Styling a grid is done using Table- and ColumnStyle objects
DataGridTableStyle tableStyle = new DataGridTableStyle();
tableStyle.MappingName = "Table";
DataGridTextBoxColumn colStyle = new DataGridTextBoxColumn();
colStyle.MappingName = "Id";
colStyle.HeaderText = "Id";
colStyle.ReadOnly = true;
tableStyle.GridColumnStyles.Add(colStyle);
colStyle = new DataGridTextBoxColumn();
colStyle.MappingName = "Name";
colStyle.HeaderText = "Name";
tableStyle.GridColumnStyles.Add(colStyle);
grid.TableStyles.Add(tableStyle);
}
Using a DataGridView:
protected override void OnLoad(System.EventArgs e)
{
DataGridView grid = new DataGridView();
grid.Dock = DockStyle.Fill;
this.Controls.Add(grid);
DataTable table = new DataTable("Table");
table.Columns.Add("Id", typeof(Int32));
table.Columns.Add("Name", typeof(String));
table.Columns.Add("Hidden", typeof(String));
DataRow newRow = table.NewRow();
newRow["Id"] = 0;
newRow["Name"] = "Zero";
newRow["Hidden"] = "Don't display";
table.Rows.Add(newRow);
newRow = table.NewRow();
newRow["Id"] = 1;
newRow["Name"] = "One";
newRow["Hidden"] = "Don't display this either";
table.Rows.Add(newRow);
grid.DataSource = table;
grid.CellFormatting += new DataGridViewCellFormattingEventHandler(grid_CellFormatting);
grid.Columns["Id"].ReadOnly = true;
grid.Columns["Hidden"].Visible = false;
}
// each cell will call this method before drawn
void grid_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.Value != null && e.Value != DBNull.Value && e.ColumnIndex == 0)
{
if ((int)e.Value > 0)
{
e.CellStyle.BackColor = Color.Green;
e.Value = "+" + e.Value;
}
else
{
e.CellStyle.BackColor = Color.Red;
e.Value = "-" + e.Value;
}
}
}
Using a DataGridView, you can easily substitute the DataTable with a List<T>, something I could not get to work with the ColumnStyle objects on the DataGrid. Then again, this is quite possibly due to my lack of knowledge.