In my WinForms I have DataGridView
. I wanted to select full row at once so I set SelectionMode
as FullRowSelect
. And now I have problem, b
Try setting DataGridView.AllowUserToAddRows = false
in constructor after InitializeComponent()
.
"Shown" event that run after frame first displayed worked for me:
private void frmMain_Shown(object sender, EventArgs e)
{
dataGridView1.ClearSelection();
}
Unfortunately none of these answers helped me, but I found other solution. Instead of unable selection I will just hide it with this piece of code:
dataGridView1.DefaultCellStyle.SelectionBackColor = dataGridView1.DefaultCellStyle.BackColor;
dataGridView1.DefaultCellStyle.SelectionForeColor = dataGridView1.DefaultCellStyle.ForeColor;
So if anyone just wants to hide the selection it's gonna work pretty well.
Cheers :)
The event to set for disabled selected row at start is this, and manage a FLAG to stop the ClearSelection
private void dataGridView_SelectionChanged(object sender, EventArgs e)
{
if (FLAG==true)
{
dataGridView.ClearSelection();
FLAG=false;
}
}
If this is because it raised unwanted GridView1_SelectionChanged event on initial loading, you can use a flag to handle this
public partial class YourFormName
{
private bool IsReady= false;
private void YourFormName_Load(object sender, EventArgs e)
{
//Load your GridView1...
//Format your GridView1...
IsReady = true;
}
void GridView1_SelectionChanged(object sender, EventArgs e)
{
if (!IsReady)
return;
//do the rest of the stuffs
}
}
This works for me:
private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
dataGridView1.Rows[0].Selected = false;
}