Home Forums WinForms controls Xceed Grid for WinForms Dynamically changing grid Reply To: Dynamically changing grid

User (Old forums)
Member
Post count: 23064

It is better to add rows at form_load, this will generate data rows instead of value rows, which is more what you want. In fact, you can set up your grid as you want at form_load :

private void Form1_Load(object sender, System.EventArgs e)
{
gridControl1.ReadOnly = true;
gridControl1.SelectionMode = SelectionMode.None;
groupByRow1.Visible = false;
columnManagerRow1.Visible = false;
dataRowTemplate1.RowSelector.Visible = false;
gridControl1.RowSelectorPane.Visible = false;

Random random = new Random();
for( int i = 0; i < 9; i++ )
{
Xceed.Grid.DataRow row = gridControl1.DataRows.AddNew();
}
}

Then in an update event, you can access each cell and do whatever you need on it :

//clear all cells, keeping the grid structure
private void button1_Click(object sender, System.EventArgs e)
{
for( int i = 0; i < 9; i++ )
{
Xceed.Grid.DataRow row = gridControl1.DataRows[ i ];
for( int j = 0; j < 9; j++)
{
row.Cells[ j ].Value = string.Empty;
row.Cells[ j ].ResetBackColor();
}
}
}

//fill all cells
private void button2_Click(object sender, System.EventArgs e)
{
Random random = new Random();
for( int i = 0; i < 9; i++ )
{
Xceed.Grid.DataRow row = gridControl1.DataRows[ i ];
for( int j = 0; j < 9; j++)
{
row.Cells[ j ].Value = random.Next( 1000 ).ToString();
row.Cells[ j ].BackColor = Color.FromArgb( random.Next(255), random.Next(255), random.Next(255) );
}
}
}

Imported from legacy forums. Posted by André (had 3175 views)