【问题标题】:DataGridView: FullRowSelect and FocusDataGridView:FullRowSelect 和焦点
【发布时间】:2009-04-30 08:35:30
【问题描述】:

我在模态窗口中有一个 DataGridView,其中包含我的程序的选项列表。网格有两个列。第一个包含用于选择该选项的复选框,第二个是该选项的名称/描述。 winform 还包含确定和取消按钮,但这不是重点。下面的代码做了我想要的。由于 FullRowSelect 属性,复选框被选中/取消选中,您单击该行内的任意位置。但是,它不再在当前行周围显示蓝色背景或虚线。我怎样才能在不丢失任何当前功能的情况下添加它?

编辑: 详细说明;我想要的是再次启用所选行/单元格上的虚线和/或蓝色背景。看起来我目前的代码以某种方式禁用了这个......

当前相关代码:

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;
}

【问题讨论】:

    标签: c# visual-studio winforms datagridview focus


    【解决方案1】:

    我会尝试使用.OnCellClick 方法并将行颜色设置为蓝色。我认为您也应该能够添加虚线边框。

    我相信你可以这样称呼它:

    optionsBannersDataGridView.OnCellClick += new DataGridViewCellEventArgs(optionsBannersDataGridView_OnCellClick);
    

    【讨论】:

    • 这会确保在加载时顶行是蓝色的吗?
    • 我不仅希望它发生在 onCellClick 上。当前行应始终被勾勒/着色。这就是我想要做的......
    • 我认为您希望被选中的每一行都具有蓝色背景是否正确?
    【解决方案2】:

    我最终做的是删除上面提到的大部分代码,因为它实际上并没有做太多。出于某种原因,当我在 Visual Studio 中设置属性时它不起作用,但现在它起作用了。我不知道那里发生了什么,但这不是重点。

    构造函数现在看起来像这样:

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

    这些属性是在 Visual Studio GUI 中设置的。 SelectionMode 设置为 FullRowSelect,MultiSelect 设置为 false。

    我仍然没有得到我想要的焦点,所以我在 Visual Studio 中将所选行的背景色设置为蓝色,将前景色设置为白色。现在可以按我的意愿工作了。

    我仍然不知道为什么之前没有正确设置属性,但至少它现在可以工作了:P

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多