467,118 Members | 907 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,118 developers. It's quick & easy.

Edit mode of DataGridCell

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
Nov 15 '05 #1
  • viewed: 6330
Share:
6 Replies
Tamir Khason wrote:
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)
Nov 15 '05 #2
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?

"Frank Oquendo" <fr****@acadxpin.com> wrote in message
news:uB****************@TK2MSFTNGP09.phx.gbl...
Tamir Khason wrote:
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)

Nov 15 '05 #3
Tamir Khason wrote:
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)
Nov 15 '05 #4

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.

Nov 15 '05 #5

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/dotne...able_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.d ataGrid1)).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.dataGr id1_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.d ataGrid1)).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[i].ColumnName;
column.MappingName=ds.Tables[0].Columns[i].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.

Nov 15 '05 #6
Thanks, I'll try it.
Merry Christmas to you too.


""Jeffrey Tan[MSFT]"" <v-*****@online.microsoft.com> wrote in message
news:re**************@cpmsftngxa07.phx.gbl...

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/dotne...able_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.d ataGrid1)).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.dataGr id1_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.d ataGrid1)).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[i].ColumnName;
column.MappingName=ds.Tables[0].Columns[i].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.

Nov 15 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Leo | last post: by
1 post views Thread by sck10 | last post: by
4 posts views Thread by wandii | last post: by
1 post views Thread by phamer@ibhs.org | last post: by
4 posts views Thread by Peter | last post: by
9 posts views Thread by rn5a@rediffmail.com | last post: by
2 posts views Thread by =?Utf-8?B?Z2FuZQ==?= | last post: by
8 posts views Thread by =?Utf-8?B?bWlrZWc=?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.