Multiple questions on datagrid

K

Kejpa

Hi,
I've just started using the datagrid and I have a few questions on it...
1. How do you autosize the columns to fit the content programmatically? When
you double click between two columns you'll get the left one autosized to
it's content, I want to do that when I load the grid with data.

2. How do you sort the grid using a command button? When you click the
column header you will get the grid sorted in ascending/descending order, my
users require to do this with a button.

3. How do I navigate to the new record in the grid? My users require that
there is a button "New record" that will set focus to the * marked row's
first column.

Any help appreciated
Regards
Kejpa
 
C

Cor Ligthert

I've just started using the datagrid and I have a few questions on it...
1. How do you autosize the columns to fit the content programmatically?
When
you double click between two columns you'll get the left one autosized to
it's content, I want to do that when I load the grid with data.

Using column styles
Dim g As Graphics = Graphics.FromHwnd(datagrid1.Handle)
column.width = Cint(10 * CInt(g.MeasureString("ABCDEFGHIJKLMNOPQRSTUVWXYZ",
Me.Font).Width / 26.0!))
2. How do you sort the grid using a command button? When you click the
column header you will get the grid sorted in ascending/descending order,
my
users require to do this with a button.

You use a dataview as the datasource and set than the sort to the right
column
dim dv as new dataview(mytable)
dv.sort = whatever
datagrid1.datasource = dv
When you renew it you have first to set the datasource to nothing
3. How do I navigate to the new record in the grid? My users require that
there is a button "New record" that will set focus to the * marked row's
first column.

Try it by making yourself a new datagrid by inheriting it, without the *
question it is easy, so probably hard to find as a ready made datagrid on
internet.

I hope this helps?

Cor
 
K

Kejpa

Thanx Cor!

Yet another one though...
When I set a cell to DBNull.Value the text isn't updated until I change
cell, I've tried to set it to "(null)" and "" but when the filed's numeric
an error is thrown :(
I want the cell content to somehow react on the value set.

Thanks again!
/Kejpa
 
R

Rajesh R

For Qn 3.
3. How do I navigate to the new record in the grid? My users require that
there is a button "New record" that will set focus to the * marked row's
first column.

Try this


DataGrid1.CurrentCell = New DataGridCell(DataGrid1.DataSource.Rows.Count, 0)

+Rajesh R
 
K

Ken Tucker [MVP]

Hi,


1) Autowidth adjusting datagrid column I wrote. Use this in your table
style instead of the datagridtextbox column

Public Class AutoWidthColumn

Inherits DataGridTextBoxColumn

Dim dg As DataGrid

Dim cm As CurrencyManager



Private Sub SetWidth()

Dim intWidth As Integer = Me.Width

Dim intRow As Integer

Dim sz As Size = dg.Size

Dim g As Graphics = dg.CreateGraphics

For intRow = 0 To cm.Count - 1

Dim s As String

Dim i As Integer

s = Me.GetColumnValueAtRow(cm, intRow).ToString()

i = g.MeasureString(s, Me.TextBox.Font).Width + 10

If i > intWidth Then

Debug.WriteLine(i.ToString)

intWidth = i

End If

Next

Me.Width = intWidth

g.Dispose()

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)

Static bPainted As Boolean = False

If Not bPainted Then

dg = Me.DataGridTableStyle.DataGrid

cm = source

SetWidth()

bPainted = True

Me.TextBox.Multiline = False

AddHandler Me.TextBox.Validating, AddressOf TextValidating

End If

MyBase.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight)

End Sub

Private Sub TextValidating(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs)

Dim s As String = Me.TextBox.Text

Dim g As Graphics = dg.CreateGraphics

Dim iWidth As Integer = g.MeasureString(s, dg.Font).Width + 10

g.Dispose()

If iWidth > Me.Width Then

Me.Width = iWidth

Else

SetWidth()

End If

End Sub

End Class



2) If you are bound to a datatable use datatable.defaultview.sort =
"ColumnName"



3)

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

DataGrid1.CurrentCell = New DataGridCell(cm.Count, 0)





Ken

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

Hi,
I've just started using the datagrid and I have a few questions on it...
1. How do you autosize the columns to fit the content programmatically? When
you double click between two columns you'll get the left one autosized to
it's content, I want to do that when I load the grid with data.

2. How do you sort the grid using a command button? When you click the
column header you will get the grid sorted in ascending/descending order, my
users require to do this with a button.

3. How do I navigate to the new record in the grid? My users require that
there is a button "New record" that will set focus to the * marked row's
first column.

Any help appreciated
Regards
Kejpa
 
G

Guest

Cor, I am a bit confused with the column.Width setting. Does the
Graphics.fromHwnd (datagrid1.Handle) return the selected column? I assume
you have to then set the column.width of the appropriate GridcolumnStyle. Is
this correct.

Also, can one use the DataGrid.Refresh method to update the display after
sorting? I know it works for a DataGrid bound to an arraylist.
 
C

Cor Ligthert

Dennis,

The graphics from handle returns only the layer where a string which has to
be measured can be painted on to know the avarage width of a used character
(which is not even exact) that is all.
Also, can one use the DataGrid.Refresh method to update the display after
sorting? I know it works for a DataGrid bound to an arraylist.

I thought that this is a problem, which needs to set the datasource to
nothing and than add the sorted datasource again.

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

Top