PC Review


Reply
Thread Tools Rate Thread

ReadOnly look and feel in DataGrid

 
 
SgeM
Guest
Posts: n/a
 
      24th Sep 2003
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



 
Reply With Quote
 
 
 
 
Parker Zhang [MSFT]
Guest
Posts: n/a
 
      25th Sep 2003
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 Zhang
Microsoft Developer Support

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

 
Reply With Quote
 
 
 
 
SgeM
Guest
Posts: n/a
 
      26th Sep 2003
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 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.


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How does volt feel ? How does ampere feel ? Skybuck Flying DIY PC 7 3rd Jul 2011 02:47 PM
Textbox readonly -> but not with readonly property Milosz - [playseven.com] Microsoft Dot NET Compact Framework 3 1st Mar 2004 01:27 PM
I feel seated in between two chairs and do feel unconfortable... =?Utf-8?B?UGF1bCwgQnJ1c3NlbHM=?= Windows XP Setup 2 9th Jan 2004 03:45 AM
ReadOnly look and feel in DataGrid SgeM Microsoft ADO .NET 3 29th Sep 2003 03:07 AM
ReadOnly look and feel in DataGrid SgeM Microsoft Dot NET Framework 3 26th Sep 2003 05:08 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:45 AM.