能不能让鼠标无法点击dataGridView1第一列为空的行,即:让所有第一列为空的行都无法用鼠标点击获取焦点?

解决方案 »

  1.   


    //如果希望DataGridView 内某个单元格不可编辑//设置 DataGridView1 的第2列整列单元格为只读 
    DataGridView1.Columns[1].ReadOnly = true;
    // 设置 DataGridView1 的第3行整行单元格为只读 
    DataGridView1.Rows[2].ReadOnly = true; 
    不知道是不是这个意思
      

  2.   

    简单的用GridView的RowDataBound事件
     protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int i = e.Row.RowIndex;
                string str = GridView1.Rows[i].Cells[0].Text;//获取要判断的列
                if (string.IsNullOrEmpty(str))
                {
                    GridView1.Rows[i].Enabled = false;
                }
            }
        }
      

  3.   

    c/s控件?好像也有类似RowDataBound事件吧。不太熟悉!
      

  4.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;namespace WinApp {
        public partial class frmDatagridviewSample : Form {
            public frmDatagridviewSample() {
                InitializeComponent();
                InitData();
            }        private void InitData() {
                System.Data.DataTable dt = new DataTable();
                dt.Columns.Add("id");
                dt.Columns.Add("name");
                dt.Rows.Add(new object[] { 65, "a" });
                dt.Rows.Add(new object[] { 66, "b" });
                dt.Rows.Add(new object[] { 67, "c" });
                dt.Rows.Add(new object[] { null, "c" });            dataGridView1.DataSource = dt;
                dataGridView1.DataBindingComplete += new DataGridViewBindingCompleteEventHandler(dataGridView1_DataBindingComplete);
                dataGridView1.Columns[0].ReadOnly = false;
            }        void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e) {
                foreach (DataGridViewRow dgvr in dataGridView1.Rows) {
                    DataRowView r = dgvr.DataBoundItem as DataRowView;
                    if (r != null && r["id"] == DBNull.Value) {
                        dgvr.Cells[0].ReadOnly = true;
                    }
                }
            }
        }
    }
      

  5.   

    void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e) {
                foreach (DataGridViewRow dgvr in dataGridView1.Rows) {
                    DataRowView r = dgvr.DataBoundItem as DataRowView;
                    if (r != null && r["id"] == DBNull.Value) {
                        dgvr.Cells[0].ReadOnly = true;
                        //dgvr.ReadOnly = true;
                    }
                }
            }
      

  6.   

    搞反了
    void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e) {
                foreach (DataGridViewRow dgvr in dataGridView1.Rows) {
                    DataRowView r = dgvr.DataBoundItem as DataRowView;
                    if (r != null && r["id"] == DBNull.Value) {
                        //dgvr.Cells[0].ReadOnly = true;
                        dgvr.ReadOnly = true;
                    }
                }
            }
      

  7.   

    那设置单元格的Enable属性吧!是不可以获得焦点的,但是我认为只读后,获得焦点也没有用吗!
      

  8.   

    To gudujianxiao:
    我用的是dataGridView1,没有你那个事件;To chengfellow:
    你的方法是否只是设置行的只读属性,并未控制焦点的获取!
      

  9.   


    你既然键盘按键实现了  不会鼠标?CellClick 事件把你的代码放进去就行了,,你要的是屏蔽焦点  不是只读?
    屏蔽焦点还真不清楚咋做,,有时间可以研究下只读的上面说的很清楚了
      

  10.   

    键盘按键就几个:UP,DOWN,ENTER,在行焦点移动时可以判断,但鼠标不一样,可以在任意地方点击,没法判断和控制。dataGridView行可能都是只读的情况,但当第一列不为空时,可以获得焦点。
      

  11.   

    申明一个静态变量num=0  在行绑定事件中判断第一列是否为空并且判断num是否为0,为空并且0==num则将该行设置为只读,并且在每次绑定之前将num归0,这样应该就可以实现你要的效果了
      

  12.   

    如果没有说错的话,行在只读的情况下,同样行是可以被点中并获得焦点的.如果将整个控件的
    Enabled = false,就可以让所有行都无法选中,但单独的行没有这个属性!!!
      

  13.   

    可以不绘制指定单元格的焦点框,
    RowPrePaint事件
    e.PaintParts &= ~DataGridViewPaintParts.Focus;
      

  14.   

    我的WinForms程序,提示:
    错误 1 “System.Windows.Forms.DataGridViewRowPostPaintEventArgs”不包含“PaintParts”的定义,并且找不到可接受类型为“System.Windows.Forms.DataGridViewRowPostPaintEventArgs”的第一个参数的扩展方法“PaintParts”(是否缺少 using 指令或程序集引用?)
      

  15.   

    http://msdn.microsoft.com/zh-cn/library/system.windows.forms.datagridviewrowprepainteventargs_members(v=VS.80).aspx有这个属性啊。不过这个方法 单元格还是能选中, 
    只是要重新设置选择状态的背景色和当前色,不显示焦点框,并且单元格为只读。这样效果就和不能选中一样了
      

  16.   

    msdn上面的代码
    // Paints the custom selection background for selected rows.
    void dataGridView1_RowPrePaint(object sender,
            DataGridViewRowPrePaintEventArgs e)
    {
        // Do not automatically paint the focus rectangle.
        e.PaintParts &= ~DataGridViewPaintParts.Focus;    // Determine whether the cell should be painted
        // with the custom selection background.
        if ((e.State & DataGridViewElementStates.Selected) ==
                    DataGridViewElementStates.Selected)
        {
            // Calculate the bounds of the row.
            Rectangle rowBounds = new Rectangle(
                this.dataGridView1.RowHeadersWidth, e.RowBounds.Top,
                this.dataGridView1.Columns.GetColumnsWidth(
                    DataGridViewElementStates.Visible) -
                this.dataGridView1.HorizontalScrollingOffset + 1,
                e.RowBounds.Height);        // Paint the custom selection background.
            using (Brush backbrush =
                new System.Drawing.Drawing2D.LinearGradientBrush(rowBounds,
                    this.dataGridView1.DefaultCellStyle.SelectionBackColor,
                    e.InheritedRowStyle.ForeColor,
                    System.Drawing.Drawing2D.LinearGradientMode.Horizontal))
            {
                e.Graphics.FillRectangle(backbrush, rowBounds);
            }
        }
    }