简体   繁体   中英

DataGridView Default Row/Cell select issue

DataGridView set to Full Row Select. Need a way to clear the default selected row thats done by the default nature of DataGridView

I am also using SelectionChanged event That dgv has 4 columns

If i leave the default row selection, SelectionChanged fires 4 times when its loaded which I don't want it to fire at all.

I have tried using the RowsPostPaint event which clears the selection and doesn't fire the SelectionChanged event but i'm unable to select any rows after.

Any ideas? Thanks

use this

in form loading:

     datagridview.DataBindingComplete += new DataGridViewBindingCompleteEventHandler(datagridview_DataBindingComplete);

and for the listener:

    private void datagridview_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
    {
        datagridview.ClearSelection();
    }

bute note that anytime you change datasource your default selection hides.

After populating datagrid,try this one.

datagrid.ClearSelection();

To de-select the initial row selection, you can enumerate the SelectedRows and set the Selected property to false . To prevent the SelectionChanged event from firing when the initial row is selected, add the event manually, after you de-select it.

For example, in the form's Load event:

foreach(DataGridViewRow row in dataGridView1.SelectedRows)
    row.Selected = false;

dataGridView1.SelectionChanged += dataGridView1_SelectionChanged;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM