如何隐藏datagridview中没有数据那行前面的checkbox复选框?
就是说比如datagridview里面只显示了一行数据,为了美观期间我插入了一些空行。怎么让那些空行前面的checkbox复选框隐藏或者去掉?
上回我问过这个问题,以为解决了,后来发现里面提供的方法findcontrol是属于System.Web.UI.WebControls 命名空间里的checkboxlist类里的,貌似不能引用。
哪位打个还有什么办法啊?

解决方案 »

  1.   

    可以在CellPaint里自己来绘制CheckBox,如果有数据就绘制,否则不绘制。
    可以参考如下两个绘制的方法:
    CheckBoxRenderer.DrawCheckBox;
    ControlPaint.DrawCheckBox
      

  2.   

    引用不到findcontrol啊
    其实主要的问题是我不能获取某一行的checkbox..
      

  3.   

    ((CheckBox)findcontrol(“”)).Visible = false;
    我用的GridView就可以。
      

  4.   

    不清楚webform是可以引用 System.Web.UI.WebControls 命名空间的吧
    我试了 根本及不认识findcontrol
      

  5.   

    在OnCellPainting处理中加上就可以了。DataGridViewCellPaintingEventArgs中包含ColumnIndex ,RowIndex 属性
    DataGridView.Rows[RowIndex ].Cells[ColumnIndex]就可以设置相应位置的控件的属性.
      

  6.   

    可以在CellPaint里自己来绘制CheckBox,如果有数据就绘制,否则不绘制。 
    可以参考如下两个绘制的方法: 
    CheckBoxRenderer.DrawCheckBox; 
    ControlPaint.DrawCheckBox
    ==================
    对`
    就是写个方法`没什么麻烦的`
      

  7.   

    找到对应的单元格好像不难啊,
    datagridview1.rows[i].cells[0]      
    不过好像不能设置是否显示
      

  8.   

    首先,需要明确的是在winform当中的datagridview控件是没有findcontrol方法的.所以我不能通过webform的方法来找到我们想要的控件.LZ可以尝试用以下的方法实现查找相应的控件.
    dataGridView1[0, dataGridView1.CurrentRow.Index].Value = true;0为CheckBox所在的IndexdataGridView1.CurrentRow.Index=現在所在的RowIndex
    参数根据实际的需求进行修改
      

  9.   

    dataGridView1[0,   dataGridView1.CurrentRow.Index].Value?
    vlaue是用来指明是否被选中的,就是是不是打了勾的。隐藏的Visible说是只读的不能改变.
      

  10.   

    可以参考下面的一段代码来解决:
    private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) 

    if (e.ColumnIndex == 1) 
    {
    e.Handled = true; 
    if (e.Value != null) 
    { ControlPaint.DrawCheckBox(e.Graphics, e.CellBounds, ButtonState.Checked); 

    }
    }
      

  11.   

    呵呵。我以前说的那个好像实现不了。那是我用GridView时候用的,前两天比较忙,没来回贴哦。
    现在已经实现了。我把部分代码贴出来。可能有点麻烦。没好好做。效果不是很好看哦。兄弟自己试试,完善一下了。
    新建的类
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Windows.Forms;
    using System.Drawing;namespace WindowsControlLibrary1
    {
        public class MyDataViewCheckBoxCell:DataGridViewCheckBoxCell
        {
            private bool showCell = true;
            protected override void Paint( Graphics graphics,
                                    Rectangle clipBounds,
                                    Rectangle cellBounds,
                                    int rowIndex,
                                    DataGridViewElementStates elementState,
                                    Object value,
                                    Object formattedValue,
                                    string errorText,
                                    DataGridViewCellStyle cellStyle,
                                    DataGridViewAdvancedBorderStyle advancedBorderStyle,
                                    DataGridViewPaintParts paintParts) 
            {            if (showCell)
                {
                    //if (showCell) 
                    //{
                    base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
                }
                //}
                else
                {            }
            }        public bool ShowCheckBox 
            {
                get
                {
                    return showCell;
                }
                set
                {
                    showCell = value;
                }
            }    }
    }
    在form的Load事件和DataGridView的CellPainting事件里作如下处理
            private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
            {
                DataGridView gdv = sender as DataGridView;
                if (e.RowIndex > 0)
                {
                    if (e.RowIndex == gdv.Rows.Count - 1)
                    {
                        MyDataViewCheckBoxCell cell = gdv.Rows[e.RowIndex].Cells[0] as MyDataViewCheckBoxCell;
                        cell.ShowCheckBox = false;
                    }
                }
            }        private void Form1_Load(object sender, EventArgs e)
            {
                dataGridView1.Columns[0].CellTemplate = new MyDataViewCheckBoxCell();
                for (int i = 0; i < 5; i++)
                {
                    dataGridView1.Rows.Add(new DataGridViewRow());
                }
            }
    这样就隐藏了最后一行的CheckBox