弱弱地问一个问题,能不能让dataGridView1某些行不能用鼠标点击
能不能让鼠标无法点击dataGridView1第一列为空的行,
即:让所有第一列为空的行都无法用鼠标点击获取焦点?
[解决办法]
可不可以理解为:第一列为空的行,就是只读呢,那这样问题就应该解决啊!
[解决办法]
//如果希望DataGridView 内某个单元格不可编辑
//设置 DataGridView1 的第2列整列单元格为只读
DataGridView1.Columns[1].ReadOnly = true;
// 设置 DataGridView1 的第3行整行单元格为只读
DataGridView1.Rows[2].ReadOnly = true;
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;
}
}
}
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);
}
}
}