Format Datagrid

  • Thread starter Thread starter One Handed Man \( OHM - Terry Burns \)
  • Start date Start date
O

One Handed Man \( OHM - Terry Burns \)

You have not added any column styles, so you are trying to reference a style
which does not exist

Style2.GridColumnStyles.Add(

HTH
--

OHM ( Terry Burns )
. . . One-Handed-Man . . .
If U Need My Email ,Ask Me

Time flies when you don't know what you're doing
 
Hi Folks,
Can someone help me out with this code, all i'm trying to do is format my
datagrid so that I have the first col of 120 and the second of 400, but i
keep getting

"An unhandled exception of type 'System.ArgumentOutOfRangeException'
occurred in mscorlib.dll
Aditional information: Index was out of range. Must be non-negative and
less than the size of the collection. " at the * line of code.

Private Sub GenEventLog()

If File.Exists("events.xml") Then
dsEvents.ReadXml("events.xml")
DataGrid2.DataSource = dsEvents.Tables(0)
Else

Dim myXmlTextWriter As XmlTextWriter = New XmlTextWriter("events.xml",
System.Text.Encoding.Unicode)
myXmlTextWriter.Formatting = System.Xml.Formatting.Indented
myXmlTextWriter.WriteStartElement("events")
myXmlTextWriter.WriteStartElement("event")
myXmlTextWriter.WriteElementString("Datetime", Now())
myXmlTextWriter.WriteElementString("Action", "Event log created")
myXmlTextWriter.WriteEndElement()
myXmlTextWriter.WriteEndElement()
myXmlTextWriter.Flush()
myXmlTextWriter.Close()
dsEvents.ReadXml("events.xml")
DataGrid2.DataSource = dsEvents.Tables(0)
End If

Dim Style2 As New DataGridTableStyle
Style2.MappingName = dsEvents.GetType().Name
DataGrid2.TableStyles.Add(Style2)
*DataGrid2.TableStyles(0).GridColumnStyles.Item(0).Width = 120* < error
here.
DataGrid2.TableStyles(0).GridColumnStyles.Item(1).Width = 400

End Sub

Any help Very much appreciated.
Thanks
 
Try this. Without adding the DataGridColumnColumnStyle ( Which must be
Inherited ) you have no styles to reference. Look up the documentation for
more information.

HTH

Class MyColumnStyle
Inherits DataGridColumnStyle


Protected Overrides Sub Abort(ByVal rowNum As Integer)

End Sub

Protected Overrides Function Commit(ByVal dataSource As
System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer) As Boolean

End Function

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)

End Sub

Protected Overrides Function GetMinimumHeight() As Integer

End Function

Protected Overrides Function GetPreferredHeight(ByVal g As
System.Drawing.Graphics, ByVal value As Object) As Integer

End Function

Protected Overrides Function GetPreferredSize(ByVal g As
System.Drawing.Graphics, ByVal value As Object) As System.Drawing.Size

End Function

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)

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 alignToRight As Boolean)

End Sub
End Class


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim Style2 As New DataGridTableStyle

Try
Style2.GridColumnStyles.Add(New MyColumnStyle)
Style2.GridColumnStyles.Add(New MyColumnStyle)

DataGrid2.TableStyles.Add(Style2)
DataGrid2.TableStyles(0).GridColumnStyles.Item(0).Width = 120
DataGrid2.TableStyles(0).GridColumnStyles.Item(1).Width = 400
Catch ex As Exception

MessageBox.Show(ex.Message)
End Try
End Sub

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .
If U Need My Email ,Ask Me

Time flies when you don't know what you're doing
 
Thanks for this, I see what your saying but I think i'm being particularly
thick today as I can't work out what i should be doing with the code you
supplied,
thanks again
Dave
 
OR something like

Friend WithEvents DataGridTextBoxColumn1 As
System.Windows.Forms.DataGridTextBoxColumn

Me.DataGridTextBoxColumn1 = New System.Windows.Forms.DataGridTextBoxColumn

Me.DataGrid2.TableStyles.AddRange(New
System.Windows.Forms.DataGridTableStyle() {Me.DataGridTableStyle1})

Me.DataGridTextBoxColumn1.Width = 75




--

OHM ( Terry Burns )
. . . One-Handed-Man . . .
If U Need My Email ,Ask Me

Time flies when you don't know what you're doing

Dave Edwards said:
Thanks for this, I'll work my way through it and see if i can get it to
work, I don't know if its just me but i find it surprising that its so
complicated and tricky to adjust a column width in a datagrid.

"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in message
Try this. Without adding the DataGridColumnColumnStyle ( Which must be
Inherited ) you have no styles to reference. Look up the documentation for
more information.

HTH

Class MyColumnStyle
Inherits DataGridColumnStyle


Protected Overrides Sub Abort(ByVal rowNum As Integer)

End Sub

Protected Overrides Function Commit(ByVal dataSource As
System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer) As Boolean

End Function

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)

End Sub

Protected Overrides Function GetMinimumHeight() As Integer

End Function

Protected Overrides Function GetPreferredHeight(ByVal g As
System.Drawing.Graphics, ByVal value As Object) As Integer

End Function

Protected Overrides Function GetPreferredSize(ByVal g As
System.Drawing.Graphics, ByVal value As Object) As System.Drawing.Size

End Function

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)

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 alignToRight As Boolean)

End Sub
End Class


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim Style2 As New DataGridTableStyle

Try
Style2.GridColumnStyles.Add(New MyColumnStyle)
Style2.GridColumnStyles.Add(New MyColumnStyle)

DataGrid2.TableStyles.Add(Style2)
DataGrid2.TableStyles(0).GridColumnStyles.Item(0).Width = 120
DataGrid2.TableStyles(0).GridColumnStyles.Item(1).Width = 400
Catch ex As Exception

MessageBox.Show(ex.Message)
End Try
End Sub

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .
If U Need My Email ,Ask Me

Time flies when you don't know what you're doing

Dave Edwards said:
Thanks for this, I see what your saying but I think i'm being
particularly
thick today as I can't work out what i should be doing with the code you
supplied,
thanks again
Dave

"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in message
You have not added any column styles, so you are trying to reference a
style
which does not exist

Style2.GridColumnStyles.Add(

HTH
--

OHM ( Terry Burns )
. . . One-Handed-Man . . .
If U Need My Email ,Ask Me

Time flies when you don't know what you're doing

Hi Folks,
Can someone help me out with this code, all i'm trying to do is
format
my
datagrid so that I have the first col of 120 and the second of 400,
but i
keep getting

"An unhandled exception of type 'System.ArgumentOutOfRangeException'
occurred in mscorlib.dll
Aditional information: Index was out of range. Must be non-negative and
less than the size of the collection. " at the * line of code.

Private Sub GenEventLog()

If File.Exists("events.xml") Then
dsEvents.ReadXml("events.xml")
DataGrid2.DataSource = dsEvents.Tables(0)
Else

Dim myXmlTextWriter As XmlTextWriter = New XmlTextWriter("events.xml",
System.Text.Encoding.Unicode)
myXmlTextWriter.Formatting = System.Xml.Formatting.Indented
myXmlTextWriter.WriteStartElement("events")
myXmlTextWriter.WriteStartElement("event")
myXmlTextWriter.WriteElementString("Datetime", Now())
myXmlTextWriter.WriteElementString("Action", "Event log created")
myXmlTextWriter.WriteEndElement()
myXmlTextWriter.WriteEndElement()
myXmlTextWriter.Flush()
myXmlTextWriter.Close()
dsEvents.ReadXml("events.xml")
DataGrid2.DataSource = dsEvents.Tables(0)
End If

Dim Style2 As New DataGridTableStyle
Style2.MappingName = dsEvents.GetType().Name
DataGrid2.TableStyles.Add(Style2)
*DataGrid2.TableStyles(0).GridColumnStyles.Item(0).Width = 120* <
error
here.
DataGrid2.TableStyles(0).GridColumnStyles.Item(1).Width = 400

End Sub

Any help Very much appreciated.
Thanks
 
Thanks for this, I'll work my way through it and see if i can get it to
work, I don't know if its just me but i find it surprising that its so
complicated and tricky to adjust a column width in a datagrid.

One Handed Man ( OHM - Terry Burns ) said:
Try this. Without adding the DataGridColumnColumnStyle ( Which must be
Inherited ) you have no styles to reference. Look up the documentation for
more information.

HTH

Class MyColumnStyle
Inherits DataGridColumnStyle


Protected Overrides Sub Abort(ByVal rowNum As Integer)

End Sub

Protected Overrides Function Commit(ByVal dataSource As
System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer) As Boolean

End Function

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)

End Sub

Protected Overrides Function GetMinimumHeight() As Integer

End Function

Protected Overrides Function GetPreferredHeight(ByVal g As
System.Drawing.Graphics, ByVal value As Object) As Integer

End Function

Protected Overrides Function GetPreferredSize(ByVal g As
System.Drawing.Graphics, ByVal value As Object) As System.Drawing.Size

End Function

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)

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 alignToRight As Boolean)

End Sub
End Class


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim Style2 As New DataGridTableStyle

Try
Style2.GridColumnStyles.Add(New MyColumnStyle)
Style2.GridColumnStyles.Add(New MyColumnStyle)

DataGrid2.TableStyles.Add(Style2)
DataGrid2.TableStyles(0).GridColumnStyles.Item(0).Width = 120
DataGrid2.TableStyles(0).GridColumnStyles.Item(1).Width = 400
Catch ex As Exception

MessageBox.Show(ex.Message)
End Try
End Sub

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .
If U Need My Email ,Ask Me

Time flies when you don't know what you're doing

Dave Edwards said:
Thanks for this, I see what your saying but I think i'm being
particularly
thick today as I can't work out what i should be doing with the code you
supplied,
thanks again
Dave

"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in message
 

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

Back
Top