一个dataGridView中,我想某些单元格不能选择,比如就像控件的Enabled属性设为false一样,单击它没有响应!
这个怎么实现?

解决方案 »

  1.   

    这个好像没有这样的属性.但可以设置为只读.
    DataGridView dgv = new DataGridView();
    dgv[0, 0].ReadOnly = true;  //单元格只读第1列第1行只读
    dgv.Columns[0].ReadOnly = true;  //第1列只读
      

  2.   

    你到这个网址看,DataGridView 三十六http://blog.csdn.net/fangxinggood/archive/2007/04/11/1561011.aspx#A1
      

  3.   


    设置每列的ReadOnly属性为true即可!
      

  4.   

    代码来自http://bingning.net/VB/SOURCE/datagridview/readonly.html
    根据条件单元格不能编辑//CellBeginEdit事件处理器
     private void DataGridView1_CellBeginEdit(object sender,
         DataGridViewCellCancelEventArgs e)
     {
         DataGridView dgv = (DataGridView)sender;
         //判断是否可以编辑
         if (dgv.Columns[e.ColumnIndex].Name == "Column1" &&
             !(bool)dgv["Column2", e.RowIndex].Value)
         {
             //编辑不能
             e.Cancel = true;
         }
     }(0, 0)的单元格只读//DataGridView1的(0, 0)的单元格只读
     DataGridView1[0, 0].ReadOnly = true;
      

  5.   

    这个好像没有这样的属性.但可以设置为只读. 
    DataGridView dgv = new DataGridView(); 
    dgv[i, i].ReadOnly = true;  //单元格只读第(i+1)列第(i+1)行只读 
    dgv.Columns[i].ReadOnly = true;  //第(i+1)列只读
      

  6.   

    编辑模板中 去掉textBox 换成 label
      

  7.   

    我是将字段换成模版
    在rowdatabind里面实现就可以了  Label l = e.Row.Cells[5].Controls[1] as Label;
      l.Enabled = false;
      

  8.   

    dataGridview有这个rowdatabind事件吗?
      

  9.   

     if (dgv.Columns[e.ColumnIndex].Name == "Column1" && !(bool)dgv["Column2", e.RowIndex].Value)
        {
            e.Cancel = true;
        }
    或 ReadOnly = true;
      

  10.   

    效果像是没反应,不过只是为了达到这样的效果,直接用可能不行,可能会有其它的没考虑的问题        //第二行,第二列,选中无反应效果
            private void Form_Load(object sender, EventArgs e)
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("a", typeof(string));
                dt.Columns.Add("b", typeof(string));            dt.Rows.Add(new object[] { "fad", "fdfsdf" });
                dt.Rows.Add(new object[] { "fad", "fdfsdf" });
                dt.Rows.Add(new object[] { "fad", "fdfsdf" });
                dt.AcceptChanges();
                this.dataGridView1.DataSource = dt;            this.dataGridView1[1, 1].ReadOnly = true;
            }        private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
            {
                if ((e.RowIndex == 1) && (e.ColumnIndex  == 1) &&
                    ((e.PaintParts & DataGridViewPaintParts.Background) == DataGridViewPaintParts.Background)
                   )
                {                SolidBrush bush = new SolidBrush(e.CellStyle.BackColor);
                    try
                    {
                        e.Graphics.FillRectangle(bush, e.CellBounds);
                    }
                    finally
                    {
                        bush.Dispose();
                    }
                    
                    DataGridViewPaintParts paintparts = e.PaintParts & (~DataGridViewPaintParts.Background);
                    paintparts = paintparts & (~DataGridViewPaintParts.Focus);
                    paintparts = paintparts & (~DataGridViewPaintParts.SelectionBackground);                e.Paint(e.CellBounds, paintparts);
                    TextRenderer.DrawText(e.Graphics, e.FormattedValue.ToString()   , e.CellStyle.Font, e.CellBounds, e.CellStyle.ForeColor, e.CellStyle.BackColor,TextFormatFlags.Left | TextFormatFlags.VerticalCenter);
                   
                    e.Handled = true;
                }
            }