472,131 Members | 1,323 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,131 software developers and data experts.

Sort a Datagridview with code.

Hi, I'm learning to use the Datagridview; I would like sort a column of my
DGV, but with code, not with user click over the column, how make that?; Mi
other question is how
search for a especific chain in my DGV?.

Thanks in advance for any help, I'm using VB.Net in VS2005.

Freddy Coal
Feb 26 '07 #1
3 55981
When you bind your DataGridView to your datasource, use a bindingsource.

Dim nwData as CustomersDataSet = CustomersDataSet.GetCustomers()
m_CustomersGrid.DataSource = m_CustomersBindingSource
m_CustomersBindingSource.DataSource = nwData.Customers

Then you can sort using the BindingSource.

CustomersBindingSource.Sort = "ContactName ASC"

And you can find using the BindingSource.

Dim index as integer = _
CustomersBindingSource.Find("CompanyName", CompanyNameTextBox.Text)
If index <-1 then 'it was found; move to that position
CustomersBindingSource.Position = index
End If
Robin S.
-------------------------------------------
"Freddy Coal" <fr********@gmail.comwrote in message
news:OO*************@TK2MSFTNGP05.phx.gbl...
Hi, I'm learning to use the Datagridview; I would like sort a column of
my
DGV, but with code, not with user click over the column, how make that?;
Mi other question is how
search for a especific chain in my DGV?.

Thanks in advance for any help, I'm using VB.Net in VS2005.

Freddy Coal

Feb 27 '07 #2
Hi Robin, Thanks for your answer, I solve the problem with this code:

DGV.Sort(DGV.Columns(0), System.ComponentModel.ListSortDirection.Ascending)
'Where DGV is a DataGridView

Excuse me, but I don´t understand your second answer; I would like search a
chain (form example "Claymore Street") inside of my DGV; I make that
searching cell by cell with row and columns cycles, but I would like know if
Vb.Net have any instruction for make this search.

Thanks a lot again, your help is very appreciated.

Freddy Coal

"RobinS" <Ro****@NoSpam.yah.nonewrote in message
news:Dq******************************@comcast.com. ..
When you bind your DataGridView to your datasource, use a bindingsource.

Dim nwData as CustomersDataSet = CustomersDataSet.GetCustomers()
m_CustomersGrid.DataSource = m_CustomersBindingSource
m_CustomersBindingSource.DataSource = nwData.Customers

Then you can sort using the BindingSource.

CustomersBindingSource.Sort = "ContactName ASC"

And you can find using the BindingSource.

Dim index as integer = _
CustomersBindingSource.Find("CompanyName", CompanyNameTextBox.Text)
If index <-1 then 'it was found; move to that position
CustomersBindingSource.Position = index
End If
Robin S.
-------------------------------------------
"Freddy Coal" <fr********@gmail.comwrote in message
news:OO*************@TK2MSFTNGP05.phx.gbl...
>Hi, I'm learning to use the Datagridview; I would like sort a column of
my
DGV, but with code, not with user click over the column, how make that?;
Mi other question is how
search for a especific chain in my DGV?.

Thanks in advance for any help, I'm using VB.Net in VS2005.

Freddy Coal

Feb 27 '07 #3
I already answered that question in my previous post. If you want to know
how to do it without using a BindingSource, maybe someone else will answer
you.

Robin S.
----------------------------------------
"Freddy Coal" <fr********@gmail.comwrote in message
news:uH**************@TK2MSFTNGP04.phx.gbl...
Hi Robin, Thanks for your answer, I solve the problem with this code:

DGV.Sort(DGV.Columns(0),
System.ComponentModel.ListSortDirection.Ascending) 'Where DGV is a
DataGridView

Excuse me, but I don´t understand your second answer; I would like search
a chain (form example "Claymore Street") inside of my DGV; I make that
searching cell by cell with row and columns cycles, but I would like know
if Vb.Net have any instruction for make this search.

Thanks a lot again, your help is very appreciated.

Freddy Coal

"RobinS" <Ro****@NoSpam.yah.nonewrote in message
news:Dq******************************@comcast.com. ..
>When you bind your DataGridView to your datasource, use a bindingsource.

Dim nwData as CustomersDataSet = CustomersDataSet.GetCustomers()
m_CustomersGrid.DataSource = m_CustomersBindingSource
m_CustomersBindingSource.DataSource = nwData.Customers

Then you can sort using the BindingSource.

CustomersBindingSource.Sort = "ContactName ASC"

And you can find using the BindingSource.

Dim index as integer = _
CustomersBindingSource.Find("CompanyName", CompanyNameTextBox.Text)
If index <-1 then 'it was found; move to that position
CustomersBindingSource.Position = index
End If
Robin S.
-------------------------------------------
"Freddy Coal" <fr********@gmail.comwrote in message
news:OO*************@TK2MSFTNGP05.phx.gbl...
>>Hi, I'm learning to use the Datagridview; I would like sort a column of
my
DGV, but with code, not with user click over the column, how make
that?; Mi other question is how
search for a especific chain in my DGV?.

Thanks in advance for any help, I'm using VB.Net in VS2005.

Freddy Coal


Feb 27 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by William LaMartin | last post: by
3 posts views Thread by joey.powell | last post: by
2 posts views Thread by Claes Wedin | last post: by
reply views Thread by leo001 | last post: by

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.