Click here to Skip to main content
14,699,232 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i need single grivew column split and display muliple column based on condtion

for ex;

|No    |code   |           qty     |
|1055  |956,957,958,959 | 10,9,5,4 |

i need 

|No    |code1 |  code2|   code3  |  code4  |
|1055  |956   | 957     | 958      | 959   |

What I have tried:

i m tried this coding for

properties dev express gridview
if (e.Column.FieldName == "Product" || e.Column.FieldName == "Proddesc")
                  var Prodvalue = e.Value;
                  string[] procol = Prodvalue.ToString().Split(';');

                  for (int i = 0; i < 5; i++)
                      string[] qtval = procol[i].Split('[');
                      string Qtval1 = qtval[1].Substring(0, qtval[1].Length - 1);

                      //  e.DisplayText = qtyvalue123;
                //  e.DisplayText = Qtval1 + "and " + qtval[i];
                  //  e.DisplayText = "test1";
Updated 15-May-17 21:35pm
Maciej Los 16-May-17 2:08am
Forget about gridview! Keep focus on data! What is the source of gridview: datatable, dataset, ...?

1 solution

Please, read my comment to the question first. As i mentioned, you have to keep your focus on data, not gridview object.

On the first look you have 2 ways to achieve that:
Split data on server side by using CTE[^] or custom function[^].
split string from database column and display in gridview[^]
get 2nd and 3rd string by splitting a long string using T-Sql[^]

Split data on client side by using dataset (datatable) and Linq
LINQ Tutorial - Linq to strings with examples.[^]

Take a look at example:
DataTable dt = new DataTable();

dt.Columns.Add(new DataColumn("No", typeof(int)));
dt.Columns.Add(new DataColumn("Code", typeof(string)));
dt.Columns.Add(new DataColumn("qty", typeof(string)));
dt.Rows.Add(new object[]{1055, "956,957,958,959", "10,9,5,4"});

var result = dt.AsEnumerable()
			No = x.Field<int>("No"),
			Codes = x.Field<string>("Code")
				.Split(new string[]{","}, StringSplitOptions.RemoveEmptyEntries)
				.Select((a, b) => new
						Index = b,
						Code = a
			Qty = x.Field<string>("qty")
			No = x.No,
			Code1 = x.Codes.Where(y=>y.Index==0).Select(z=>z.Code).SingleOrDefault(),
			Code2 = x.Codes.Where(y=>y.Index==1).Select(z=>z.Code).SingleOrDefault(),
			Code3 = x.Codes.Where(y=>y.Index==2).Select(z=>z.Code).SingleOrDefault(),
			Code4 = x.Codes.Where(y=>y.Index==3).Select(z=>z.Code).SingleOrDefault(),
			Qty = x.Qty

As you see, static headers have been used in above code.
CHill60 16-May-17 6:58am
The funniest part about this is this question from the same OP - How to select resultant rows in a single column in SQL server[^] - they had the data individually originally and wanted it as comma-separated data. I think they actually need PIVOT
Maciej Los 16-May-17 10:50am
Some people are lazy and don't want to write its own data. ;)

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