473,385 Members | 1,402 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.

Making a datagrid cell readonly

Here is what i have:
I have a datagrid with two columns one is a combo box style and the other
is a textbox style. If the combo box is "Between" or "Like" i want that the
next cell to be readonly..which is the textbox one.

How do i do this???

Tricia

--
Message posted via http://www.dotnetmonster.com
Nov 21 '05 #1
1 2383
Hi,

You have to make your own datagrid column style to do that. Here is
a simple example. You need a datagrid1 on a form. This will only allow you
to edit the name of product in the northwind database if it isnt
discontinued.

Imports System.Data.SqlClient

Public Class Form1

Inherits System.Windows.Forms.Form

Dim ds As New DataSet

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

Dim conn As SqlConnection

Dim strConn As String

Dim strSQL As String

Dim da As SqlDataAdapter

'strConn = "Provider = Microsoft.Jet.OLEDB.4.0;"

'strConn &= "Data Source = Northwind.mdb;"

strConn = "Server = (local);"

strConn &= "Database = NorthWind;"

strConn &= "Integrated Security = SSPI;"

conn = New SqlConnection(strConn)

da = New SqlDataAdapter("Select * From Products", conn)

da.Fill(ds, "Products")

Dim ts As New DataGridTableStyle

ts.MappingName = ds.Tables("Products").TableName

Dim colDiscontinued As New DataGridBoolColumn

With colDiscontinued

..MappingName = "Discontinued"

..HeaderText = "Discontinued"

..Width = 80

End With

Dim colName As New EditSomeTimes

With colName

..MappingName = "ProductName"

..HeaderText = "Product Name"

..Width = 180

End With

ts.GridColumnStyles.Add(colName)

ts.GridColumnStyles.Add(colDiscontinued)

DataGrid1.TableStyles.Add(ts)

DataGrid1.DataSource = ds.Tables("Products")

ts = Nothing

colDiscontinued = Nothing

colName = Nothing

End Sub

End Class

Public Class EditSomeTimes

Inherits DataGridTextBoxColumn

Protected Overloads Overrides Sub Edit(ByVal source As
System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal bounds
As System.Drawing.Rectangle, ByVal [readOnly] As Boolean, ByVal instantText
As String, ByVal cellIsVisible As Boolean)

Dim dt As DataTable

Try

dt = CType(Me.DataGridTableStyle.DataGrid.DataSource, DataTable)

Dim dr As DataRow

dr = dt.Rows.Item(source.Position)

If Not CBool(dr.Item("Discontinued")) Then

MyBase.Edit(source, rowNum, bounds, [readOnly], instantText, cellIsVisible)

End If

Catch ex As Exception

End Try

End Sub

End Class

Ken

-----------------------
"Tricia Colwell via DotNetMonster.com" <fo***@DotNetMonster.com> wrote in
message news:b7******************************@DotNetMonste r.com...
Here is what i have:
I have a datagrid with two columns one is a combo box style and the other
is a textbox style. If the combo box is "Between" or "Like" i want that the
next cell to be readonly..which is the textbox one.

How do i do this???

Tricia

--
Message posted via http://www.dotnetmonster.com
Nov 21 '05 #2

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

Similar topics

0
by: Job Lot | last post by:
I have an Expense Data Entry form which contains a DataGrid showing various expense categories. There are three columns Description, Cash Exp, Credit Exp, where Description column is readonly. ...
3
by: Bill C. | last post by:
Hello, I know this has been discussed a lot already because I've been searching around for information the last few weeks. I'm trying to implement a DataGridComboBoxColumn class. I've found...
3
by: PeterZ | last post by:
G'day, After doing much searching and pinching bits of ideas from here there and everywhere I came up with a fairly 'clean' solution of including a comboBox into a dataGrid column. You can...
2
by: CSL | last post by:
I am using the DataGrid in a Windows Application, how can I adjust the widths of each column individually.
14
by: scorpion53061 | last post by:
In this code what do I have to do to get the read only and Height property to work? this is going into a datagrid. Dim BOOKSLABEL As New ColoredTextBoxColumn BOOKSLABEL.TextBox.BackColor =...
8
by: Scott Meddows | last post by:
I have a datagrid control that I've inherited from the base datagrid control (Source below). I am applying a datatable style onto the datatable that I assign as my datasource. All of my column...
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. ...
2
by: Flack | last post by:
Hey guys, I have a DataGrid and DataTable field in my class : private ImageDataGrid dataGrid1; //ImageDataGrid extends dataGrid and just overides OnMouseDown private DataTable dt = new...
6
by: Doug Bell | last post by:
Hi I have a DataGrid with some hidden columns and also some read Only and some ComboBox Columns. Sandard Tabbing through the Datagrid sees the focus go to the hidden columns requiring further...
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...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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...

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.