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

A couple of Datagrid questions

EdB
1) How do I make the contents of a Windows datagrid editable while at the
same time preventing new rows from being added?

2) How do I align the contents of a column align-right, but the header of
that same column align-left?
Nov 21 '05 #1
5 1119
Hi,

1) Dataset.tables("MyTable").defaultview.allownew=fal se
http://msdn.microsoft.com/library/de...ownewtopic.asp

2) you need to make your own column style for that. Here is an example of
one I wrote. Alignment is the header. Dataalignment is the grid cell.

Public Class HeaderAndDataAlignColumn

Inherits DataGridTextBoxColumn

Private mTxtAlign As HorizontalAlignment = HorizontalAlignment.Left

Private mDrawTxt As New StringFormat

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)

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

MyBase.TextBox.TextAlign = mTxtAlign

MyBase.TextBox.CharacterCasing = CharacterCasing.Upper

End Sub

Protected Overloads Overrides Sub Paint(ByVal g As System.Drawing.Graphics,
ByVal bounds As System.Drawing.Rectangle, ByVal source As
System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal
backBrush As System.Drawing.Brush, ByVal foreBrush As System.Drawing.Brush,
ByVal alignToRight As Boolean)

'clear the cell

g.FillRectangle(backBrush, bounds)

'draw the value

Dim s As String = Me.GetColumnValueAtRow([source], rowNum).ToString()

Dim r As Rectangle = bounds

r.Inflate(0, -1)

g.DrawString(s, MyBase.TextBox.Font, foreBrush, RectangleF.op_Implicit(r), _

mDrawTxt)

End Sub

Public Property DataAlignment() As HorizontalAlignment

Get

Return mTxtAlign

End Get

Set(ByVal Value As HorizontalAlignment)

mTxtAlign = Value

If mTxtAlign = HorizontalAlignment.Center Then

mDrawTxt.Alignment = StringAlignment.Center

ElseIf mTxtAlign = HorizontalAlignment.Right Then

mDrawTxt.Alignment = StringAlignment.Far

Else

mDrawTxt.Alignment = StringAlignment.Near

End If

End Set

End Property

End Class


Ken
--------------------------------
"EdB" <Ed*@discussions.microsoft.com> wrote in message
news:CF**********************************@microsof t.com...
1) How do I make the contents of a Windows datagrid editable while at the
same time preventing new rows from being added?

2) How do I align the contents of a column align-right, but the header of
that same column align-left?

Nov 21 '05 #2
I tried to understand you example but couldn't see how the header was
aligned, only appears as if the data was aligned. Would appreciate some
insight on how the header gets aligned to "alignment" in the example.

"Ken Tucker [MVP]" wrote:
Hi,

1) Dataset.tables("MyTable").defaultview.allownew=fal se
http://msdn.microsoft.com/library/de...ownewtopic.asp

2) you need to make your own column style for that. Here is an example of
one I wrote. Alignment is the header. Dataalignment is the grid cell.

Public Class HeaderAndDataAlignColumn

Inherits DataGridTextBoxColumn

Private mTxtAlign As HorizontalAlignment = HorizontalAlignment.Left

Private mDrawTxt As New StringFormat

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)

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

MyBase.TextBox.TextAlign = mTxtAlign

MyBase.TextBox.CharacterCasing = CharacterCasing.Upper

End Sub

Protected Overloads Overrides Sub Paint(ByVal g As System.Drawing.Graphics,
ByVal bounds As System.Drawing.Rectangle, ByVal source As
System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal
backBrush As System.Drawing.Brush, ByVal foreBrush As System.Drawing.Brush,
ByVal alignToRight As Boolean)

'clear the cell

g.FillRectangle(backBrush, bounds)

'draw the value

Dim s As String = Me.GetColumnValueAtRow([source], rowNum).ToString()

Dim r As Rectangle = bounds

r.Inflate(0, -1)

g.DrawString(s, MyBase.TextBox.Font, foreBrush, RectangleF.op_Implicit(r), _

mDrawTxt)

End Sub

Public Property DataAlignment() As HorizontalAlignment

Get

Return mTxtAlign

End Get

Set(ByVal Value As HorizontalAlignment)

mTxtAlign = Value

If mTxtAlign = HorizontalAlignment.Center Then

mDrawTxt.Alignment = StringAlignment.Center

ElseIf mTxtAlign = HorizontalAlignment.Right Then

mDrawTxt.Alignment = StringAlignment.Far

Else

mDrawTxt.Alignment = StringAlignment.Near

End If

End Set

End Property

End Class


Ken
--------------------------------
"EdB" <Ed*@discussions.microsoft.com> wrote in message
news:CF**********************************@microsof t.com...
1) How do I make the contents of a Windows datagrid editable while at the
same time preventing new rows from being added?

2) How do I align the contents of a column align-right, but the header of
that same column align-left?

Nov 21 '05 #3
Hi,

Here is an example
http://www.onteorasoftware.com/downl...todatagrid.zip

Ken
------------------
"Dennis" <De****@discussions.microsoft.com> wrote in message
news:76**********************************@microsof t.com...
I tried to understand you example but couldn't see how the header was
aligned, only appears as if the data was aligned. Would appreciate some
insight on how the header gets aligned to "alignment" in the example.

"Ken Tucker [MVP]" wrote:
Hi,

1) Dataset.tables("MyTable").defaultview.allownew=fal se
http://msdn.microsoft.com/library/de...ownewtopic.asp

2) you need to make your own column style for that. Here is an example
of
one I wrote. Alignment is the header. Dataalignment is the grid cell.

Public Class HeaderAndDataAlignColumn

Inherits DataGridTextBoxColumn

Private mTxtAlign As HorizontalAlignment = HorizontalAlignment.Left

Private mDrawTxt As New StringFormat

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)

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

MyBase.TextBox.TextAlign = mTxtAlign

MyBase.TextBox.CharacterCasing = CharacterCasing.Upper

End Sub

Protected Overloads Overrides Sub Paint(ByVal g As
System.Drawing.Graphics,
ByVal bounds As System.Drawing.Rectangle, ByVal source As
System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal
backBrush As System.Drawing.Brush, ByVal foreBrush As
System.Drawing.Brush,
ByVal alignToRight As Boolean)

'clear the cell

g.FillRectangle(backBrush, bounds)

'draw the value

Dim s As String = Me.GetColumnValueAtRow([source], rowNum).ToString()

Dim r As Rectangle = bounds

r.Inflate(0, -1)

g.DrawString(s, MyBase.TextBox.Font, foreBrush, RectangleF.op_Implicit(r),
_

mDrawTxt)

End Sub

Public Property DataAlignment() As HorizontalAlignment

Get

Return mTxtAlign

End Get

Set(ByVal Value As HorizontalAlignment)

mTxtAlign = Value

If mTxtAlign = HorizontalAlignment.Center Then

mDrawTxt.Alignment = StringAlignment.Center

ElseIf mTxtAlign = HorizontalAlignment.Right Then

mDrawTxt.Alignment = StringAlignment.Far

Else

mDrawTxt.Alignment = StringAlignment.Near

End If

End Set

End Property

End Class


Ken
--------------------------------
"EdB" <Ed*@discussions.microsoft.com> wrote in message
news:CF**********************************@microsof t.com...
1) How do I make the contents of a Windows datagrid editable while at the
same time preventing new rows from being added?

2) How do I align the contents of a column align-right, but the header of
that same column align-left?

Nov 21 '05 #4
Ken,

If you use your code, the column does not retain it's format. Setting
col.Format = "n0" has no affect on the column. Any ideas?

"Ken Tucker [MVP]" wrote:
Hi,

Here is an example
http://www.onteorasoftware.com/downl...todatagrid.zip

Ken
------------------
"Dennis" <De****@discussions.microsoft.com> wrote in message
news:76**********************************@microsof t.com...
I tried to understand you example but couldn't see how the header was
aligned, only appears as if the data was aligned. Would appreciate some
insight on how the header gets aligned to "alignment" in the example.

"Ken Tucker [MVP]" wrote:
Hi,

1) Dataset.tables("MyTable").defaultview.allownew=fal se
http://msdn.microsoft.com/library/de...ownewtopic.asp

2) you need to make your own column style for that. Here is an example
of
one I wrote. Alignment is the header. Dataalignment is the grid cell.

Public Class HeaderAndDataAlignColumn

Inherits DataGridTextBoxColumn

Private mTxtAlign As HorizontalAlignment = HorizontalAlignment.Left

Private mDrawTxt As New StringFormat

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)

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

MyBase.TextBox.TextAlign = mTxtAlign

MyBase.TextBox.CharacterCasing = CharacterCasing.Upper

End Sub

Protected Overloads Overrides Sub Paint(ByVal g As
System.Drawing.Graphics,
ByVal bounds As System.Drawing.Rectangle, ByVal source As
System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal
backBrush As System.Drawing.Brush, ByVal foreBrush As
System.Drawing.Brush,
ByVal alignToRight As Boolean)

'clear the cell

g.FillRectangle(backBrush, bounds)

'draw the value

Dim s As String = Me.GetColumnValueAtRow([source], rowNum).ToString()

Dim r As Rectangle = bounds

r.Inflate(0, -1)

g.DrawString(s, MyBase.TextBox.Font, foreBrush, RectangleF.op_Implicit(r),
_

mDrawTxt)

End Sub

Public Property DataAlignment() As HorizontalAlignment

Get

Return mTxtAlign

End Get

Set(ByVal Value As HorizontalAlignment)

mTxtAlign = Value

If mTxtAlign = HorizontalAlignment.Center Then

mDrawTxt.Alignment = StringAlignment.Center

ElseIf mTxtAlign = HorizontalAlignment.Right Then

mDrawTxt.Alignment = StringAlignment.Far

Else

mDrawTxt.Alignment = StringAlignment.Near

End If

End Set

End Property

End Class


Ken
--------------------------------
"EdB" <Ed*@discussions.microsoft.com> wrote in message
news:CF**********************************@microsof t.com...
1) How do I make the contents of a Windows datagrid editable while at the
same time preventing new rows from being added?

2) How do I align the contents of a column align-right, but the header of
that same column align-left?


Nov 21 '05 #5
Hi,

Private Sub SetupGrid()

Dim ts As New DataGridTableStyle

ts.MappingName = "ArrayList"

Dim colDescription As New MultiLineColumn

With colDescription

..MappingName = "Col2"

..HeaderText = "Description"

..Width = 180

End With

ts.GridLineColor = Color.Red

ts.GridLineStyle = DataGridLineStyle.Solid

Dim cm As CurrencyManager = CType(Me.BindingContext(DataGrid1.DataSource),
CurrencyManager)

Dim pd As System.ComponentModel.PropertyDescriptor =
cm.GetItemProperties()("Qty")

Dim ni As New System.Globalization.NumberFormatInfo

ni.NumberDecimalDigits = 3

Dim colQty As New DataGridTextBoxColumn(pd, "f")

With colQty

..MappingName = "Col1"

..HeaderText = "Qty"

..Width = 75

..FormatInfo = ni

End With

Dim collink As New HyperLinkColumn

With collink

..MappingName = "LinkColumn"

..HeaderText = "Link"

..Width = 150

End With

ts.GridColumnStyles.Add(colDescription)

ts.GridColumnStyles.Add(colQty)

ts.GridColumnStyles.Add(collink)

DataGrid1.TableStyles.Add(ts)

ts = Nothing

colQty = Nothing

colDescription = Nothing

End Sub

Ken

-----------------------

"blue_nirvana" <bl*********@discussions.microsoft.com> wrote in message
news:E0**********************************@microsof t.com...
Ken,

If you use your code, the column does not retain it's format. Setting
col.Format = "n0" has no affect on the column. Any ideas?

"Ken Tucker [MVP]" wrote:
Hi,

Here is an example
http://www.onteorasoftware.com/downl...todatagrid.zip

Ken
------------------
"Dennis" <De****@discussions.microsoft.com> wrote in message
news:76**********************************@microsof t.com...
I tried to understand you example but couldn't see how the header was
aligned, only appears as if the data was aligned. Would appreciate some
insight on how the header gets aligned to "alignment" in the example.

"Ken Tucker [MVP]" wrote:
Hi,

1) Dataset.tables("MyTable").defaultview.allownew=fal se
http://msdn.microsoft.com/library/de...ownewtopic.asp

2) you need to make your own column style for that. Here is an example
of
one I wrote. Alignment is the header. Dataalignment is the grid cell.

Public Class HeaderAndDataAlignColumn

Inherits DataGridTextBoxColumn

Private mTxtAlign As HorizontalAlignment = HorizontalAlignment.Left

Private mDrawTxt As New StringFormat

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)

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

MyBase.TextBox.TextAlign = mTxtAlign

MyBase.TextBox.CharacterCasing = CharacterCasing.Upper

End Sub

Protected Overloads Overrides Sub Paint(ByVal g As
System.Drawing.Graphics,
ByVal bounds As System.Drawing.Rectangle, ByVal source As
System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal
backBrush As System.Drawing.Brush, ByVal foreBrush As
System.Drawing.Brush,
ByVal alignToRight As Boolean)

'clear the cell

g.FillRectangle(backBrush, bounds)

'draw the value

Dim s As String = Me.GetColumnValueAtRow([source], rowNum).ToString()

Dim r As Rectangle = bounds

r.Inflate(0, -1)

g.DrawString(s, MyBase.TextBox.Font, foreBrush,
RectangleF.op_Implicit(r),
_

mDrawTxt)

End Sub

Public Property DataAlignment() As HorizontalAlignment

Get

Return mTxtAlign

End Get

Set(ByVal Value As HorizontalAlignment)

mTxtAlign = Value

If mTxtAlign = HorizontalAlignment.Center Then

mDrawTxt.Alignment = StringAlignment.Center

ElseIf mTxtAlign = HorizontalAlignment.Right Then

mDrawTxt.Alignment = StringAlignment.Far

Else

mDrawTxt.Alignment = StringAlignment.Near

End If

End Set

End Property

End Class


Ken
--------------------------------
"EdB" <Ed*@discussions.microsoft.com> wrote in message
news:CF**********************************@microsof t.com...
1) How do I make the contents of a Windows datagrid editable while at
the
same time preventing new rows from being added?

2) How do I align the contents of a column align-right, but the header
of
that same column align-left?


Nov 21 '05 #6

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

Similar topics

3
by: Dmitry Akselrod | last post by:
Hi, I am using a standard .NET 1.0 Datagrid. Actually, I am using two, bound to a master-detail relationship. Both DataGrids are bound to a datasource. I have the MSDN version of the...
7
by: Matthew Wieder | last post by:
Hi - I have a datagrid that has a black header and the rows alternate white and gray. The problem is that until items are added to the grid, the grid appears as a large black rectangle, which is...
5
by: VB Programmer | last post by:
I have an ASP.NET form that allows people to answer survey questions. There could be from 5 to an unlimited amt of questions, depending on which questions the admin wants to users to answer (he...
3
by: jy836 | last post by:
1) When using images for an application (such as in an ImageList or as the image for a button), are the images embedded in the application or must the image files be included in the setup package?...
10
by: JohnR | last post by:
I have a datatable as the datasource to a datagrid. The datagrid has a datagridtablestyle defined. I use the datagridtablestyle to change the order of the columns (so they can be different than...
3
by: melton9 | last post by:
I'm just getting into using datagrid and have a couple of questions. 1.)How do you get the grid to show the values of a datatable automatically? Currently I have to hit the + sign and then...
0
by: Newish | last post by:
Hi Couple of questions on datagrid 1) Is there a performance issue when using datagrid to display data from a datatable. 2) Is there a security issue when using datagrid to display data...
9
by: Tony Girgenti | last post by:
Hello. I'm developing and testing a web application using VS.NET 2003, VB, .NET Framework 1.1.4322, ASP.NET 1.1.4322 and IIS5.1 on a WIN XP Pro, SP2 computer. I'm using a web form. For a...
9
by: rn5a | last post by:
A Form has a DataGrid which displays records from a SQL Server 2005 DB table. Users can modify the records using this DataGrid for which I am using EditCommandColumn in the DataGrid. This is the...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
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...
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: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
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...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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

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.