Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# .NET4
Hello,
 
I want to split one datagridview column value in six variable,
 
My Datagridview columna like this:-
 
"\Library\Daino Royal - A.jpg,\Library\Abc.jpg,\Library\DEF.jpg,\Library\GHI.jpg,\Library\TIVOLI_GREY_A.jpg,\Library\URBANA_GREY - C.jpg"
 
I want to fill only ".jpg" file in six variable like,
string a = "Abc.jpg";
So, Please help me how can we split one datagridview column value?
 
Thanks
Ankit Agarwal
Software Engineer
Posted 10-Apr-13 3:17am
v2
Comments
ThePhantomUpvoter at 10-Apr-13 8:20am
   
Okay so use Split, SubString or a regular expression.
ankit.agg1984 at 10-Apr-13 8:22am
   
but how, can you give me the code according to datagridview column value split?
PrashantSonewane at 10-Apr-13 8:30am
   
use something like this:
 
string str = DataGrid1.Column(X).Value;
 
str.Replace("\\Library\\","");
string[] lstImage = str.Split(',');
string img1 = lstImage[0].ToString();
string img2 = lstImage[1].ToString();
string img3 = lstImage[2].ToString();
string img4 = lstImage[3].ToString();
string img5 = lstImage[4].ToString();
string img6 = lstImage[5].ToString();
 
However \ will give you some trouble working with in C# as it is escape char. Check on that.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Here's one way of doing it ...
 
// copy the dgv value to a string e.g.
string dgvCell = dataGridView1.Rows[0].Cells[0].Value.ToString();
 
// Remove the /libary bits
dgvCell = dgvCell.Replace(@"\Library\", "");
 
//Use string.split to split on commas, store results in string array
string[] jpgList = dgvCell.Split(',');
 
// Do something with the results e.g. ...
foreach (string j in jpgList)
    if(j.Contains("jpg"))
        Debug.Print(j);
  Permalink  
v2
Comments
ankit.agg1984 at 10-Apr-13 8:53am
   
I need only image name .jpg,
I need not \Library\
ankit.agg1984 at 10-Apr-13 8:55am
   
I need only images name.
Daino Royal - A.jpg
Abc.jpg
DEF.jpg
GHI.jpg
TIVOLI_GREY_A.jpg
URBANA_GREY - C.jpg
CHill60 at 10-Apr-13 9:07am
   
I've added a call to .Replace to my solution - also see comment from PrashantSonewane
ankit.agg1984 at 10-Apr-13 9:34am
   
I have also commented PrashantSonewane's solutions before use your solution but did not run on my condition.
This code displayed "\Library\Abc.jpg"
CHill60 at 10-Apr-13 9:48am
   
Ah yes - he's using WPF I think. However, my revised solution listed the jpg files as per your comment when I tested it - is it not working for you?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

also try this regex code
string s = @"\Library\Daino Royal - A.jpg,\Library\Abc.jpg,\Library\DEF.jpg,\Library\GHI.jpg,\Library\TIVOLI_GREY_A.jpg,\Library\URBANA_GREY - C.jpg";
 MatchCollection matches = Regex.Matches(s, @"(\w*.jpg)");
// or use MatchCollection matches = Regex.Matches(s, @"(\w*?\s*?-?\s*?\w*.jpg)");
//or use MatchCollection matches = Regex.Matches(s, @"((\w*?\s*?-?\s*?){0,}\w*.jpg)");
 string[] imgFileName = new string[matches.Count];
 int i=0;
 foreach (Match match in matches)
{
   imgFileName[i] = match.Value;
   MessageBox.Show(match.Value);
   i++;
}
  Permalink  
v5
Comments
CHill60 at 10-Apr-13 11:22am
   
URBANA_GREY - C.jpg is being returned as C.jpg for me
Pallavi Waikar at 10-Apr-13 12:00pm
   
for that use regex as
MatchCollection matches = Regex.Matches(s, @"(\w*?\s*?-?\s*?\w*.jpg)");
CHill60 at 10-Apr-13 12:27pm
   
Yep - that's better. But also "Daino Royal - A.jpg" still returned as "Royal - A.jpg"
Pallavi Waikar at 10-Apr-13 12:51pm
   
ok then try this one
MatchCollection matches = Regex.Matches(s, @"((\w*?\s*?-?\s*?){0,}\w*.jpg)");
Pallavi Waikar at 10-Apr-13 12:55pm
   
this will also work
string[] arr = @"\Library\Daino Royal - A.jpg,\Library\Abc.jpg,\Library\DEF.jpg,\Library\GHI.jpg,\Library\TIVOLI_GREY_A.jpg,\Library\URBANA_GREY - C.jpg".Replace(",", "").Replace("\\Library\\", ",").Remove(0, 1).Split(',');
foreach (string m in arr)
{
MessageBox.Show(m);
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 277
1 Maciej Los 210
2 BillWoodruff 205
3 Jochen Arndt 180
4 Sergey Alexandrovich Kryukov 165
0 OriginalGriff 5,130
1 DamithSL 4,157
2 Maciej Los 3,650
3 Kornfeld Eliyahu Peter 3,460
4 Sergey Alexandrovich Kryukov 2,811


Advertise | Privacy | Mobile
Web03 | 2.8.141216.1 | Last Updated 10 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100