我在我的 C# Windows 窗体项目中,我有
DataGridView
6 列。但是其中一栏内的文本信息长度很长,有时达到5或6行。
如果行数很高,超过了单元格的高度,如何只为该列的单元格放置滚动条?
比如这里的截图,我写了7行,滚动条应该放在这个cell里
我应该覆盖 gridview 吗?如果是 - 我该怎么做?
我没有找到任何解决方案。请帮助
DataGridView 在视图模式下不使用单元格控件,因此它不支持视图模式下单元格的滚动条。
自定义油漆也无济于事;最接近您正在寻找的是为控件启用 EditOnEnter 编辑模式,并在进入编辑模式时显示带有垂直滚动条的多行编辑器。
示例 - 带垂直滚动条的多行单元格
//Increase the height or row, to show 3 rows of data by default
dataGridView1.RowTemplate.Height *= 3;
//Load data
var dt = new DataTable();
dt.Columns.Add("عنوان");
dt.Columns.Add("دسته بندی ها");
dt.Rows.Add("دسته بندی یک",
"زیر مجموعه یک" + Environment.NewLine +
"زیر مجموعه دو" + Environment.NewLine +
"زیر مجموعه سه" + Environment.NewLine +
"زیر مجموعه چهار" + Environment.NewLine +
"زیر مجموعه پنج");
dataGridView1.DataSource = dt;
//Goes to edit mode on enter to show scrollbar once you click on the cell
dataGridView1.EditMode = DataGridViewEditMode.EditOnEnter;
//Increase the width of column to open some space for vertical scrollbar
dataGridView1.Columns[1].Width += SystemInformation.VerticalScrollBarWidth;
//Support multiline data for the cell
dataGridView1.Columns[1].DefaultCellStyle.WrapMode = DataGridViewTriState.True;
//If you want to keep the data read-only
dataGridView1.Columns[0].ReadOnly = true;
dataGridView1.AllowUserToAddRows = false;
dataGridView1.AllowUserToDeleteRows = false;
//Support multiline text and vertical scrrollbar
dataGridView1.EditingControlShowing += (obj, args) =>
{
if (dataGridView1.CurrentCell.ColumnIndex == 1)
{
var txt = (DataGridViewTextBoxEditingControl)args.Control;
txt.Multiline = true;
txt.WordWrap = false;
txt.ScrollBars = ScrollBars.Vertical;
//If you want to keep the data read-only
txt.ReadOnly = true;
}
};