ReadOnly look and feel in DataGrid

  • Thread starter Thread starter SgeM
  • Start date Start date
S

SgeM

I need to show some data in a readonly DataGrid. I was easily able to
make the grid readonly (DataGrid1.ReadOnly = True) so that data could
not be changed. But grid cells still go in edit mode (i.e., text is
selected and cursor is blinking) giving the wrong impression that the
text can be edited. This is in my opinion a design flow, BTW.

If I lock it it looks fine, but then I can't select cells, rows, or
cols, so no good either.

I'm still pretty new at this, so please be clear and thorough in your
reply. Sample code is highly appreciated.
Thanks
Simon
 
Hi SgeM,

Thanks for your post!

Based on my understanding, What you want is to prevent the DataGridCell
into the edit mode(or style) when a user selects it. To achieve this
purpose, you can change the property of
DataGridTextBoxColumn.TextBox.Enable to "False".

The following codes will do this job.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

' Retrieve data from the database.
Dim con As New
SqlConnection("server=MyServer;uid=sa;pwd=sa;database=northwind")
Dim daEmp As New SqlDataAdapter("Select * From customers", con)
Dim ds As New DataSet()
daEmp.Fill(ds, "customers")

Dim tableStyle As New DataGridTableStyle()
tableStyle.MappingName = "customers"

Dim DataGridTBXColumn As DataGridTextBoxColumn = Nothing

Dim i As Integer
Dim numCols As Integer = ds.Tables("customers").Columns.Count

For i = 0 To numCols - 1
DataGridTBXColumn = New DataGridTextBoxColumn()
DataGridTBXColumn.TextBox.Enabled = False
DataGridTBXColumn.TextBox.BackColor = System.Drawing.Color.White
DataGridTBXColumn.HeaderText =
ds.Tables("customers").Columns(i).ColumnName
DataGridTBXColumn.MappingName =
ds.Tables("customers").Columns(i).ColumnName
tableStyle.GridColumnStyles.Add(DataGridTBXColumn)
Next

DataGrid1.TableStyles.Clear()
DataGrid1.TableStyles.Add(tableStyle)

DataGrid1.DataSource = ds.Tables("customers")
End Sub
End Class

If you have any Qs, please reply to this post.
 
Parker,
I tried the code and although it works, it kind of screws up the
colors: on MouseDown the cell text turns grey. Also the line:
DataGridTBXColumn.TextBox.BackColor = System.Drawing.Color.White

forces one bg color which is a problem when using alternating
backgrounds.

Can you think of any other way, such as trapping clicks and keydowns
and disable edit at that point ? I tried with no success.

Thanks again!
Simon
 
Thanks, Parker. You gave us a good solution.

SgeM, you can try another way to achieve this.

' Retrieve data from the database.
Dim con As New
SqlConnection("server=MyServer;uid=sa;pwd=sa;database=northwind")
Dim daEmp As New SqlDataAdapter("Select * From customers", con)
Dim ds As New DataSet
daEmp.Fill(ds, "customers")
ds.Tables(0).DefaultView.AllowEdit = False

DataGrid1.DataSource = ds.Tables(0)

If anything is unclear, please feel free to reply to the post.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."


--------------------
| NNTP-Posting-Date: Fri, 26 Sep 2003 11:08:14 -0500
| From: SgeM <[email protected]>
| Newsgroups:
microsoft.public.dotnet.framework,microsoft.public.dotnet.framework.adonet,m
icrosoft.public.dotnet.general
| Subject: Re: ReadOnly look and feel in DataGrid
| Date: Fri, 26 Sep 2003 12:08:14 -0400
| Message-ID: <[email protected]>
| References: <[email protected]>
<[email protected]>
| X-Newsreader: Forte Free Agent 1.93/32.576 English (American)
| MIME-Version: 1.0
| Content-Type: text/plain; charset=us-ascii
| Content-Transfer-Encoding: 7bit
| Lines: 67
| X-Trace:
sv3-0YWu2QGp/BlrBl1ooYby54uLKnfmnc0DVcdHSoPC3c1LSsyq3x3kk27AjmBUt44OMtb4MHnt
Ic9HAkN!xBthdJjvhITPyWJ8wZ6thdMKQyIlr24wXk/Uqr5K9rqbsdrimDgWjA7QJ5TVMGYUO6oD
8A==
| X-Complaints-To: (e-mail address removed)
| X-DMCA-Notifications: http://www.giganews.com/info/dmca.html
| X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
| X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your
complaint properly
| X-Postfilter: 1.1
| Path:
cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed00.sul.t-online.de!t-onlin
e.de!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!small1.nntp.aus1.gigane
ws.com!border1.nntp.aus1.giganews.com!intern1.nntp.aus1.giganews.com!nntp.gi
ganews.com!news.giganews.com.POSTED!not-for-mail
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.adonet:62230
microsoft.public.dotnet.general:110038
microsoft.public.dotnet.framework:54828
| X-Tomcat-NG: microsoft.public.dotnet.framework.adonet
|
| Parker,
| I tried the code and although it works, it kind of screws up the
| colors: on MouseDown the cell text turns grey. Also the line:
|
| >DataGridTBXColumn.TextBox.BackColor = System.Drawing.Color.White
|
| forces one bg color which is a problem when using alternating
| backgrounds.
|
| Can you think of any other way, such as trapping clicks and keydowns
| and disable edit at that point ? I tried with no success.
|
| Thanks again!
| Simon
|
| On Thu, 25 Sep 2003 11:10:01 GMT, (e-mail address removed)
| (Parker Zhang [MSFT]) wrote:
|
| >Hi SgeM,
| >
| >Thanks for your post!
| >
| >Based on my understanding, What you want is to prevent the DataGridCell
| >into the edit mode(or style) when a user selects it. To achieve this
| >purpose, you can change the property of
| >DataGridTextBoxColumn.TextBox.Enable to "False".
| >
| >The following codes will do this job.
| >
| >Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
| >System.EventArgs) Handles MyBase.Load
| >
| > ' Retrieve data from the database.
| > Dim con As New
| >SqlConnection("server=MyServer;uid=sa;pwd=sa;database=northwind")
| > Dim daEmp As New SqlDataAdapter("Select * From customers", con)
| > Dim ds As New DataSet()
| > daEmp.Fill(ds, "customers")
| >
| > Dim tableStyle As New DataGridTableStyle()
| > tableStyle.MappingName = "customers"
| >
| > Dim DataGridTBXColumn As DataGridTextBoxColumn = Nothing
| >
| > Dim i As Integer
| > Dim numCols As Integer = ds.Tables("customers").Columns.Count
| >
| > For i = 0 To numCols - 1
| > DataGridTBXColumn = New DataGridTextBoxColumn()
| > DataGridTBXColumn.TextBox.Enabled = False
| > DataGridTBXColumn.TextBox.BackColor =
System.Drawing.Color.White
| > DataGridTBXColumn.HeaderText =
| >ds.Tables("customers").Columns(i).ColumnName
| > DataGridTBXColumn.MappingName =
| >ds.Tables("customers").Columns(i).ColumnName
| > tableStyle.GridColumnStyles.Add(DataGridTBXColumn)
| > Next
| >
| > DataGrid1.TableStyles.Clear()
| > DataGrid1.TableStyles.Add(tableStyle)
| >
| > DataGrid1.DataSource = ds.Tables("customers")
| > End Sub
| >End Class
| >
| >If you have any Qs, please reply to this post.
|
|
 
Back
Top