DataGridView .Net 3.5中的只读单元格 [英] Read-Only cell in DataGridView .Net 3.5
问题描述
我在将DataGridView的特定单元格的属性设置为只读时遇到问题。我将DataGridView dgv 绑定到DataSet(从存储过程检索的DataSet ),然后设置dgv列的属性,例如:颜色,字体,allignment等,它工作正常。现在我想要一些特定的dgv单元格是只读的,但它不起作用。我已经尝试了很多东西,例如
dgv.Item(0,1).ReadOnly = True
dgv.Rows(0).Cells( 1).ReadOnly = True
我们将非常感谢任何帮助。
简单,
使用gridview的rowdatabound事件
并写上
if(e.Row.RowType == DataControlRowType.DataRow)
{
e.row.cells [6]。 enabled = false;
}: - O
我在这里有一个类似的问题,但我必须尝试一个想法。
在单元格中进行条件检查开始编辑,如果为true,则将您的单元格设置为只读,然后离开CellBeginEdit事件。
< pre lang =cs> if(Set_some_cells_readonly&&(e.ColumnIndex == 0
|| e.ColumnIndex == 1
|| e.ColumnIndex == 2))
{
//必须设置只读单元格,否则编辑继续正常
//即使使用e.cancel - 所有这一切都会终止开始进程
dgv.Rows [e .RowIndex] .Cells [0] .ReadOnly = true;
dgv.Rows [e.RowIndex] .Cells [1] .ReadOnly = true;
dgv.Rows [e.RowIndex] .Cells [2] .ReadOnly = true;
dgv.CurrentCell.Selected = false;
//结束编辑是不够的,我们必须离开单元格否则
//它在本回合中保持写入模式,并且在此编辑事件结束后仅变为只读
// 。
dgv.CurrentCell = nullptr;
返回; //以防其他任何代码'以后'滑入'
}
其他
{
dgv.Rows [e.RowIndex] .Cells [0] .ReadOnly = false;
dgv.Rows [e.RowIndex] .Cells [1] .ReadOnly = false;
dgv.Rows [e.RowIndex] .Cells [2] .ReadOnly = false;
}
抱歉,我无法找到DataGridView的任何RowDataBound事件或类似的东西这一点。
Hi, I am having a problem with setting the property of a particular cell of DataGridView to read-only. I am binding a DataGridView "dgv" to a DataSet (DataSet retrieved from a stored procedure) and then setting the properties of dgv columns for e.g. color, font, allignment etc and it is working fine. Now I want some particular cells of dgv to be read-only, but it is not working. I've tried a lot of things for e.g.
dgv.Item(0, 1).ReadOnly = True
OR
dgv.Rows(0).Cells(1).ReadOnly = True
Any help will be greatly appreciated.
Simply,
use rowdatabound event of gridview
and write
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.row.cells[6].enabled=false;
} :-O
I have a similar question up here, but a thought has struck me which I must try.
Putting a conditional check in the cell begin edit, and if true, set your cells readonly then leave the CellBeginEdit event.
<pre lang="cs">if(Set_some_cells_readonly && (e.ColumnIndex == 0 || e.ColumnIndex == 1 || e.ColumnIndex == 2)) { // The cells must be set readonly otherwise edit proceeds as normal // even with an e.cancel - all that does is kill the begin process dgv.Rows[e.RowIndex].Cells[0].ReadOnly = true; dgv.Rows[e.RowIndex].Cells[1].ReadOnly = true; dgv.Rows[e.RowIndex].Cells[2].ReadOnly = true; dgv.CurrentCell.Selected = false; // It is not enough to end the edit, we must leave the cell otherwise // it remains in write mode for this turn and only becomes readonly // after this edit event ends. dgv.CurrentCell = nullptr; return; // just in case any other code 'slips in' at a later date } else { dgv.Rows[e.RowIndex].Cells[0].ReadOnly = false; dgv.Rows[e.RowIndex].Cells[1].ReadOnly = false; dgv.Rows[e.RowIndex].Cells[2].ReadOnly = false; }
Sorry, I am unable to find any RowDataBound Event of DataGridView or something like that.
这篇关于DataGridView .Net 3.5中的只读单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!