首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

弱弱地问一个有关问题,能不能让dataGridView1某些行不能用鼠标点击

2013-01-02 
弱弱地问一个问题,能不能让dataGridView1某些行不能用鼠标点击能不能让鼠标无法点击dataGridView1第一列为

弱弱地问一个问题,能不能让dataGridView1某些行不能用鼠标点击
能不能让鼠标无法点击dataGridView1第一列为空的行,

即:让所有第一列为空的行都无法用鼠标点击获取焦点?
[解决办法]
可不可以理解为:第一列为空的行,就是只读呢,那这样问题就应该解决啊!
[解决办法]


//如果希望DataGridView 内某个单元格不可编辑

//设置 DataGridView1 的第2列整列单元格为只读 
DataGridView1.Columns[1].ReadOnly = true;
// 设置 DataGridView1 的第3行整行单元格为只读 
DataGridView1.Rows[2].ReadOnly = true; 


不知道是不是这个意思。。。
[解决办法]
简单的用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;
            }
        }
    }
[解决办法]
c/s控件?好像也有类似RowDataBound事件吧。不太熟悉!
[解决办法]
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;
                }
            }
        }
    }
}


[解决办法]
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;
                }
            }
        }

[解决办法]
搞反了
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;
                }
            }
        }
------解决方案--------------------


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

引用:
难道这个问题真的没有办法了吗?

就是当某行的第一列为空时,鼠标不能点击获得焦点,键盘按键都实现了,就是鼠标不能控制,请高手指点!


你既然键盘按键实现了  不会鼠标?

CellClick 事件把你的代码放进去就行了,,你要的是屏蔽焦点  不是只读?
屏蔽焦点还真不清楚咋做,,有时间可以研究下。。。只读的上面说的很清楚了
[解决办法]
可以。。。
[解决办法]
好难啊
[解决办法]
申明一个静态变量num=0  在行绑定事件中判断第一列是否为空并且判断num是否为0,为空并且0==num则将该行设置为只读,并且在每次绑定之前将num归0,这样应该就可以实现你要的效果了
[解决办法]
接上面忘记说 num++了
[解决办法]
判断得到焦点以后,马上失去焦点
[解决办法]
该回复于2011-01-04 17:33:47被版主删除
[解决办法]
可以不绘制指定单元格的焦点框,
RowPrePaint事件
e.PaintParts &= ~DataGridViewPaintParts.Focus;

[解决办法]
引用:
引用:
可以不绘制指定单元格的焦点框,
RowPrePaint事件
e.PaintParts &= ~DataGridViewPaintParts.Focus;


这个代码编译无法通过。

提示什么?我刚试了 可以编译啊。

[解决办法]
http://msdn.microsoft.com/zh-cn/library/system.windows.forms.datagridviewrowprepainteventargs_members(v=VS.80).aspx

有这个属性啊。

不过这个方法 单元格还是能选中, 
只是要重新设置选择状态的背景色和当前色,不显示焦点框,并且单元格为只读。这样效果就和不能选中一样了
[解决办法]
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);
        }
    }
}

热点排行