Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Select datagridview rows by range or numbers

Posted on 2016-10-27
11
33 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 28

Expert Comment

by:Pawan Kumar
ID: 41861896
So what is your req?
0
 

Author Comment

by:RIAS
ID: 41861900
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
ID: 41861913
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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 28

Assisted Solution

by:Pawan Kumar
Pawan Kumar earned 50 total points
ID: 41861934
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
ID: 41861946
Pawan thanks !  Will try and get back
0
 
LVL 28

Accepted Solution

by:
Ark earned 450 total points
ID: 41861949
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
ID: 41861950
Thanks Ark!
0
 

Author Comment

by:RIAS
ID: 41861964
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 28

Expert Comment

by:Ark
ID: 41861966
DataGridView1.Rows(num-1).Selected = True
0
 

Author Closing Comment

by:RIAS
ID: 41861967
Cheers!
0
 

Author Comment

by:RIAS
ID: 41862066
Ark amazing code!!!
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

838 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