PC Review


Reply
Thread Tools Rate Thread

A couple of Datagrid questions

 
 
=?Utf-8?B?RWRC?=
Guest
Posts: n/a
 
      22nd Oct 2004
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?


 
Reply With Quote
 
 
 
 
Ken Tucker [MVP]
Guest
Posts: n/a
 
      22nd Oct 2004
Hi,

1) Dataset.tables("MyTable").defaultview.allownew=false
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" <(E-Mail Removed)> wrote in message
news:CF300430-B251-4C69-BAD3-(E-Mail Removed)...
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?



 
Reply With Quote
 
=?Utf-8?B?RGVubmlz?=
Guest
Posts: n/a
 
      23rd Oct 2004
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=false
> 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" <(E-Mail Removed)> wrote in message
> news:CF300430-B251-4C69-BAD3-(E-Mail Removed)...
> 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?
>
>
>
>

 
Reply With Quote
 
Ken Tucker [MVP]
Guest
Posts: n/a
 
      23rd Oct 2004
Hi,

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

Ken
------------------
"Dennis" <(E-Mail Removed)> wrote in message
news:763427C3-C661-4421-AAD5-(E-Mail Removed)...
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=false
> 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" <(E-Mail Removed)> wrote in message
> news:CF300430-B251-4C69-BAD3-(E-Mail Removed)...
> 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?
>
>
>
>



 
Reply With Quote
 
=?Utf-8?B?Ymx1ZV9uaXJ2YW5h?=
Guest
Posts: n/a
 
      15th Feb 2005
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" <(E-Mail Removed)> wrote in message
> news:763427C3-C661-4421-AAD5-(E-Mail Removed)...
> 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=false
> > 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" <(E-Mail Removed)> wrote in message
> > news:CF300430-B251-4C69-BAD3-(E-Mail Removed)...
> > 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?
> >
> >
> >
> >

>
>
>

 
Reply With Quote
 
Ken Tucker [MVP]
Guest
Posts: n/a
 
      16th Feb 2005
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" <(E-Mail Removed)> wrote in message
news:E03A3DFB-310E-471C-B3F4-(E-Mail Removed)...
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" <(E-Mail Removed)> wrote in message
> news:763427C3-C661-4421-AAD5-(E-Mail Removed)...
> 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=false
> > 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" <(E-Mail Removed)> wrote in message
> > news:CF300430-B251-4C69-BAD3-(E-Mail Removed)...
> > 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?
> >
> >
> >
> >

>
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Datagrid couple of questions Newish Microsoft ASP .NET 0 27th Sep 2006 04:45 PM
A couple of easy datagrid questions melton9@hotmail.com Microsoft VB .NET 3 24th Apr 2006 03:59 PM
A couple of questions... littlegreenmen1 Microsoft Excel Misc 0 10th Jun 2005 09:40 PM
Couple of OL3 questions John Smith Microsoft Outlook 0 26th Apr 2005 07:19 PM
A couple of questions - MVP help... buRford Windows XP Help 6 11th Nov 2004 09:51 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:20 PM.