473,322 Members | 1,241 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,322 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 4324
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.