473,397 Members | 2,033 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,397 software developers and data experts.

Multiple questions on datagrid

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
10 1404
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
Sorry for the confusion, Windowsforms Datagrid.
Nov 21 '05 #3
> 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
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
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
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
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
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
Thank you all!
I've sorted this out thanks to you.

/Kejpa
Nov 21 '05 #10
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Billy Jacobs | last post by:
I am using a datagrid to display some data. I need to create 2 header rows for this grid with columns of varying spans. In html it would be the following. <Table> <tr> <td colspan=8>Official...
0
by: Phil Townsend | last post by:
I am building an online testing application that uses a usercontrol to navigate through a list of questions. The questions are multiple choice. I have a stored procedure that returns two tables:...
1
by: Craig Banks | last post by:
If a row of data in a dataset has a lot of columns the row displaying the data in a datagrid will run way off the screen. What I'd like to do is display a row of data over several datagrid rows so...
2
by: Joey | last post by:
Guys, How can I add multiple parameters to a url in the format string property of a hyperlinkcolumn in a datagrid Thanks
5
by: Lie | last post by:
Hi all, I have problem in getting selectedindex of multiple listbox selection in a datagrid. I have a listbox with multiple selection mode inside datagrid. In Edit mode, I need to get back all...
1
by: sneha123 | last post by:
There will be some 20 questions and for each question there will be 4 choices.what i want to do is to select multiple answers by clicking the checkbox. i m using asp.net,vb.net pls help me we...
1
by: Bob Loveshade | last post by:
I am looking for an example that shows how to select and highlight multiple rows in a DataGrid. My DataGrid is part of a Web User Control which is contained in an ASPX page. I haven't been...
4
by: Dave Edwards | last post by:
I understand that I can fill a datagrid with multiple queries, but I cannot figure out how to fill a dataset with the same query but run against multiple SQL servers, the query , table structure...
0
by: dbuchanan | last post by:
How do I make multiple DataGridTableStyles serve the same table? (I can get multiple styles for the same datagrid, but not multple styles for the same *table* on the same datagrid) I want to do...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.