By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,851 Members | 1,109 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,851 IT Pros & Developers. It's quick & easy.

Multiple questions on datagrid

P: n/a
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
Nov 21 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Kepja,

Windowforms datagrid or Webform Datagrid?

Cor

"Kejpa" <kS*******@saj.fi>
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

Nov 21 '05 #2

P: n/a
Sorry for the confusion, Windowsforms Datagrid.
Nov 21 '05 #3

P: n/a
> 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
Nov 21 '05 #4

P: n/a
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

"Cor Ligthert" <no************@planet.nl> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
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

Nov 21 '05 #5

P: n/a
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

"Cor Ligthert" <no************@planet.nl> wrote in message
news:#1**************@TK2MSFTNGP14.phx.gbl...
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

Nov 21 '05 #6

P: n/a
Kepja,

In that same columnstyle as the width there is the possibiltity to set the

http://msdn.microsoft.com/library/de...ltexttopic.asp

I hope this helps?

Cor

"Kejpa" <kS*******@saj.fi> schreef in bericht
news:cl**********@gandalf.alcom.aland.fi...
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

"Cor Ligthert" <no************@planet.nl> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
> 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


Nov 21 '05 #7

P: n/a
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

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

"Kejpa" <kS*******@saj.fi> wrote in message
news:cl**********@gandalf.alcom.aland.fi...
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

Nov 21 '05 #8

P: n/a
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.

"Cor Ligthert" wrote:
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

Nov 21 '05 #9

P: n/a
Thank you all!
I've sorted this out thanks to you.

/Kejpa
Nov 21 '05 #10

P: n/a
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
Nov 21 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.