datagridview - don't want any rows selected initially....

  • Thread starter Thread starter Oli
  • Start date Start date
O

Oli

hi all - try as I might I can't get my datagridview to *not* select
(and hence highlight) a row...

....any help appreciated,
cheers,
O.
 
Code like this in Form.Load avoids a selected current cell for me.

this.dataGridView1.CurrentCell = null;

===================
Clay Burch
Syncfusion, Inc.
 
I think in order for the null CurrentCell to work, the grid cannot be
the active control on the form when the form first appears. If the
form with the grid has other controls on it, you could set the
taborder such that the grid has a higher tabindex property than one of
the other controls on the form. Or, you could explicilty set the
form's activecontrol to some other control on the form. If all I do is
drop a DataGridView and a button on a form, add a form.Load event with
the code below, then the form displays with no row marked in the grid.
If I comment out the CurrentCell = null line, then cell 0,0 is
selected when the form appears. How does your use case differ?

private void Form1_Load(object sender, EventArgs e)
{
#region Get the DataSource
DataTable dt = new DataTable("MyTable");
int nCols = 4;
int nRows = 20;
Random r = new Random(123345345);
for (int i = 0; i < nCols; i++)
dt.Columns.Add(new DataColumn(string.Format("Col{0}",
i), typeof(int)));
for (int i = 0; i < nRows; ++i)
{
DataRow dr = dt.NewRow();
for (int j = 0; j < nCols; j++)
dr[j] = r.Next(10);
dt.Rows.Add(dr);
}
#endregion

grid.DataSource = dt; //grid is a DataGridView
this.ActiveControl = this.button1;
grid.CurrentCell = null;
}
=================
Clay Burch
Syncfusion, Inc.
 
Back
Top