问题
I have created an application in Visual Studio (C#) that makes use of a datagridview. Now, when I assign the DataSource of that datagridview, it automatically selects the first row, and executes my code for selection. Since I frequently reassign that datasource, this is not desireable. Is there any way to change it so it doesn't automatically make that first select, and only relies on the user's selections?
Thanks!
In response to the comment of Darshan Joshi: Apart from the auto-generated code, the only thing altered on the datagridview is setting AutoGenerateColumns to false, and setting the DataSource property. I've placed a MessageBox.Show in my selectionchanged delegate, and it seems it even gets called thrice every time the datasource is set. Once just before the data is loaded, and twice after.
I can't set selected to false on load, since the datasource is set after certain user actions, not on initialization.
回答1:
I had the same problem and here is my solution.
The tricky part was finding where to clear the selection... We can only clear the selection after the selection has been set by the DataGridView. At first the selection is only ready to be cleared in the Form.Load event, but subsiquent settings of the DataGridView.DataSource the selection is ready to be cleared straight after the DataSource assignment.
public class DataGridView_AutoSelectSuppressed : DataGridView
{
private bool SuppressAutoSelection { get; set; }
public DataGridView_AutoSelectSuppressed() : base()
{
SuppressAutoSelection = true;
}
public new /*shadowing*/ object DataSource
{
get
{
return base.DataSource;
}
set
{
SuppressAutoSelection = true;
Form parent = this.FindForm();
// Either the selection gets cleared on form load....
parent.Load -= parent_Load;
parent.Load += parent_Load;
base.DataSource = value;
// ...or it gets cleared straight after the DataSource is set
ClearSelectionAndResetSuppression();
}
}
protected override void OnSelectionChanged(EventArgs e)
{
if (SuppressAutoSelection)
return;
base.OnSelectionChanged(e);
}
private void ClearSelectionAndResetSuppression()
{
if (this.SelectedRows.Count > 0 || this.SelectedCells.Count > 0)
{
this.ClearSelection();
SuppressAutoSelection = false;
}
}
private void parent_Load(object sender, EventArgs e)
{
ClearSelectionAndResetSuppression();
}
}
Hope this helps.
回答2:
You should call: ClearSelection after event: DataBindingComplete
回答3:
you can deselect it in your form_load event like
private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.Rows[0].Selected = false;
}
回答4:
To load the grid without any selection, you can use this code snippet.
GridView.CurrentCell = null;
This will load it plain, without any selection. Add this after assigning data-source to the grid.
回答5:
Make sure your are NOT calling the method to load the data from the form constructor. If you call it from the Form.load()
also after the myDataGridView is loaded do this
myDataGridView.Rows[0].Selected = false;
回答6:
This worked for me:
Deregister the SelectionChanged event just before binding the data and then re-register the event. Maybe you should delete the registration of the event in the designer and register the event manually in your code.
myDGV.SelectionChanged -= new System.EventHandler(this.myDGV_SelectionChanged);
回答7:
Your experience may vary, but in my work I frequently fight this when the grid first loads, and although many solutions have been found to be acceptable (and far more are just ludicrous), they don't all work in every scenario. The one solution I find that works the most (for me, again your experience and scenarios may vary) is handling DataGridView.VisibleChanged:
public ThingWithGrid() {
Grid.VisibleChanged += Grid_VisibleChanged;
}
private void Grid_VisibleChanged(object sender, EventArgs e)
{
UpdateSelectedRows(InitialSelection);
Grid.VisibleChanged -= Grid_VisibleChanged;
}
回答8:
Just add SelectedItem="-1", this will do the trick
<DataGrid Name="dataGrid" SelectedItem="-1" />
来源:https://stackoverflow.com/questions/12494719/how-can-one-disable-the-first-autoselect-in-a-vs-datagridview