Edit mode of DataGridCell

T

Tamir Khason

How to prevent the selected cell from being editable (visual) at DataGrid?
Once click on cell (even readonly) there are cursor inside it and select
text appears. How to prevent it

Thankx
 
F

Frank Oquendo

Tamir said:
How to prevent the selected cell from being editable (visual) at
DataGrid? Once click on cell (even readonly) there are cursor inside
it and select text appears. How to prevent it

Define a DataGridTableStyle for your grid. The individual columns styles
have a ReadOnly property which will do what you want.

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
 
T

Tamir Khason

That's not exactly what I'm looking for.
When you click on textbox item (default) in datagrid you'll recieve REGULAR
TEXTBOX EDIT MODE (including text selection and cursor inside). I want to
prevent it in DataGrid, but there is no override for DataGridCell
(valuestype) - How to do it?
 
F

Frank Oquendo

Tamir said:
That's not exactly what I'm looking for.
When you click on textbox item (default) in datagrid you'll recieve
REGULAR TEXTBOX EDIT MODE (including text selection and cursor
inside). I want to prevent it in DataGrid, but there is no override
for DataGridCell (valuestype) - How to do it?

Edit the ItemTemplate or EditItemTemplate and set the ReadOnly property
of your TextBox to True.

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
 
J

Jeffrey Tan[MSFT]

Hi Frank,

Tamir's datagrid is a windows form based, while your class and property is
for web form datagrid

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.
 
J

Jeffrey Tan[MSFT]

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:DataGridTextBoxColumn
{
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.
 
T

Tamir Khason

Thanks, I'll try it.
Merry Christmas to you too.




"Jeffrey Tan[MSFT]" said:
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:DataGridTextBoxColumn
{
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top