views:

1208

answers:

2

I've got a DataGridView in a modal window with a list of options for my program. The grid has two collumns. The first one contains a checkbox for selecting that option, the seccond is the name/description of that option. The winform also contains OK and cancel buttons but that's beside the point. The code below does what I want it to. Because of the FullRowSelect property the checkbox is checked/unchecked in you click anywhere withint that row. It does however not show a blue background or a dotted line around the current row anymore. How would I be able to add this without loosing any of the current functionality?

EDIT: To elaborate; what I want is to once again enable the dotted line and/or blue background on the selected row/cells. It looks like the code I have currently somehow disables this...

Relevant current code:

public OptionsForm()
{
    InitializeComponent();
    OptionsRoot = Options.GetReadOnlyRoot(OptionsBannersNameValueList.GetNameValueList(Settings.Default.OptionsBanners));
    optionsBannersDataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
    optionsBannersDataGridView.MultiSelect = false;
    optionsBannersDataGridView.RowPrePaint += new DataGridViewRowPrePaintEventHandler(optionsBannersDataGridView_RowPrePaint);
    InitUI();
    Closing += MyFormClosing;
    BindingSourceTree = BindingSourceHelper.InitializeBindingSourceTree(components, rootBindingSource);
}

private void optionsBannersDataGridView_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
{
    e.PaintParts &= ~DataGridViewPaintParts.Focus;
}
+2  A: 

I would try using the .OnCellClick method and set the row color to blue. I think you should be able to add a dotted border as well.

I believe you can call it like this:

optionsBannersDataGridView.OnCellClick += new DataGridViewCellEventArgs(optionsBannersDataGridView_OnCellClick);
Jared Harley
Would this ensure that the top row is blue at load time though?
Sakkle
I don't only want it to happen onCellClick though. The current row whould be outlined/colored at all times. This is what I'm trying to do...
Sakkle
Am I right in thinking that you want each row that is checked to have a blue background?
Jared Harley
A: 

What I finally ened up doing was remove most of the code mentioned above, as it really didn't do much. for some reason when I set the properties in visual studio it did not work, but now it does. I don't know what happened there but thats beside the point.

The constructor now looks like this:

public OptionsForm()
    {
        InitializeComponent();
        AlternativerRoot = Alternativer.GetReadOnlyRoot(AlternativerFanerNameValueList.GetNameValueList(Settings.Default.AlternativerFaner));
        InitUI();
        Closing += MyFormClosing;
        _bindingSourceTree = BindingSourceHelper.InitializeBindingSourceTree(components, rootBindingSource);
    }

The properties are set in the visual studio GUI instead. SelectionMode is set to FullRowSelect and MultiSelect is set to false.

I still didn't get the focus I wanted so I set the backcolor of of the selected row to blue and the forecolor to white in visual studio. This now works like I wanted it to.

I still don't know why the properties were not getting set properly earlier, but at least it works now :P

Sakkle