C# 如何在DataGridView中禁用单击空行

C# 如何在DataGridView中禁用单击空行,c#,winforms,datagridview,C#,Winforms,Datagridview,如何在DataGridView中的空行上禁用单击/光标?我有交替的行,一个有数据,另一个空,然后是有数据的行,然后是空行。我只想禁用单击空/空行 多亏了breeze,我改进了我的代码,部分实现了我想要的功能,但这显然适用于每个单元。如何实现这段代码,使只读模式仅适用于完全空的行,而不适用于包含数据的单个单元格的行 private void dataGridView3_CellMouseEnter(object sender, DataGridViewCellEventArgs e) {

如何在
DataGridView
中的空行上禁用单击/光标?我有交替的行,一个有数据,另一个空,然后是有数据的行,然后是空行。我只想禁用单击空/空行

多亏了breeze,我改进了我的代码,部分实现了我想要的功能,但这显然适用于每个单元。如何实现这段代码,使只读模式仅适用于完全空的行,而不适用于包含数据的单个单元格的行

 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);
        }

    }
private void dataGridView3\u CellMouseEnter(对象发送方,DataGridViewCellEventArgs e)
{
如果(e.ColumnIndex<0 | | e.RowIndex<0)
{
返回;
}
var dataGridView=(发送方为dataGridView);
尝试
{
int col=e.ColumnIndex;
int row=e.RowIndex;
变量单元格=((DataGridView)发送方)[列,行];
if(cell!=null&&cell.Value!=“”)
{
dataGridView.Cursor=Cursors.Hand;
}
其他的
{
dataGridView.Cursor=Cursors.No;
dataGridView.ReadOnly=true;
}
}
捕获(异常es)
{
MessageBox.Show(es.Message,“,MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}

谢谢你的帮助。

这会让你达到目的。这可能需要针对您的实例进行调整,您可能应该使用“!=”从结构中删除几行

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
    }

}

诀窍是不仅要检查对单元格的引用是否为空,还要检查单元格的值是否为空字符串。要做到这一点,您需要扩展

if(cell != null) 


检查
cell
是否为
null
并不是真正必要的,但它不会对您造成伤害,并且可以防止您在进一步开发过程中可能出现的一些错误。

强制性“您尝试了什么?”请发布您的代码。强制性“您想要实现什么?”:您不能阻止用户单击,您希望在您的程序中实现什么不同?它如何处理单击?我假设您希望在发送方为空行时基本上“忽略”单击事件?@Breeze单击单元格会打开另一个窗体,该窗体将加载单元格的数据,单击空单元格时,新表单当然也是空的,没有任何用途。扩展
单元格!=空
单元格!=null&&cell.value!=“
解决您的问题?什么是“DataGridViewRow=dataGridView1.Rows[rowIndex];”好用?为什么只检查行[1]。单元格[1]?@Nebr DataGridViewRow直接指向行,我应该将其添加到语句中,我现在就编辑它。谢谢,我得到了它
if(cell != null && cell.Value != "")