понедельник, 30 июля 2012 г.
C#/ DataGridView. Делаем некоторые ячейки в строке только для чтения.
Иногда возникает задача, сделать так, что бы некоторые ячейки в строке были только для чтения, ну или выделялись другим шрифтом и т.д.. Достигается это очень просто. Вот так собственно
//обьявляем ячейки
DataGridViewCheckBoxCell cell1 = new DataGridViewCheckBoxCell(false);
DataGridViewTextBoxCell cell2 = new DataGridViewTextBoxCell();
//поскольку запрещать редактировать мы будем чекбокс ему нужно задать дуфолтное значение для нула, иначе программа будет выдать ошибки
cell1.EditingCellFormattedValue = false;
//Обьявляем колонки
DataGridViewColumn col1 = new DataGridViewColumn(cell1);
col1.DefaultCellStyle.NullValue = false;
DataGridViewColumn col2 = new DataGridViewColumn(cell2);
//Добавляем их в грид
this.dataGridView1.Columns.Add(col1);
this.dataGridView1.Columns.Add(col2);
//Создаем строку, которую можно изменять
DataGridViewRow row = new DataGridViewRow();
row.Cells.AddRange(col1.CellTemplate, col2.CellTemplate); //или вот так row.CreateCells(dataGridView1);
row.Cells[0].ReadOnly = false;
row.Cells[1].Value = "ReadOnly = false";
//Отображаем в ячейке текст полужирным
row.Cells[1].Style.Font = new Font(Font, FontStyle.Bold);
this.dataGridView1.Rows.Add(row);
//Создаем строку, в которой первую ячейку изменять нельзя
cell1 = new DataGridViewCheckBoxCell();
cell2 = new DataGridViewTextBoxCell();
row = new DataGridViewRow();
row.Cells.AddRange(cell1, cell2);
//Говорим ячейке что она только для чтения
row.Cells[0].ReadOnly = true;
row.Cells[1].Value = "ReadOnly = true";
this.dataGridView1.Rows.Add(row);
//обьявляем ячейки
DataGridViewCheckBoxCell cell1 = new DataGridViewCheckBoxCell(false);
DataGridViewTextBoxCell cell2 = new DataGridViewTextBoxCell();
//поскольку запрещать редактировать мы будем чекбокс ему нужно задать дуфолтное значение для нула, иначе программа будет выдать ошибки
cell1.EditingCellFormattedValue = false;
//Обьявляем колонки
DataGridViewColumn col1 = new DataGridViewColumn(cell1);
col1.DefaultCellStyle.NullValue = false;
DataGridViewColumn col2 = new DataGridViewColumn(cell2);
//Добавляем их в грид
this.dataGridView1.Columns.Add(col1);
this.dataGridView1.Columns.Add(col2);
//Создаем строку, которую можно изменять
DataGridViewRow row = new DataGridViewRow();
row.Cells.AddRange(col1.CellTemplate, col2.CellTemplate); //или вот так row.CreateCells(dataGridView1);
row.Cells[0].ReadOnly = false;
row.Cells[1].Value = "ReadOnly = false";
//Отображаем в ячейке текст полужирным
row.Cells[1].Style.Font = new Font(Font, FontStyle.Bold);
this.dataGridView1.Rows.Add(row);
//Создаем строку, в которой первую ячейку изменять нельзя
cell1 = new DataGridViewCheckBoxCell();
cell2 = new DataGridViewTextBoxCell();
row = new DataGridViewRow();
row.Cells.AddRange(cell1, cell2);
//Говорим ячейке что она только для чтения
row.Cells[0].ReadOnly = true;
row.Cells[1].Value = "ReadOnly = true";
this.dataGridView1.Rows.Add(row);
Подписаться на:
Сообщения (Atom)
