c# – 如何在datagridview中创建特定的列不可编辑?

使用DataGridView,如何在网格视图本身启用“允许编辑”时使特定列不可编辑?

另外,当DataGridView中的ComboBox中的选定索引发生更改时,如何执行事件?这里,ComboBox是一种列类型.

一个问题是,如何使标题标题与中心对齐?我找不到合适的房产.

解决方法

在这里有几个问题.

(1) How can I make a specific column uneditable in DataGridView?

在要进行不可编辑的特定列上设置ReadOnly标志.

dataGridView.Columns["YourColumnName"].ReadOnly = true;

(2) How can I execute an event when the selected index on a ComboBox in the DataGridView changes?

如果它在你的DataGridView中,它不是一个ComboBox;它是一个DataGridViewComboBoxColumn.根据MSDN

Unlike the ComboBox control,the DataGridViewComboBoxCell does not have Selectedindex and SelectedValue properties. Instead,selecting a value from a drop-down list sets the cell Value property.

这个我不熟悉,因为我自己从未尝试过.您似乎想要订阅EditingControlShowing事件,然后查看something like this是否适合您(稍微调整一下).

(3) How can I make the header title align in the center?

设置HeaderCell.Style.Alignment

dataGridView.Columns["YourColumnName"].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;

相关文章

目录简介使用JS互操作使用ClipLazor库创建项目使用方法简单测...
目录简介快速入门安装 NuGet 包实体类User数据库类DbFactory...
本文实现一个简单的配置类,原理比较简单,适用于一些小型项...
C#中Description特性主要用于枚举和属性,方法比较简单,记录...
[TOC] # 原理简介 本文参考[C#/WPF/WinForm/程序实现软件开机...
目录简介获取 HTML 文档解析 HTML 文档测试补充:使用 CSS 选...