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); } } }
//如果希望DataGridView 内某个单元格不可编辑//设置 DataGridView1 的第2列整列单元格为只读
DataGridView1.Columns[1].ReadOnly = true;
// 设置 DataGridView1 的第3行整行单元格为只读
DataGridView1.Rows[2].ReadOnly = true;
不知道是不是这个意思
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;
}
}
}
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;
}
}
}
}
}
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;
}
}
}
我用的是dataGridView1,没有你那个事件;To chengfellow:
你的方法是否只是设置行的只读属性,并未控制焦点的获取!
你既然键盘按键实现了 不会鼠标?CellClick 事件把你的代码放进去就行了,,你要的是屏蔽焦点 不是只读?
屏蔽焦点还真不清楚咋做,,有时间可以研究下只读的上面说的很清楚了
Enabled = false,就可以让所有行都无法选中,但单独的行没有这个属性!!!
RowPrePaint事件
e.PaintParts &= ~DataGridViewPaintParts.Focus;
错误 1 “System.Windows.Forms.DataGridViewRowPostPaintEventArgs”不包含“PaintParts”的定义,并且找不到可接受类型为“System.Windows.Forms.DataGridViewRowPostPaintEventArgs”的第一个参数的扩展方法“PaintParts”(是否缺少 using 指令或程序集引用?)
只是要重新设置选择状态的背景色和当前色,不显示焦点框,并且单元格为只读。这样效果就和不能选中一样了
// 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);
}
}
}