Click here to Skip to main content
14,668,708 members
Rate this:
Please Sign up or sign in to vote.
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
Grid_CustomColumnDisplayText

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";
              }
Posted
Updated 15-May-17 20:35pm
Comments
Maciej Los 16-May-17 2:08am
   
Forget about gridview! Keep focus on data! What is the source of gridview: datatable, dataset, ...?

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

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:
#1
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[^]

#2
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()
	.Select(x=>new
		{
			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")
		})
	.Select(x=>new
		{
			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
		})
	.ToList();
		
	result.Dump();


As you see, static headers have been used in above code.
   
Comments
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, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100