Solved

Select datagridview rows by range or numbers

Posted on 2016-10-27
11
22 Views
Last Modified: 2016-10-27
Hello,

I have textbox where user can input numbers like 3.4,7,8 or add a range like 6-12 for selection of datagridview rows.
Any suggestions on how can i achieve this.

Cheers
0
Comment
Question by:RIAS
  • 6
  • 2
  • 2
  • +1
11 Comments
 
LVL 17

Expert Comment

by:Pawan Kumar Khowal
Comment Utility
So what is your req?
0
 

Author Comment

by:RIAS
Comment Utility
Select the rows as per user input

example:

3.4,7,8 or add a range like 6-12
0
 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
Use string.split and split on the comma.
Look at each string in the array.
If it does NOT contain a hyphen then convert to a number (index of row) and select that row
else split into two strings at the hyphen.  The first is the index of the start row to select, the second is the index of the row to stop selecting at.  You will probably require a loop to achieve that - or should I not mention that nasty word  ;-)
1
 
LVL 17

Assisted Solution

by:Pawan Kumar Khowal
Pawan Kumar Khowal earned 50 total points
Comment Utility
Try...


int index = 2
string val1 =  dataset.Tables[0].Rows[index][Col_1].ToString();



string s = "1,2,3";
string[] values = s.Split(',');
for(int Counter = 0; i < values.Length; Counter++)
{
   string a = dataset.Tables[0].Rows[Counter][Col_1].ToString();
}


string s = "1-6";
--Here get value 1 & 6 using substring function -- s.Substring(0,1);
for(int Counter = 1; i <= 6; Counter++)
{
   string a = dataset.Tables[0].Rows[Counter][Col_1].ToString();
}
1
 

Author Comment

by:RIAS
Comment Utility
Pawan thanks !  Will try and get back
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 27

Accepted Solution

by:
Ark earned 450 total points
Comment Utility
Private Function GetNumbers(input As String) As List(Of Integer)
    Dim lst As New List(Of Integer)
    Dim comma = input.Split(New Char() {","c}, StringSplitOptions.RemoveEmptyEntries)
    For Each s In comma
        Dim dash = s.Split(New Char() {"-"c}, StringSplitOptions.RemoveEmptyEntries)
        If dash.Length = 0 OrElse dash.Length > 2 Then Continue For
        Dim i1 As Integer
        If Not Integer.TryParse(dash(0).Trim, i1) Then Continue For
        If dash.Length = 1 Then
            lst.Add(i1)
        ElseIf dash.Length = 2 Then
            Dim i2 As Integer
            If Not Integer.TryParse(dash(1).Trim, i2) Then Continue For
            lst.AddRange(Enumerable.Range(i1, i2 - i1 + 1))
        End If
    Next
    Return lst
End Function

Open in new window

Using:
DataGridView1.ClearSelection()
For Each num In GetNumbers("1,2 - 5,8,10 - 15")
    DataGridView1.Rows(num).Selected = True
Next

Open in new window

1
 

Author Comment

by:RIAS
Comment Utility
Thanks Ark!
0
 

Author Comment

by:RIAS
Comment Utility
Ark,It works perfectly mate, but slight issue regarding the numbers
As in dotnet the datagrid starts with 0
The user will input something like 1-10 which is actuallly  0-9 for the dsatragridview to select .
Any suggestions?
0
 
LVL 27

Expert Comment

by:Ark
Comment Utility
DataGridView1.Rows(num-1).Selected = True
0
 

Author Closing Comment

by:RIAS
Comment Utility
Cheers!
0
 

Author Comment

by:RIAS
Comment Utility
Ark amazing code!!!
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now