//Increase the height of row, to show more lines by default
dataGridView1.RowTemplate.Height *= 2;
//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;
}
};
1条答案
按热度按时间crcmnpdw1#
DataGridView在视图模式下不对单元格使用控件,因此它不支持视图模式下单元格的滚动条。自定义油漆也无济于事。最接近你正在寻找的东西是:在编辑模式下启用单元格的多行可滚动编辑器,并将编辑模式设置为EditOnEnter,这样当您单击单元格时,它会显示滚动条。如果需要,您还可以轻松地将数据保持为只读。
示例-带垂直滚动条的多行单元格
在具有名为dataGridView 1的DataGridView的窗体的Load事件处理程序中使用以下代码,以查看我在屏幕截图中显示的操作: