473,385 Members | 1,757 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,385 software developers and data experts.

ReadOnly look and feel in DataGrid

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

Jul 21 '05 #1
2 4326
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;datab ase=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.

Jul 21 '05 #2
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, pa******@online.microsoft.com
(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;data base=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.


Jul 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Paulo Gon?alves via .NET 247 | last post by:
My datagrid has a big problem, it happens only in columns that are read only. Please see the picture at http://mega.ist.utl.pt/~pfrg/datagrid/datagrid_problem.jpg The gost that I show it?s like a...
3
by: Lyners | last post by:
I am having a hard time with this one, and I thought it would be easy. I have a datagrid in which I have textboxs for users to enter data. One of the fields in the database behind the datagrid...
4
by: Doug Bell | last post by:
Hi, I needed to make a TextBox on a DataGrid ReadOnly based on the condition of another cell on its row. I achieved this using a Custom Data Column Class "DacDGTextColLotNo" but I can't get the...
4
by: John Smith | last post by:
Hi All; I am using the HeaderAndDataAlignColumn class from Ken Tucker. I have customized it for a wider row. I reposition the DrawString method so the text is centered vertically in the row. ...
0
by: johnb41 | last post by:
I have a datagrid that has it's first column set to a checkbox column. I need that column to be "checkable", while all the others not editable. So I created columnstyles First I add a boolean...
2
by: SgeM | last post by:
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...
5
by: John Smith | last post by:
Is it possible to set DataGrid's ReadOnly property to True/False at runtime? An example: <asp:datagrid id="dg1" runat="server" AutoGenerateColumns="False"> <Columns> <asp:BoundColumn...
3
by: Hamed | last post by:
Hello I have a DataGrid object in my ASP.NET page that has the following template column. When I put the "readonly" attribute in the INPUT tag, it generates readonly="". <asp:TemplateColumn...
7
by: RolfHerbert | last post by:
Hi all, Source code below. I am using an sqldatasorce to populate a datagrid. I am using datakeyIds. I am interested in two keys which I have added to the datakeyids, however if either of the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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,...
0
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...

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.