如何在一行中的split mulitple列中获取单列值 [英] How to get single column value in split mulitple column in a row
本文介绍了如何在一行中的split mulitple列中获取单列值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要单个grivew柱分割和显示muliple柱基于condtion
for ex;
+ ------ + ---------------- + ---------- +
| No |代码|数量|
+ ------ + ---------------- + ---------- +
| 1055 | 956,957,958,959 | 10,9,5,4 |
i需要
+ ------ + ---------------- + ---- ------ + ---------- +
| No | code1 |码2 | code3 | code4 |
+ ------ + ---------------- + ---------- + ---------- +
| 1055 | 956 | 957 | 958 | 959 |
---------- + ---------- + ---------- + ---------- + -
我的尝试:
im试过这个编码
Grid_CustomColumnDisplayText
属性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 +和+ qtval [i];
// e.DisplayText =test1;
}
解决方案
请先阅读我对该问题的评论。正如我所提到的,你必须把注意力集中在数据上,而不是gridview对象。
第一眼看你有两种方法可以达到这个目的:
#1
使用 CTE 在服务器端拆分数据[ ^ ]或自定义功能 [ ^ ]。
从数据库列拆分字符串并在gridview中显示 [ ^ ]
通过使用T-Sql分割长字符串来确定第2和第3个字符串 [ ^ ]
#2
使用数据集(数据表)在客户端拆分数据Linq
LINQ教程 - Linq to examples with examples。 [ ^ ]
看看例子:
DataTable dt = new DataTable();
dt.Columns.Add( new DataColumn( No, typeof ( int )));
dt.Columns.Add( new DataColumn( 代码, 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>( 代码)
.Split( new string [] { ,},StringSplitOptions.RemoveEmptyEntries)
。选择((a,b)= > new
{
Index = b,
Code = a
}),
Qty = x.Field< string>(< span class =code-string> qty)
})
.Select(x => new
{
否= x.No,
Code1 = x.Codes.Where(y => y.Index == 0 ).Select(z => z.Code).SingleOrDefault(),
Code2 = x.Codes.Where(y => y.Index == 1 )。选择(z => z.Code).SingleOrDefault(),
Code3 = x.Codes.Where(y => y.Index == 2 )。选择(z => z.Code).SingleOrDefault( ),
Code4 = x.Codes.Where(y => y.Index == 3 )。选择(z => z.Code) .SingleOrDefault(),
数量= x.Qty
})
.ToList();
result.Dump();
如您所见,上面的代码中使用了静态标头。
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"; }
解决方案
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.
这篇关于如何在一行中的split mulitple列中获取单列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文