填充在GridView中单列多行的值 [英] populate the values of multiple rows in single row in gridview

查看:120
本文介绍了填充在GridView中单列多行的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用c像网页背后code的部分的RowDataBound锐来填充GridView中单列多行的值:

  COL1 COL61
a2
3
4

我已经改变了这

  COL1 COL61
            2
            3
            4

现在我要实现这一点,任何帮助吗?

  COL1 COL61 2 3 4

什么错低于code?

 如果(e.Row.Cells [0] ==。文本与&&安培;!e.Row.Cells [5]。文本=)
{
    对于(int类型的= 0; A< GridView1.Rows.Count; A ++)
    {
        字符串s = GridView1.Rows [α] .Cells [5]。文本;
        GridView1.Rows [e.Row.RowIndex - 1] .Cells [5]。文本+ =秒;
    }
}

我做什么错,任何人?

 细胞[0]是COL1
细胞[5]是COL6


解决方案

我觉得你是从错误的方向接近这一点。在 GridView控件看作只是你的显示器。你需要交给它关闭到 GridView控件之前的数据最好的,你可以安排。我不知道你是如何存储数据,但你可能想通过你的数据进行迭代,并创造新的纪录,基本上是一个分组,并把所有你在一个单行需要,你可以向数据绑定数据。

使用问题的RowDataBound (或任何数据绑定方法)是你在逐行基础上处理数据这是不是你想要的,因为你可以' T'看'其它行的数据在它的上下文中。

至于排列数据,可以使用下面的代码片段,让你的每一个COL1值的逗号分隔的列表:

 字符串commaValues​​ =的string.join(,
     (yourList.Where(ⅰ= GT; i.Col1.Equals(someValue中))。
      选择(S => s.YourStringValue)。)ToArray的()));

这将产生分隔字符串给定值( someValue中例子)一个逗号:

  commaValues​​ =1,2,3,4,5

使用上述弥补布置你可以绑定到它直接对象的列表。例如:

 类NewObject的
{
    公共字符串ID {搞定;组; }
    公共字符串文本{搞定;组; }
}清单<&NewObject的GT; arrangedData =新的List<&NewObject的GT;();
//此处安排数据
yourGrid.DataSource = arrangedData;
yourGrid.DataBind();

I am trying to populate the values of multiple rows in single row in gridview using c sharp in rowdatabound section of code behind page like:

Col1      Col6

a            1
a            2
a            3
a            4

I have changed this to

Col1     Col6

a           1 
            2
            3
            4

Now I want to achieve this, any help please?

Col1     Col6

a            1  2  3  4

What wrong with the code below?

if (e.Row.Cells[0].Text == "" && e.Row.Cells[5].Text != "")
{
    for (int a = 0; a<GridView1.Rows.Count; a++)
    {
        string s = GridView1.Rows[a].Cells[5].Text;
        GridView1.Rows[e.Row.RowIndex - 1].Cells[5].Text +=  s;
    }
}

What mistake I am making, anyone?

Cell[0]  is Col1
Cell[5]  is Col6

解决方案

I think you are approaching this from the wrong direction. Think of the GridView as your display only. You need to arrange the data as best you can before handing it off to the GridView. I am not sure how your data is stored but you might want to iterate through your data and create new records that essentially are a grouping and put all the data you need in a single row that you can databind to.

The problem with using RowDataBound (or any databinding methods) is that you are dealing with data on a row by row basis which is not what you want since you can't 'see' the other rows data in it's context.

As for arranging the data, you could use the following snippet to get you the comma seperated list of for each col1 value:

 string commaValues = string.Join(",",
     (yourList.Where(i => i.Col1.Equals(someValue)).
      Select(s => s.YourStringValue)).ToArray()));

This will produce a comma delimited string for a given value (someValue in example):

 commaValues = "1,2,3,4,5"

Use the above to make up a List of objects arranged so you can just bind to it directly. Eg:

class NewObject
{
    public string ID { get; set; }
    public string Text { get; set; }
}

List<NewObject> arrangedData = new List<NewObject>();
// arrange the data here
yourGrid.DataSource = arrangedData;
yourGrid.DataBind();

这篇关于填充在GridView中单列多行的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆