Click here to Skip to main content
11,802,762 members (71,772 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C# .NET .NET4

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?

Ankit Agarwal
Software Engineer
Posted 10-Apr-13 2:17am
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;

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
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)
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
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
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;
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)

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

  Print Answers RSS
0 KrunalRohit 366
1 OriginalGriff 365
2 F-ES Sitecore 295
3 CPallini 270
4 Sergey Alexandrovich Kryukov 239
0 OriginalGriff 2,950
1 Maciej Los 1,910
2 KrunalRohit 1,862
3 CPallini 1,695
4 Richard MacCutchan 1,157

Advertise | Privacy | Mobile
Web04 | 2.8.151002.1 | Last Updated 10 Apr 2013
Copyright © CodeProject, 1999-2015
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