473,287 Members | 1,674 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

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
6 6578
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Leo | last post by:
I have a datagrid with the first column as a Edit,Update,Cancel button column. The other 5 columns are template columns. When I click the Edit button in IE6 the row correctly displays the...
1
by: sck10 | last post by:
Hello, I am trying to change a value when a user goes into edit mode on a DetailsView control. I am trying to use the following, but can not figure out how to get to the bound field...
4
by: wandii | last post by:
Hi, I have a datagrid attached to a dataset. It displays the records fine however, when I edit one of the cells it does not change the edit icon to the pencil icon on the left of the row I just...
1
by: phamer | last post by:
Hello. My switchbaord contains 2 options: add a record and edit a record. So, of course, if I click add a record, the first form opens in add mode; if I click edit, the first form opens in edit...
4
by: Peter | last post by:
(VS 2003) I have a DataGrid and accept button. My problem is when a user starts to edit one of the values in the grid and does not either press Enter or move of the current cell and presses the...
1
by: Mark Stafford | last post by:
I am attempting to use a DetailsView control to view some data where the fields returned by the database are determined at runtime. I create the TemplateFields on the fly using a class that...
9
by: rn5a | last post by:
A Form has a DataGrid which displays records from a SQL Server 2005 DB table. Users can modify the records using this DataGrid for which I am using EditCommandColumn in the DataGrid. This is the...
2
by: =?Utf-8?B?Z2FuZQ==?= | last post by:
Hi, In a gridview, How can i display different columns between item and edit modes. For eg. i have a sql that returns productname, categoryname, etc. In viewmode, i need to display only...
8
by: =?Utf-8?B?bWlrZWc=?= | last post by:
Hi, I am building a small Help Desk application for my company and need to be able to edit "open" help desk issues. I use a simple datagrid to display each issue (6 per page) , with an Edit...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.