Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm Controls / December 2006

Tip: Looking for answers? Try searching our database.

How to prevent DataGrid cell editing ?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Sharon - 19 Mar 2006 09:22 GMT
I have a DataGrid on my Form, and I wish to disable any kind of cell editing.
For example; when the user clicks on a cell, the cell become editable by
changing its color to gray and context menu is shown on muse right mouse
click.
I do not want the enable this cell context menu (I have my own context menu)
and I do not want the cell to change it's color. I want that on any click -
the entire row will be selected.
There are some other cases where the cell become editable, and I want to
prevent them all.
I did not find any event that I can hook to for that.
I'm setting my dataGrid with a DataSet by:
m_DataSet = new myDataSet();// myDataSet is created by a XSD schema.
DataTable defectsTable = m_DataSet.Tables["Defects"]
myDataGrid.DataSource = new DataView(defectsTable);
All the cells are not editable, but in same cases the TexBox cells get in
edit mode while the text can not be changed, but the cell become gray and a
default context menu is shown overriding my own.

I looked at George Shepherd's Windows Forms FAQ:
http://www.syncfusion.com/FAQ/WindowsForms/FAQ_c44c.aspx
And tried the following:
5.7 How do I prevent a click into the grid highlighting a cell?*** Not good
for me because then the user can do nothing with the grid.

5.11 How can I select the entire row when the user clicks on a cell in the
row?*** Doing that.

5.18 How can I prevent a particular cell from being editable?*** Done that,
not solving my problem.

5.41 How can I make my grid never have an active edit cell and always select
whole rows (as in a browser-type grid)?*** Like 5.18, not solving my problem.

5.87 How can I prevent all the cells in my DataGrid from being edited
without deriving GridColumnStyle?*** Why should I do that?! and in my case
the rows are added on runtime so this function will have to used at any row
adding. I did it but the Control.Count is always 2 and this 2 are the
scrollbars.

If the highlighting of the selected cell is a built-in feature of the
standard Winform datagrid control, maybe I can drived from this control and
do something about it by overriding some functionally.

Can any guru in here tell how to prevent any kind of cell editing in the
DataGrid?

-------
Thanks
Sharon
"Jeffrey Tan[MSFT]" - 20 Mar 2006 03:09 GMT
Hi Sharon,

Thanks for your post!

Can you show me the real problem regarding these Winform FAQs? I have
downloaded and tried "5.41 How can I make my grid never have an active edit
cell and always select whole rows (as in a browser-type grid)?", which
works well on my side. No cell will become edit mode. Why does not it solve
your problem?

Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Signature

Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Sharon - 20 Mar 2006 15:59 GMT
The common way to reproduce this problem is to add some rows so the
horizontal scrollbar is shown, select an entire row, and then touch and move
the scroll bar down or up.
Result : The current cell that you clicked on to select the row get into
edit mode.

------
Thanks
Sharon
"Jeffrey Tan[MSFT]" - 21 Mar 2006 07:53 GMT
Hi Sharon,

Thanks for your feedback.

I am not sure if you have downloaded that sample. This sample retrieves the
datasource from "northwind" sample database of Access, which contains a lot
of data, which will force DataGrid's Vertical and Horizontal scrollbar to
appear. However, after following you instructions to select one row then
scroll the Vertical bar up and down, I can not reproduce out the edit mode
problem.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Signature

Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

adh - 14 Apr 2006 13:14 GMT
Me too I have tried:
5.18 How can I prevent a particular cell from being editable?***
Me too: Done that, did not solve the problem.
Explanation:
To a regular VB-Winforms Datagrid I added a right-Click-Context-Menu. It
works, but the problem is that when the user Left_Clicks a cell (which
he usally does) and then Right_clicks (to popup the context_menu) this
cell is already sellected and bcs of that the regular Win_context-menu
pops up (not my private one).
I looked and tried all kinds of suggestions, but Nada, the cell still
remains selected.

Please help.

Thanks, adh
VJ - 15 Apr 2006 23:20 GMT
public class CustomDataGrid : System.Windows.Forms.DataGrid
{

//this override will prevent the cell in row 1 from getting the edit focus

protected override void Edit(System.Windows.Forms.CurrencyManager source,
int rowNum, System.Drawing.Rectangle bounds, bool readOnly,      string
instantText, bool cellIsVisible)
{
     if(rowNum == 1)
          return;
     base.Edit(source, rowNum, bounds, readOnly, instantText,
cellIsVisible);
}

}

codeproject.com has some good examples... also check this link

http://www.syncfusion.com/FAQ/WindowsForms/FAQ_c44c.aspx

VJ

> Me too I have tried:
> 5.18 How can I prevent a particular cell from being editable?***
[quoted text clipped - 13 lines]
>
> *** Sent via Developersdex http://www.developersdex.com ***
adh - 16 Apr 2006 06:30 GMT
To VJ,
Thanks I'll try it out.
As for
http://www.syncfusion.com/FAQ/WindowsForms/FAQ_c44c.aspx
none of them gave a solution (not in VB2003)

Thanks, adh
adh - 17 Apr 2006 18:31 GMT
TO VJ II
Thanks, I tried out: protected override void Edit(.....
without success. My project is a VB.NET (2003) and I could not fix the
translation of your C# proc. to VB.
Could you please send it to me?

Thanks, adh
Anant Tiwari - 04 Dec 2006 06:17 GMT
hi
This is the vb.net form you have to just copy paste the below code in
subroutine into mouse up event of the datagrid

Hope this will solve ur problem

Private Sub dataGrid1_MouseUp(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles dataGrid1.MouseUp

    Dim pt = New Point(e.X, e.Y)

    Dim hti As DataGrid.HitTestInfo = dataGrid1.HitTest(pt)

    If hti.Type = DataGrid.HitTestType.Cell Then

         dataGrid1.CurrentCell = New DataGridCell(hti.Row, hti.Column)

         dataGrid1.Select(hti.Row)

    End If

End Sub


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.