Click here to Skip to main content
15,920,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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,
C#
string a = "Abc.jpg";

So, Please help me how can we split one datagridview column value?

Thanks
Ankit Agarwal
Software Engineer
Posted
v2
Comments
[no name] 10-Apr-13 8:20am    
Okay so use Split, SubString or a regular expression.
[no name] 10-Apr-13 8:22am    
but how, can you give me the code according to datagridview column value split?
PrashantSonewane 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.

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++;
}
 
Share this answer
 
v5
Comments
CHill60 10-Apr-13 11:22am    
URBANA_GREY - C.jpg is being returned as C.jpg for me
Pallavi Waikar 10-Apr-13 12:00pm    
for that use regex as
MatchCollection matches = Regex.Matches(s, @"(\w*?\s*?-?\s*?\w*.jpg)");
CHill60 10-Apr-13 12:27pm    
Yep - that's better. But also "Daino Royal - A.jpg" still returned as "Royal - A.jpg"
Pallavi Waikar 10-Apr-13 12:51pm    
ok then try this one
MatchCollection matches = Regex.Matches(s, @"((\w*?\s*?-?\s*?){0,}\w*.jpg)");
Pallavi Waikar 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);
}
Here's one way of doing it ...

C#
// 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);
 
Share this answer
 
v2
Comments
[no name] 10-Apr-13 8:53am    
I need only image name .jpg,
I need not \Library\
[no name] 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 10-Apr-13 9:07am    
I've added a call to .Replace to my solution - also see comment from PrashantSonewane
[no name] 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 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?

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900