Hi Tamir,
Based on my understanding, I think you want to disable the winform
datagrid's default edit mode.
The article below tells you how to do it, actually, it disabled the
DataGridColumnStyle.Edit Method:
http://www.akadia.com/services/dotnet_datagrid_disable_cell.html
But I think you need to do your customized cell selection for your
datagrid(Because your datagrid diabled the edit mode, you can not select a
datagrid cell),
I wrote a sample below which change the selected cell's forecolor and
backcolor as selection(Actually, I do hittest in mouse down event):
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
namespace datagridreadonly
{
public delegate void datagridselectiondelegate(object sender,
textboxcelldisableeventargs e);
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.DataGrid dataGrid1;
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.dataGrid1 = new System.Windows.Forms.DataGrid();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
//
// dataGrid1
//
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(48, 16);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(464, 288);
this.dataGrid1.TabIndex = 0;
this.dataGrid1.MouseDown += new
System.Windows.Forms.MouseEventHandler(this.dataGrid1_MouseDown);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(560, 342);
this.Controls.Add(this.dataGrid1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
SqlDataAdapter adapter=new SqlDataAdapter("select * from
jobs","server=localhost;database=pubs;uid=sa;pwd=");
DataSet ds=new DataSet();
adapter.Fill(ds);
DataGridTableStyle dgts=new DataGridTableStyle();
dgts.MappingName=ds.Tables[0].TableName;
for(int i=0;i<ds.Tables[0].Columns.Count;i++)
{
datagridreadonlycolumn column=new datagridreadonlycolumn(i);
column.HeaderText=ds.Tables[0].Columns
.ColumnName;
column.MappingName=ds.Tables[0].Columns.ColumnName;
column.textboxcellselectioneventhandler+=new
datagridselectiondelegate(setcellselection);
dgts.GridColumnStyles.Add(column);
}
dataGrid1.TableStyles.Clear();
dataGrid1.TableStyles.Add(dgts);
dataGrid1.DataSource=ds.Tables[0];
}
private int selectedindex_x=-1;
private int selectedindex_y=-1;
public void setcellselection(object sender, textboxcelldisableeventargs e)
{
e.enable_selection=false;
if (selectedindex_x==e.row&&selectedindex_y==e.col)
{
e.enable_selection = true;
}
else
{
e.enable_selection = false;
}
}
private void dataGrid1_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
System.Windows.Forms.DataGrid.HitTestInfo myHitTest;
myHitTest=dataGrid1.HitTest(e.X,e.Y );
selectedindex_x=myHitTest.Row;
selectedindex_y=myHitTest.Column;
}
}
public class textboxcelldisableeventargs:EventArgs
{
private int _col=-1;
private int _row=-1;
private bool selected=false;
public textboxcelldisableeventargs(int col_num, int row_num)
{
this.col=col_num;
this.row=row_num;
}
public int col
{
get
{
return _col;
}
set
{
_col=value;
}
}
public int row
{
get
{
return _row;
}
set
{
_row=value;
}
}
public bool enable_selection
{
get
{
return selected;
}
set
{
selected=value;
}
}
}
public class datagridreadonlycolumn
ataGridTextBoxColumn
{
public event datagridselectiondelegate textboxcellselectioneventhandler;
private int _col=-1;
public datagridreadonlycolumn(int col_num)
{
_col=col_num;
}
protected override void Paint(Graphics g, Rectangle bounds,
CurrencyManager source, int rowNum, Brush backBrush, Brush foreBrush, bool
alignToRight)
{
if(textboxcellselectioneventhandler!=null)
{
textboxcelldisableeventargs e=new
textboxcelldisableeventargs(_col,rowNum);
textboxcellselectioneventhandler(this,e);
if(e.enable_selection==true)
{
backBrush=Brushes.Black;
foreBrush=Brushes.White;
}
}
base.Paint (g, bounds, source, rowNum, backBrush, foreBrush,
alignToRight);
}
//override Edit method, and does not call
its base.Edit method, so the edit mode will be disabled
protected override void Edit(CurrencyManager source, int rowNum,
Rectangle bounds, bool readOnly, string instantText, bool cellIsVisible)
{
}
}
}
If you want, you can also change the cursor's icon when selection.
If you have anything unclear, please feel free to tell me. Merry Christmas!!
Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.