473,846 Members | 1,914 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6607
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 DataGridTableSt yle 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****@acadxpi n.com> wrote in message
news:uB******** ********@TK2MSF TNGP09.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 DataGridTableSt yle 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 EditItemTemplat e 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
DataGridColumnS tyle.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(Becaus e 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(Actua lly, I do hittest in mouse down event):

using System;
using System.Drawing;
using System.Collecti ons;
using System.Componen tModel;
using System.Windows. Forms;
using System.Data;
using System.Data.Sql Client;

namespace datagridreadonl y
{
public delegate void datagridselecti ondelegate(obje ct sender,
textboxcelldisa bleeventargs e);

public class Form1 : System.Windows. Forms.Form
{
private System.Windows. Forms.DataGrid dataGrid1;
private System.Componen tModel.Containe r components = null;

public Form1()
{
InitializeCompo nent();
}

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Disp ose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code

private void InitializeCompo nent()
{
this.dataGrid1 = new System.Windows. Forms.DataGrid( );
((System.Compon entModel.ISuppo rtInitialize)(t his.dataGrid1)) .BeginInit();
this.SuspendLay out();
//
// dataGrid1
//
this.dataGrid1. DataMember = "";
this.dataGrid1. HeaderForeColor = System.Drawing. SystemColors.Co ntrolText;
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.MouseEven tHandler(this.d ataGrid1_MouseD own);
//
// Form1
//
this.AutoScaleB aseSize = new System.Drawing. Size(5, 13);
this.ClientSize = new System.Drawing. Size(560, 342);
this.Controls.A dd(this.dataGri d1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHan dler(this.Form1 _Load);
((System.Compon entModel.ISuppo rtInitialize)(t his.dataGrid1)) .EndInit();
this.ResumeLayo ut(false);

}
#endregion

[STAThread]
static void Main()
{
Application.Run (new Form1());
}

private void Form1_Load(obje ct sender, System.EventArg s e)
{
SqlDataAdapter adapter=new SqlDataAdapter( "select * from
jobs","server=l ocalhost;databa se=pubs;uid=sa; pwd=");
DataSet ds=new DataSet();
adapter.Fill(ds );

DataGridTableSt yle dgts=new DataGridTableSt yle();
dgts.MappingNam e=ds.Tables[0].TableName;
for(int i=0;i<ds.Tables[0].Columns.Count; i++)
{
datagridreadonl ycolumn column=new datagridreadonl ycolumn(i);
column.HeaderTe xt=ds.Tables[0].Columns[i].ColumnName;
column.MappingN ame=ds.Tables[0].Columns[i].ColumnName;

column.textboxc ellselectioneve nthandler+=new
datagridselecti ondelegate(setc ellselection);

dgts.GridColumn Styles.Add(colu mn);
}

dataGrid1.Table Styles.Clear();
dataGrid1.Table Styles.Add(dgts );

dataGrid1.DataS ource=ds.Tables[0];
}
private int selectedindex_x =-1;
private int selectedindex_y =-1;
public void setcellselectio n(object sender, textboxcelldisa bleeventargs e)
{
e.enable_select ion=false;
if (selectedindex_ x==e.row&&selec tedindex_y==e.c ol)
{
e.enable_select ion = true;
}
else
{
e.enable_select ion = false;
}
}

private void dataGrid1_Mouse Down(object sender,
System.Windows. Forms.MouseEven tArgs e)
{
System.Windows. Forms.DataGrid. HitTestInfo myHitTest;

myHitTest=dataG rid1.HitTest(e. X,e.Y );
selectedindex_x =myHitTest.Row;
selectedindex_y =myHitTest.Colu mn;
}
}

public class textboxcelldisa bleeventargs:Ev entArgs
{
private int _col=-1;
private int _row=-1;
private bool selected=false;

public textboxcelldisa bleeventargs(in t col_num, int row_num)
{
this.col=col_nu m;
this.row=row_nu m;
}

public int col
{
get
{
return _col;
}
set
{
_col=value;
}
}

public int row
{
get
{
return _row;
}
set
{
_row=value;
}
}

public bool enable_selectio n
{
get
{
return selected;
}
set
{
selected=value;
}
}
}

public class datagridreadonl ycolumn:DataGri dTextBoxColumn
{
public event datagridselecti ondelegate textboxcellsele ctioneventhandl er;
private int _col=-1;

public datagridreadonl ycolumn(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(textboxcells electioneventha ndler!=null)
{
textboxcelldisa bleeventargs e=new
textboxcelldisa bleeventargs(_c ol,rowNum);
textboxcellsele ctioneventhandl er(this,e);

if(e.enable_sel ection==true)
{
backBrush=Brush es.Black;
foreBrush=Brush es.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(CurrencyMa nager 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.mi crosoft.com> wrote in message
news:re******** ******@cpmsftng xa07.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
DataGridColumnS tyle.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(Becaus e 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(Actua lly, I do hittest in mouse down event):

using System;
using System.Drawing;
using System.Collecti ons;
using System.Componen tModel;
using System.Windows. Forms;
using System.Data;
using System.Data.Sql Client;

namespace datagridreadonl y
{
public delegate void datagridselecti ondelegate(obje ct sender,
textboxcelldisa bleeventargs e);

public class Form1 : System.Windows. Forms.Form
{
private System.Windows. Forms.DataGrid dataGrid1;
private System.Componen tModel.Containe r components = null;

public Form1()
{
InitializeCompo nent();
}

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Disp ose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code

private void InitializeCompo nent()
{
this.dataGrid1 = new System.Windows. Forms.DataGrid( );
((System.Compon entModel.ISuppo rtInitialize)(t his.dataGrid1)) .BeginInit();
this.SuspendLay out();
//
// dataGrid1
//
this.dataGrid1. DataMember = "";
this.dataGrid1. HeaderForeColor = System.Drawing. SystemColors.Co ntrolText;
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.MouseEven tHandler(this.d ataGrid1_MouseD own);
//
// Form1
//
this.AutoScaleB aseSize = new System.Drawing. Size(5, 13);
this.ClientSize = new System.Drawing. Size(560, 342);
this.Controls.A dd(this.dataGri d1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHan dler(this.Form1 _Load);
((System.Compon entModel.ISuppo rtInitialize)(t his.dataGrid1)) .EndInit();
this.ResumeLayo ut(false);

}
#endregion

[STAThread]
static void Main()
{
Application.Run (new Form1());
}

private void Form1_Load(obje ct sender, System.EventArg s e)
{
SqlDataAdapter adapter=new SqlDataAdapter( "select * from
jobs","server=l ocalhost;databa se=pubs;uid=sa; pwd=");
DataSet ds=new DataSet();
adapter.Fill(ds );

DataGridTableSt yle dgts=new DataGridTableSt yle();
dgts.MappingNam e=ds.Tables[0].TableName;
for(int i=0;i<ds.Tables[0].Columns.Count; i++)
{
datagridreadonl ycolumn column=new datagridreadonl ycolumn(i);
column.HeaderTe xt=ds.Tables[0].Columns[i].ColumnName;
column.MappingN ame=ds.Tables[0].Columns[i].ColumnName;

column.textboxc ellselectioneve nthandler+=new
datagridselecti ondelegate(setc ellselection);

dgts.GridColumn Styles.Add(colu mn);
}

dataGrid1.Table Styles.Clear();
dataGrid1.Table Styles.Add(dgts );

dataGrid1.DataS ource=ds.Tables[0];
}
private int selectedindex_x =-1;
private int selectedindex_y =-1;
public void setcellselectio n(object sender, textboxcelldisa bleeventargs e)
{
e.enable_select ion=false;
if (selectedindex_ x==e.row&&selec tedindex_y==e.c ol)
{
e.enable_select ion = true;
}
else
{
e.enable_select ion = false;
}
}

private void dataGrid1_Mouse Down(object sender,
System.Windows. Forms.MouseEven tArgs e)
{
System.Windows. Forms.DataGrid. HitTestInfo myHitTest;

myHitTest=dataG rid1.HitTest(e. X,e.Y );
selectedindex_x =myHitTest.Row;
selectedindex_y =myHitTest.Colu mn;
}
}

public class textboxcelldisa bleeventargs:Ev entArgs
{
private int _col=-1;
private int _row=-1;
private bool selected=false;

public textboxcelldisa bleeventargs(in t col_num, int row_num)
{
this.col=col_nu m;
this.row=row_nu m;
}

public int col
{
get
{
return _col;
}
set
{
_col=value;
}
}

public int row
{
get
{
return _row;
}
set
{
_row=value;
}
}

public bool enable_selectio n
{
get
{
return selected;
}
set
{
selected=value;
}
}
}

public class datagridreadonl ycolumn:DataGri dTextBoxColumn
{
public event datagridselecti ondelegate textboxcellsele ctioneventhandl er;
private int _col=-1;

public datagridreadonl ycolumn(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(textboxcells electioneventha ndler!=null)
{
textboxcelldisa bleeventargs e=new
textboxcelldisa bleeventargs(_c ol,rowNum);
textboxcellsele ctioneventhandl er(this,e);

if(e.enable_sel ection==true)
{
backBrush=Brush es.Black;
foreBrush=Brush es.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(CurrencyMa nager 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
1639
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 controls defined in the <EditItemTemplate> however when I right click and do a view source I cannot find any of the input textboxes in the source. I have used the ItemDatabound event to try to attach javascript to the textboxes and in debug mode while...
1
4442
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 ("MyBoundField") to set its new value. Any help with this would be appreciated. Sub EmployeeDetailView_ModeChanged(ByVal sender As Object, ByVal e As EventArgs) Select Case CustomerDetailView.CurrentMode
4
1918
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 changed. I have the allowediting property set to true which should upon editing a cell indicate row is in edit mode. How can I set the edit mode ? Thanks in advance.
1
8269
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 mode. BUT, when I click on the nav buttons I've created on the forms, the next form will only open in the mode specified by the code for that button. So I need to find out how I can set up the code so that, if I open the first form in add mode,...
4
7630
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 Accept buton the current typed value does not get passed into the grid, but stays in the edit mode. How can I move the current edit value into the grid without requireing user to press Enter or move to a different cell? Thanks
1
7327
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 implements ITemplate and repopulate the Template properters of TemplateField in OnInit. And I am DataBinding by getting a DataTable from my db provider class in Page_Load event. When I databind programmatically in the Page_Load, the data displays in...
9
2734
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 code: <script runat="server"> Dim sqlConn As New SqlConnection(".....") Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs) If Not (Page.IsPostBack) Then FillDataGrid()
2
7238
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 productname column and not categoryname column. Again in Edit mode, i need display/edit category name only. But not productname column shouldnt be displayed/editable. In visual studio 2005, I tried converting the columns to template fields and
8
2000
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 button. There are a lot of fields across, and my Network Admins would like a way of editing/updating each issue seperately in a a vertical form. I need a way of clicking on the edit button ,bringing up a form with the record, editng the fields, and...
0
9879
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9727
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10978
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10643
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9477
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7050
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4521
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4113
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3158
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.