如何禁用DataGridView
中空行的点击/光标?我交替行,一个是数据,另一个是空,然后是数据行,然后是空行。我想禁用单击空/空行。
我通过微风改进了我的代码,部分得到了我想要的但是这显然适用于每个细胞。如何实现此代码,以便ReadOnly Mode只对完全空行而不是那些甚至包含数据的单个单元格的行。
private void dataGridView3_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex < 0 || e.RowIndex < 0)
{
return;
}
var dataGridView = (sender as DataGridView);
try
{
int col = e.ColumnIndex;
int row = e.RowIndex;
var cell = ((DataGridView)sender)[col, row];
if (cell != null && cell.Value != "")
{
dataGridView.Cursor = Cursors.Hand;
}
else
{
dataGridView.Cursor = Cursors.No;
dataGridView.ReadOnly = true;
}
}
catch (Exception es)
{
MessageBox.Show(es.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
感谢您的帮助..
答案 0 :(得分:1)
诀窍是不仅检查对单元格的引用是否为空,而且还检查单元格的值是否为空字符串。为此,您需要展开
if(cell != null)
到
if(cell != null && cell.Value != "")
检查cell
是null
是否真的不是必需的,但它没有任何损害,并保护您免受进一步开发中可能发生的一些错误。
答案 1 :(得分:0)
这应该会让你到那里。这可能需要根据您的实例进行调整,您可能应该使用&#34;!=&#34;从结构中删除几行。
internal void AllRows_OnClick(object sender, EventArgs e){
int rowIndex = e.RowIndex;//Get Row that was clicked
DataGridViewRow row = dataGridView1.Rows[rowIndex];//update to Row
if (row.Cells[1].Value.ToString() == ""){
//Do Nothing
}else{
//Do Something
}
}