Partially "ReadOnly" windows form datagrid

  • Thread starter Thread starter Dany P. Wu
  • Start date Start date
D

Dany P. Wu

Hi everyone,

I'm not entirely sure if this is the best way of going about it, but here's
the scenario..........

I have two datagrid, each bound to a datatable which have checkbox columns.
There are two buttons between the datagrids, "Add" and "Remove". They
basically remove rows from one grid to the other, only those with the
checkbox ticked.

The problem is I don't want users to add new rows, or alter any of the
columns except for the checkbox column. Is this possible? Or perhaps is
there a better way of selecting multiple rows and working with them? I
realise I have to work with the underlying datasources, *not* the datagrids
themselves, but damn if I know how to go about it.

It's been bugging me all weekend. Any suggestions would be greatly
appreciated.

Cheers,
Dany.
 
The problem is I don't want users to add new rows, or alter any of the
columns except for the checkbox column. Is this possible?

Create a DataGridTableStyle set your column to read only



Or perhaps is
 
Hi,

To prevent the user from adding a new row set the
datatables.defaultview.allownew = false.

http://msdn.microsoft.com/library/d...frlrfsystemdatadataviewclassallownewtopic.asp

To make a column readonly set the datagridtextbox column readonly
property to false.

http://msdn.microsoft.com/library/d...msdatagridtextboxcolumnclassreadonlytopic.asp

Ken
------------------------
Hi everyone,

I'm not entirely sure if this is the best way of going about it, but here's
the scenario..........

I have two datagrid, each bound to a datatable which have checkbox columns.
There are two buttons between the datagrids, "Add" and "Remove". They
basically remove rows from one grid to the other, only those with the
checkbox ticked.

The problem is I don't want users to add new rows, or alter any of the
columns except for the checkbox column. Is this possible? Or perhaps is
there a better way of selecting multiple rows and working with them? I
realise I have to work with the underlying datasources, *not* the datagrids
themselves, but damn if I know how to go about it.

It's been bugging me all weekend. Any suggestions would be greatly
appreciated.

Cheers,
Dany.
 
Ken Tucker said:
Hi,
To prevent the user from adding a new row set the
datatables.defaultview.allownew = false.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/htm
l/frlrfsystemdatadataviewclassallownewtopic.asp To make a column
readonly set the datagridtextbox column readonly property to false.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/htm
l/frlrfsystemwindowsformsdatagridtextboxcolumnclassreadonlytopic.asp Ken

Thanks, I've actually done those. I manually added the datagridboolcolumn
and datagridtextboxcolumns to a newly created tablestyle, each mapped to the
appropriate datatable columns.

I set the textbox columns "readonly=false" and the boolcolumn
"readonly=true". It worked for the textbox columns - i.e. they are not
editable. Unfortunately it didn't work for the boolcolumn! I could not
change the value of the column. In fact, if I click on it I get an
indexoutofrange exception! The checkbox column is the first one in the
tablestyle.

Any idea why this may be a problem?

Cheers,
D.
 
Dany,

I did today another sample, so this one was easy to made from it.

\\\Needs only a datagrid on a form
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
DataGrid1.DataSource = CreateTables()

DirectCast(DataGrid1.DataSource, _
DataTable).DefaultView.AllowNew = False
Dim dgts As New DataGridTableStyle
dgts.MappingName = "Persons"
Dim textCol As New DataGridTextBoxColumn
textCol.MappingName = "Name"
textCol.HeaderText = "Name"
textCol.Width = 200
textCol.ReadOnly = True
dgts.GridColumnStyles.Add(textCol)
Dim boolCol As New DataGridBoolColumn
boolCol.MappingName = "US"
boolCol.HeaderText = "US"
boolCol.Width = 20
boolCol.NullValue = False
dgts.GridColumnStyles.Add(boolCol)
DataGrid1.TableStyles.Add(dgts)
End Sub
'Sample datatable
Private Function CreateTables() As DataTable
Dim dtVBReg As New DataTable("Persons")
dtVBReg.Columns.Add("Name")
dtVBReg.Columns.Add("US", GetType(System.Boolean))
For i As Integer = 0 To 6
dtVBReg.Rows.Add(dtVBReg.NewRow)
Next
dtVBReg.Rows(0).ItemArray = New Object() _
{"Herfried K. Wagner", False}
dtVBReg.Rows(1).ItemArray = New Object() _
{"Ken Tucker", True}
dtVBReg.Rows(2).ItemArray = New Object() _
{"CJ Taylor", True}
dtVBReg.Rows(3).ItemArray = New Object() _
{"Jay B Harlow", True}
dtVBReg.Rows(4).ItemArray = New Object() _
{"Terry Burns", False}
dtVBReg.Rows(5).ItemArray = New Object() _
{"Tom Shelton", True}
dtVBReg.Rows(6).ItemArray = New Object() _
{"Cor Ligthert", False}
Return dtVBReg
End Function
////

I hope this helps a little bit?

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

DataGrid with checkboxes 3
Datagrid Checkbox Column 17
Rows in Datagrid 12
Datagrid events 1
DataGrid SUCKS! 4
Can't find this one 6
Selecting row from DataGrid 13
Checkboxes and datagrids 1

Back
Top